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