Spaces:
Running
Running
Update index.html
Browse files- index.html +16 -42
index.html
CHANGED
@@ -67,8 +67,7 @@
|
|
67 |
WASD - Move tank<br>
|
68 |
Mouse - Aim<br>
|
69 |
Space - Fire<br>
|
70 |
-
C - Switch Weapon
|
71 |
-
R - Toggle Auto-fire
|
72 |
</div>
|
73 |
<div id="weaponInfo">Current Weapon: Cannon</div>
|
74 |
<div id="countdown">3</div>
|
@@ -85,7 +84,6 @@
|
|
85 |
const countdownEl = document.getElementById('countdown');
|
86 |
canvas.width = window.innerWidth;
|
87 |
canvas.height = window.innerHeight;
|
88 |
-
|
89 |
// Game state
|
90 |
let currentRound = 1;
|
91 |
let gameOver = false;
|
@@ -96,22 +94,20 @@
|
|
96 |
let lastShot = 0;
|
97 |
let isCountingDown = true;
|
98 |
let countdownTime = 3;
|
99 |
-
let autoFire = false;
|
100 |
|
101 |
// Load assets
|
102 |
const backgroundImg = new Image();
|
103 |
backgroundImg.src = 'city.png';
|
104 |
const playerImg = new Image();
|
105 |
playerImg.src = 'player.png';
|
|
|
106 |
const enemyImg = new Image();
|
107 |
enemyImg.src = 'enemy.png';
|
108 |
-
|
109 |
// Audio setup
|
110 |
const cannonSound = new Audio('firemn.ogg');
|
111 |
const machinegunSound = new Audio('firemg.ogg');
|
112 |
const enemyFireSound = new Audio('fireenemy.ogg');
|
113 |
enemyFireSound.volume = 0.5;
|
114 |
-
|
115 |
const weapons = {
|
116 |
cannon: {
|
117 |
fireRate: 1000,
|
@@ -126,7 +122,6 @@
|
|
126 |
sound: machinegunSound
|
127 |
}
|
128 |
};
|
129 |
-
|
130 |
// Player setup
|
131 |
const player = {
|
132 |
x: canvas.width/2,
|
@@ -138,7 +133,6 @@
|
|
138 |
health: 1000,
|
139 |
maxHealth: 1000
|
140 |
};
|
141 |
-
|
142 |
function startCountdown() {
|
143 |
isCountingDown = true;
|
144 |
countdownTime = 3;
|
@@ -154,7 +148,6 @@
|
|
154 |
countdownEl.textContent = countdownTime > 0 ? countdownTime : 'GO!';
|
155 |
}, 1000);
|
156 |
}
|
157 |
-
|
158 |
class Enemy {
|
159 |
constructor() {
|
160 |
this.x = Math.random() * canvas.width;
|
@@ -171,7 +164,6 @@
|
|
171 |
this.moveInterval = Math.random() * 2000 + 1000;
|
172 |
this.moveAngle = Math.random() * Math.PI * 2;
|
173 |
}
|
174 |
-
|
175 |
update() {
|
176 |
if(isCountingDown) return;
|
177 |
const now = Date.now();
|
@@ -183,21 +175,17 @@
|
|
183 |
}
|
184 |
this.x += Math.cos(this.moveAngle) * this.speed;
|
185 |
this.y += Math.sin(this.moveAngle) * this.speed;
|
186 |
-
|
187 |
// Bounds check
|
188 |
this.x = Math.max(this.width/2, Math.min(canvas.width - this.width/2, this.x));
|
189 |
this.y = Math.max(this.height/2, Math.min(canvas.height - this.height/2, this.y));
|
190 |
-
|
191 |
// Aim at player
|
192 |
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
|
193 |
-
|
194 |
// Shooting
|
195 |
if (now - this.lastShot > this.shootInterval && !isCountingDown) {
|
196 |
this.shoot();
|
197 |
this.lastShot = now;
|
198 |
}
|
199 |
}
|
200 |
-
|
201 |
shoot() {
|
202 |
enemyFireSound.cloneNode().play();
|
203 |
bullets.push({
|
@@ -210,7 +198,6 @@
|
|
210 |
});
|
211 |
}
|
212 |
}
|
213 |
-
|
214 |
function initRound() {
|
215 |
enemies = [];
|
216 |
for(let i = 0; i < 1 * currentRound; i++) {
|
@@ -221,29 +208,23 @@
|
|
221 |
items = [];
|
222 |
startCountdown();
|
223 |
}
|
224 |
-
|
225 |
document.addEventListener('keydown', (e) => {
|
226 |
if(e.key.toLowerCase() === 'c') {
|
227 |
currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
|
228 |
weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
|
229 |
-
} else if(e.key.toLowerCase() === 'r') {
|
230 |
-
autoFire = !autoFire;
|
231 |
}
|
232 |
});
|
233 |
-
|
234 |
canvas.addEventListener('mousemove', (e) => {
|
235 |
player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
|
236 |
});
|
237 |
-
|
238 |
const keys = {};
|
239 |
document.addEventListener('keydown', e => keys[e.key] = true);
|
240 |
document.addEventListener('keyup', e => keys[e.key] = false);
|
241 |
-
|
242 |
function fireBullet() {
|
243 |
if(isCountingDown) return;
|
244 |
const weapon = weapons[currentWeapon];
|
245 |
const now = Date.now();
|
246 |
-
if (
|
247 |
weapon.sound.cloneNode().play();
|
248 |
bullets.push({
|
249 |
x: player.x + Math.cos(player.angle) * 30,
|
@@ -257,7 +238,6 @@
|
|
257 |
lastShot = now;
|
258 |
}
|
259 |
}
|
260 |
-
|
261 |
function updateGame() {
|
262 |
if(gameOver) return;
|
263 |
if(!isCountingDown) {
|
@@ -269,9 +249,7 @@
|
|
269 |
player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
|
270 |
fireBullet();
|
271 |
}
|
272 |
-
|
273 |
enemies.forEach(enemy => enemy.update());
|
274 |
-
|
275 |
if(!isCountingDown) {
|
276 |
// Update bullets and collisions
|
277 |
bullets = bullets.filter(bullet => {
|
@@ -304,7 +282,6 @@
|
|
304 |
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
305 |
bullet.y >= 0 && bullet.y <= canvas.height;
|
306 |
});
|
307 |
-
|
308 |
items = items.filter(item => {
|
309 |
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
310 |
if(dist < 30) {
|
@@ -313,7 +290,6 @@
|
|
313 |
}
|
314 |
return true;
|
315 |
});
|
316 |
-
|
317 |
if(enemies.length === 0) {
|
318 |
if(currentRound < 10) {
|
319 |
nextRoundBtn.style.display = 'block';
|
@@ -324,33 +300,27 @@
|
|
324 |
}
|
325 |
}
|
326 |
}
|
327 |
-
|
328 |
function spawnHealthItem(x, y) {
|
329 |
items.push({x, y});
|
330 |
}
|
331 |
-
|
332 |
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
333 |
ctx.fillStyle = '#333';
|
334 |
ctx.fillRect(x - width/2, y - height/2, width, height);
|
335 |
ctx.fillStyle = color;
|
336 |
ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
|
337 |
}
|
338 |
-
|
339 |
function drawGame() {
|
340 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
341 |
const pattern = ctx.createPattern(backgroundImg, 'repeat');
|
342 |
ctx.fillStyle = pattern;
|
343 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
344 |
-
|
345 |
// Draw all game objects
|
346 |
ctx.save();
|
347 |
ctx.translate(player.x, player.y);
|
348 |
ctx.rotate(player.angle);
|
349 |
ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
|
350 |
ctx.restore();
|
351 |
-
|
352 |
drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
|
353 |
-
|
354 |
enemies.forEach(enemy => {
|
355 |
ctx.save();
|
356 |
ctx.translate(enemy.x, enemy.y);
|
@@ -359,50 +329,54 @@
|
|
359 |
ctx.restore();
|
360 |
drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 60, 5, 'red');
|
361 |
});
|
362 |
-
|
363 |
bullets.forEach(bullet => {
|
364 |
ctx.beginPath();
|
365 |
ctx.fillStyle = bullet.isEnemy ? 'red' : 'yellow';
|
366 |
ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
|
367 |
ctx.fill();
|
368 |
});
|
369 |
-
|
370 |
items.forEach(item => {
|
371 |
ctx.beginPath();
|
372 |
ctx.fillStyle = 'green';
|
373 |
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
|
374 |
ctx.fill();
|
375 |
});
|
376 |
-
|
377 |
ctx.fillStyle = 'white';
|
378 |
ctx.font = '24px Arial';
|
379 |
ctx.fillText(`Round ${currentRound}/10`, 10, 30);
|
380 |
-
|
381 |
if(isCountingDown) {
|
382 |
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
383 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
384 |
}
|
385 |
}
|
386 |
-
|
387 |
function gameLoop() {
|
388 |
updateGame();
|
389 |
drawGame();
|
390 |
requestAnimationFrame(gameLoop);
|
391 |
}
|
392 |
-
|
393 |
nextRoundBtn.addEventListener('click', () => {
|
394 |
currentRound++;
|
395 |
nextRoundBtn.style.display = 'none';
|
396 |
initRound();
|
397 |
});
|
398 |
-
|
399 |
restartBtn.addEventListener('click', () => {
|
400 |
currentRound = 1;
|
401 |
gameOver = false;
|
402 |
restartBtn.style.display = 'none';
|
403 |
initRound();
|
404 |
});
|
405 |
-
|
406 |
Promise.all([
|
407 |
new Promise(resolve => backgroundImg.onload = resolve),
|
408 |
-
new Promise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
WASD - Move tank<br>
|
68 |
Mouse - Aim<br>
|
69 |
Space - Fire<br>
|
70 |
+
C - Switch Weapon
|
|
|
71 |
</div>
|
72 |
<div id="weaponInfo">Current Weapon: Cannon</div>
|
73 |
<div id="countdown">3</div>
|
|
|
84 |
const countdownEl = document.getElementById('countdown');
|
85 |
canvas.width = window.innerWidth;
|
86 |
canvas.height = window.innerHeight;
|
|
|
87 |
// Game state
|
88 |
let currentRound = 1;
|
89 |
let gameOver = false;
|
|
|
94 |
let lastShot = 0;
|
95 |
let isCountingDown = true;
|
96 |
let countdownTime = 3;
|
|
|
97 |
|
98 |
// Load assets
|
99 |
const backgroundImg = new Image();
|
100 |
backgroundImg.src = 'city.png';
|
101 |
const playerImg = new Image();
|
102 |
playerImg.src = 'player.png';
|
103 |
+
|
104 |
const enemyImg = new Image();
|
105 |
enemyImg.src = 'enemy.png';
|
|
|
106 |
// Audio setup
|
107 |
const cannonSound = new Audio('firemn.ogg');
|
108 |
const machinegunSound = new Audio('firemg.ogg');
|
109 |
const enemyFireSound = new Audio('fireenemy.ogg');
|
110 |
enemyFireSound.volume = 0.5;
|
|
|
111 |
const weapons = {
|
112 |
cannon: {
|
113 |
fireRate: 1000,
|
|
|
122 |
sound: machinegunSound
|
123 |
}
|
124 |
};
|
|
|
125 |
// Player setup
|
126 |
const player = {
|
127 |
x: canvas.width/2,
|
|
|
133 |
health: 1000,
|
134 |
maxHealth: 1000
|
135 |
};
|
|
|
136 |
function startCountdown() {
|
137 |
isCountingDown = true;
|
138 |
countdownTime = 3;
|
|
|
148 |
countdownEl.textContent = countdownTime > 0 ? countdownTime : 'GO!';
|
149 |
}, 1000);
|
150 |
}
|
|
|
151 |
class Enemy {
|
152 |
constructor() {
|
153 |
this.x = Math.random() * canvas.width;
|
|
|
164 |
this.moveInterval = Math.random() * 2000 + 1000;
|
165 |
this.moveAngle = Math.random() * Math.PI * 2;
|
166 |
}
|
|
|
167 |
update() {
|
168 |
if(isCountingDown) return;
|
169 |
const now = Date.now();
|
|
|
175 |
}
|
176 |
this.x += Math.cos(this.moveAngle) * this.speed;
|
177 |
this.y += Math.sin(this.moveAngle) * this.speed;
|
|
|
178 |
// Bounds check
|
179 |
this.x = Math.max(this.width/2, Math.min(canvas.width - this.width/2, this.x));
|
180 |
this.y = Math.max(this.height/2, Math.min(canvas.height - this.height/2, this.y));
|
|
|
181 |
// Aim at player
|
182 |
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
|
|
|
183 |
// Shooting
|
184 |
if (now - this.lastShot > this.shootInterval && !isCountingDown) {
|
185 |
this.shoot();
|
186 |
this.lastShot = now;
|
187 |
}
|
188 |
}
|
|
|
189 |
shoot() {
|
190 |
enemyFireSound.cloneNode().play();
|
191 |
bullets.push({
|
|
|
198 |
});
|
199 |
}
|
200 |
}
|
|
|
201 |
function initRound() {
|
202 |
enemies = [];
|
203 |
for(let i = 0; i < 1 * currentRound; i++) {
|
|
|
208 |
items = [];
|
209 |
startCountdown();
|
210 |
}
|
|
|
211 |
document.addEventListener('keydown', (e) => {
|
212 |
if(e.key.toLowerCase() === 'c') {
|
213 |
currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
|
214 |
weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
|
|
|
|
|
215 |
}
|
216 |
});
|
|
|
217 |
canvas.addEventListener('mousemove', (e) => {
|
218 |
player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
|
219 |
});
|
|
|
220 |
const keys = {};
|
221 |
document.addEventListener('keydown', e => keys[e.key] = true);
|
222 |
document.addEventListener('keyup', e => keys[e.key] = false);
|
|
|
223 |
function fireBullet() {
|
224 |
if(isCountingDown) return;
|
225 |
const weapon = weapons[currentWeapon];
|
226 |
const now = Date.now();
|
227 |
+
if (keys[' '] && now - lastShot > weapon.fireRate) {
|
228 |
weapon.sound.cloneNode().play();
|
229 |
bullets.push({
|
230 |
x: player.x + Math.cos(player.angle) * 30,
|
|
|
238 |
lastShot = now;
|
239 |
}
|
240 |
}
|
|
|
241 |
function updateGame() {
|
242 |
if(gameOver) return;
|
243 |
if(!isCountingDown) {
|
|
|
249 |
player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
|
250 |
fireBullet();
|
251 |
}
|
|
|
252 |
enemies.forEach(enemy => enemy.update());
|
|
|
253 |
if(!isCountingDown) {
|
254 |
// Update bullets and collisions
|
255 |
bullets = bullets.filter(bullet => {
|
|
|
282 |
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
283 |
bullet.y >= 0 && bullet.y <= canvas.height;
|
284 |
});
|
|
|
285 |
items = items.filter(item => {
|
286 |
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
287 |
if(dist < 30) {
|
|
|
290 |
}
|
291 |
return true;
|
292 |
});
|
|
|
293 |
if(enemies.length === 0) {
|
294 |
if(currentRound < 10) {
|
295 |
nextRoundBtn.style.display = 'block';
|
|
|
300 |
}
|
301 |
}
|
302 |
}
|
|
|
303 |
function spawnHealthItem(x, y) {
|
304 |
items.push({x, y});
|
305 |
}
|
|
|
306 |
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
307 |
ctx.fillStyle = '#333';
|
308 |
ctx.fillRect(x - width/2, y - height/2, width, height);
|
309 |
ctx.fillStyle = color;
|
310 |
ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
|
311 |
}
|
|
|
312 |
function drawGame() {
|
313 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
314 |
const pattern = ctx.createPattern(backgroundImg, 'repeat');
|
315 |
ctx.fillStyle = pattern;
|
316 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
317 |
// Draw all game objects
|
318 |
ctx.save();
|
319 |
ctx.translate(player.x, player.y);
|
320 |
ctx.rotate(player.angle);
|
321 |
ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
|
322 |
ctx.restore();
|
|
|
323 |
drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
|
|
|
324 |
enemies.forEach(enemy => {
|
325 |
ctx.save();
|
326 |
ctx.translate(enemy.x, enemy.y);
|
|
|
329 |
ctx.restore();
|
330 |
drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 60, 5, 'red');
|
331 |
});
|
|
|
332 |
bullets.forEach(bullet => {
|
333 |
ctx.beginPath();
|
334 |
ctx.fillStyle = bullet.isEnemy ? 'red' : 'yellow';
|
335 |
ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
|
336 |
ctx.fill();
|
337 |
});
|
|
|
338 |
items.forEach(item => {
|
339 |
ctx.beginPath();
|
340 |
ctx.fillStyle = 'green';
|
341 |
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
|
342 |
ctx.fill();
|
343 |
});
|
|
|
344 |
ctx.fillStyle = 'white';
|
345 |
ctx.font = '24px Arial';
|
346 |
ctx.fillText(`Round ${currentRound}/10`, 10, 30);
|
|
|
347 |
if(isCountingDown) {
|
348 |
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
349 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
350 |
}
|
351 |
}
|
|
|
352 |
function gameLoop() {
|
353 |
updateGame();
|
354 |
drawGame();
|
355 |
requestAnimationFrame(gameLoop);
|
356 |
}
|
|
|
357 |
nextRoundBtn.addEventListener('click', () => {
|
358 |
currentRound++;
|
359 |
nextRoundBtn.style.display = 'none';
|
360 |
initRound();
|
361 |
});
|
|
|
362 |
restartBtn.addEventListener('click', () => {
|
363 |
currentRound = 1;
|
364 |
gameOver = false;
|
365 |
restartBtn.style.display = 'none';
|
366 |
initRound();
|
367 |
});
|
|
|
368 |
Promise.all([
|
369 |
new Promise(resolve => backgroundImg.onload = resolve),
|
370 |
+
new Promise(resolve => playerImg.onload = resolve),
|
371 |
+
new Promise(resolve => enemyImg.onload = resolve)
|
372 |
+
]).then(() => {
|
373 |
+
initRound();
|
374 |
+
gameLoop();
|
375 |
+
});
|
376 |
+
window.addEventListener('resize', () => {
|
377 |
+
canvas.width = window.innerWidth;
|
378 |
+
canvas.height = window.innerHeight;
|
379 |
+
});
|
380 |
+
</script>
|
381 |
+
</body>
|
382 |
+
</html>
|