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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
<head>
|
4 |
+
<title>Tank Battle</title>
|
5 |
+
<style>
|
6 |
+
body {
|
7 |
+
margin: 0;
|
8 |
+
overflow: hidden;
|
9 |
+
background: #333;
|
10 |
+
font-family: Arial;
|
11 |
+
}
|
12 |
+
#gameCanvas {
|
13 |
+
background-repeat: repeat;
|
14 |
+
}
|
15 |
+
#instructions {
|
16 |
+
position: fixed;
|
17 |
+
top: 10px;
|
18 |
+
right: 10px;
|
19 |
+
color: white;
|
20 |
+
background: rgba(0,0,0,0.7);
|
21 |
+
padding: 10px;
|
22 |
+
border-radius: 5px;
|
23 |
+
z-index: 1000;
|
24 |
+
}
|
25 |
+
#weaponInfo {
|
26 |
+
position: fixed;
|
27 |
+
top: 150px;
|
28 |
+
right: 10px;
|
29 |
+
color: white;
|
30 |
+
background: rgba(0,0,0,0.7);
|
31 |
+
padding: 10px;
|
32 |
+
border-radius: 5px;
|
33 |
+
z-index: 1000;
|
34 |
+
font-size: 18px;
|
35 |
+
}
|
36 |
+
.button {
|
37 |
+
position: fixed;
|
38 |
+
top: 50%;
|
39 |
+
left: 50%;
|
40 |
+
transform: translate(-50%, -50%);
|
41 |
+
padding: 20px 40px;
|
42 |
+
font-size: 24px;
|
43 |
+
background: #4CAF50;
|
44 |
+
color: white;
|
45 |
+
border: none;
|
46 |
+
border-radius: 5px;
|
47 |
+
cursor: pointer;
|
48 |
+
display: none;
|
49 |
+
z-index: 1000;
|
50 |
+
}
|
51 |
+
#countdown {
|
52 |
+
position: fixed;
|
53 |
+
top: 50%;
|
54 |
+
left: 50%;
|
55 |
+
transform: translate(-50%, -50%);
|
56 |
+
font-size: 72px;
|
57 |
+
color: white;
|
58 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
|
59 |
+
z-index: 1000;
|
60 |
+
display: none;
|
61 |
+
}
|
62 |
+
</style>
|
63 |
+
</head>
|
64 |
+
<body>
|
65 |
+
<div id="instructions">
|
66 |
+
Controls:<br>
|
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>
|
75 |
+
<button id="nextRound" class="button">Next Round</button>
|
76 |
+
<button id="restart" class="button">Restart Game</button>
|
77 |
+
<canvas id="gameCanvas"></canvas>
|
78 |
+
|
79 |
+
<script>
|
80 |
+
const canvas = document.getElementById('gameCanvas');
|
81 |
+
const ctx = canvas.getContext('2d');
|
82 |
+
const nextRoundBtn = document.getElementById('nextRound');
|
83 |
+
const restartBtn = document.getElementById('restart');
|
84 |
+
const weaponInfo = document.getElementById('weaponInfo');
|
85 |
+
const countdownEl = document.getElementById('countdown');
|
86 |
+
canvas.width = window.innerWidth;
|
87 |
+
canvas.height = window.innerHeight;
|
88 |
+
// Game state
|
89 |
+
let currentRound = 1;
|
90 |
+
let gameOver = false;
|
91 |
+
let currentWeapon = 'cannon';
|
92 |
+
let enemies = [];
|
93 |
+
let bullets = [];
|
94 |
+
let items = [];
|
95 |
+
let lastShot = 0;
|
96 |
+
let isCountingDown = true;
|
97 |
+
let countdownTime = 3;
|
98 |
+
let autoFire = false;
|
99 |
+
|
100 |
+
// Load assets
|
101 |
+
const backgroundImg = new Image();
|
102 |
+
backgroundImg.src = 'city.png';
|
103 |
+
const playerImg = new Image();
|
104 |
+
playerImg.src = 'player.png';
|
105 |
+
|
106 |
+
const enemyImg = new Image();
|
107 |
+
enemyImg.src = 'enemy.png';
|
108 |
+
// Audio setup
|
109 |
+
const cannonSound = new Audio('firemn.ogg');
|
110 |
+
const machinegunSound = new Audio('firemg.ogg');
|
111 |
+
const enemyFireSound = new Audio('fireenemy.ogg');
|
112 |
+
enemyFireSound.volume = 0.5;
|
113 |
+
const weapons = {
|
114 |
+
cannon: {
|
115 |
+
fireRate: 1000,
|
116 |
+
damage: 0.25,
|
117 |
+
bulletSize: 5,
|
118 |
+
sound: cannonSound
|
119 |
+
},
|
120 |
+
machinegun: {
|
121 |
+
fireRate: 200,
|
122 |
+
damage: 0.05,
|
123 |
+
bulletSize: 2,
|
124 |
+
sound: machinegunSound
|
125 |
+
}
|
126 |
+
};
|
127 |
+
// Player setup
|
128 |
+
const player = {
|
129 |
+
x: canvas.width/2,
|
130 |
+
y: canvas.height/2,
|
131 |
+
speed: 5,
|
132 |
+
angle: 0,
|
133 |
+
width: 100,
|
134 |
+
height: 45,
|
135 |
+
health: 1000,
|
136 |
+
maxHealth: 1000
|
137 |
+
};
|
138 |
+
function startCountdown() {
|
139 |
+
isCountingDown = true;
|
140 |
+
countdownTime = 3;
|
141 |
+
countdownEl.style.display = 'block';
|
142 |
+
countdownEl.textContent = countdownTime;
|
143 |
+
const countInterval = setInterval(() => {
|
144 |
+
countdownTime--;
|
145 |
+
if(countdownTime <= 0) {
|
146 |
+
clearInterval(countInterval);
|
147 |
+
countdownEl.style.display = 'none';
|
148 |
+
isCountingDown = false;
|
149 |
+
}
|
150 |
+
countdownEl.textContent = countdownTime > 0 ? countdownTime : 'GO!';
|
151 |
+
}, 1000);
|
152 |
+
}
|
153 |
+
class Enemy {
|
154 |
+
constructor() {
|
155 |
+
this.x = Math.random() * canvas.width;
|
156 |
+
this.y = Math.random() * canvas.height;
|
157 |
+
this.health = 1000;
|
158 |
+
this.maxHealth = 1000;
|
159 |
+
this.speed = 2;
|
160 |
+
this.lastShot = 0;
|
161 |
+
this.shootInterval = 1000;
|
162 |
+
this.angle = 0;
|
163 |
+
this.width = 100;
|
164 |
+
this.height = 45;
|
165 |
+
this.moveTimer = 0;
|
166 |
+
this.moveInterval = Math.random() * 2000 + 1000;
|
167 |
+
this.moveAngle = Math.random() * Math.PI * 2;
|
168 |
+
}
|
169 |
+
update() {
|
170 |
+
if(isCountingDown) return;
|
171 |
+
const now = Date.now();
|
172 |
+
|
173 |
+
// Movement
|
174 |
+
if (now - this.moveTimer > this.moveInterval) {
|
175 |
+
this.moveAngle = Math.random() * Math.PI * 2;
|
176 |
+
this.moveTimer = now;
|
177 |
+
}
|
178 |
+
this.x += Math.cos(this.moveAngle) * this.speed;
|
179 |
+
this.y += Math.sin(this.moveAngle) * this.speed;
|
180 |
+
// Bounds check
|
181 |
+
this.x = Math.max(this.width/2, Math.min(canvas.width - this.width/2, this.x));
|
182 |
+
this.y = Math.max(this.height/2, Math.min(canvas.height - this.height/2, this.y));
|
183 |
+
// Aim at player
|
184 |
+
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
|
185 |
+
// Shooting
|
186 |
+
if (now - this.lastShot > this.shootInterval && !isCountingDown) {
|
187 |
+
this.shoot();
|
188 |
+
this.lastShot = now;
|
189 |
+
}
|
190 |
+
}
|
191 |
+
shoot() {
|
192 |
+
enemyFireSound.cloneNode().play();
|
193 |
+
bullets.push({
|
194 |
+
x: this.x + Math.cos(this.angle) * 30,
|
195 |
+
y: this.y + Math.sin(this.angle) * 30,
|
196 |
+
angle: this.angle,
|
197 |
+
speed: 5,
|
198 |
+
isEnemy: true,
|
199 |
+
size: 3
|
200 |
+
});
|
201 |
+
}
|
202 |
+
}
|
203 |
+
function initRound() {
|
204 |
+
enemies = [];
|
205 |
+
for(let i = 0; i < 1 * currentRound; i++) {
|
206 |
+
enemies.push(new Enemy());
|
207 |
+
}
|
208 |
+
player.health = player.maxHealth;
|
209 |
+
bullets = [];
|
210 |
+
items = [];
|
211 |
+
startCountdown();
|
212 |
+
}
|
213 |
+
canvas.addEventListener('mousemove', (e) => {
|
214 |
+
player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
|
215 |
+
});
|
216 |
+
const keys = {};
|
217 |
+
document.addEventListener('keydown', e => {
|
218 |
+
keys[e.key] = true;
|
219 |
+
if(e.key.toLowerCase() === 'c') {
|
220 |
+
currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
|
221 |
+
weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
|
222 |
+
} else if(e.key.toLowerCase() === 'r') {
|
223 |
+
autoFire = !autoFire;
|
224 |
+
}
|
225 |
+
});
|
226 |
+
document.addEventListener('keyup', e => keys[e.key] = false);
|
227 |
+
function fireBullet() {
|
228 |
+
if(isCountingDown) return;
|
229 |
+
const weapon = weapons[currentWeapon];
|
230 |
+
const now = Date.now();
|
231 |
+
if ((keys[' '] || autoFire) && now - lastShot > weapon.fireRate) {
|
232 |
+
weapon.sound.cloneNode().play();
|
233 |
+
bullets.push({
|
234 |
+
x: player.x + Math.cos(player.angle) * 30,
|
235 |
+
y: player.y + Math.sin(player.angle) * 30,
|
236 |
+
angle: player.angle,
|
237 |
+
speed: 10,
|
238 |
+
isEnemy: false,
|
239 |
+
damage: weapon.damage,
|
240 |
+
size: weapon.bulletSize
|
241 |
+
});
|
242 |
+
lastShot = now;
|
243 |
+
}
|
244 |
+
}
|
245 |
+
function updateGame() {
|
246 |
+
if(gameOver) return;
|
247 |
+
if(!isCountingDown) {
|
248 |
+
if(keys['w']) player.y -= player.speed;
|
249 |
+
if(keys['s']) player.y += player.speed;
|
250 |
+
if(keys['a']) player.x -= player.speed;
|
251 |
+
if(keys['d']) player.x += player.speed;
|
252 |
+
player.x = Math.max(player.width/2, Math.min(canvas.width - player.width/2, player.x));
|
253 |
+
player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
|
254 |
+
fireBullet();
|
255 |
+
}
|
256 |
+
enemies.forEach(enemy => enemy.update());
|
257 |
+
if(!isCountingDown) {
|
258 |
+
// Update bullets and collisions
|
259 |
+
bullets = bullets.filter(bullet => {
|
260 |
+
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
261 |
+
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
262 |
+
if(!bullet.isEnemy) {
|
263 |
+
enemies = enemies.filter(enemy => {
|
264 |
+
const dist = Math.hypot(bullet.x - enemy.x, bullet.y - enemy.y);
|
265 |
+
if(dist < 30) {
|
266 |
+
enemy.health -= enemy.maxHealth * bullet.damage;
|
267 |
+
if(enemy.health <= 0) {
|
268 |
+
spawnHealthItem(enemy.x, enemy.y);
|
269 |
+
return false;
|
270 |
+
}
|
271 |
+
return true;
|
272 |
+
}
|
273 |
+
return true;
|
274 |
+
});
|
275 |
+
} else {
|
276 |
+
const dist = Math.hypot(bullet.x - player.x, bullet.y - player.y);
|
277 |
+
if(dist < 30) {
|
278 |
+
player.health -= 100;
|
279 |
+
if(player.health <= 0) {
|
280 |
+
gameOver = true;
|
281 |
+
restartBtn.style.display = 'block';
|
282 |
+
}
|
283 |
+
return false;
|
284 |
+
}
|
285 |
+
}
|
286 |
+
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
287 |
+
bullet.y >= 0 && bullet.y <= canvas.height;
|
288 |
+
});
|
289 |
+
items = items.filter(item => {
|
290 |
+
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
291 |
+
if(dist < 30) {
|
292 |
+
player.health = Math.min(player.health + 200, player.maxHealth);
|
293 |
+
return false;
|
294 |
+
}
|
295 |
+
return true;
|
296 |
+
});
|
297 |
+
if(enemies.length === 0) {
|
298 |
+
if(currentRound < 10) {
|
299 |
+
nextRoundBtn.style.display = 'block';
|
300 |
+
} else {
|
301 |
+
gameOver = true;
|
302 |
+
restartBtn.style.display = 'block';
|
303 |
+
}
|
304 |
+
}
|
305 |
+
}
|
306 |
+
}
|
307 |
+
function spawnHealthItem(x, y) {
|
308 |
+
items.push({x, y});
|
309 |
+
}
|
310 |
+
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
311 |
+
ctx.fillStyle = '#333';
|
312 |
+
ctx.fillRect(x - width/2, y - height/2, width, height);
|
313 |
+
ctx.fillStyle = color;
|
314 |
+
ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
|
315 |
+
}
|
316 |
+
function drawGame() {
|
317 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
318 |
+
const pattern = ctx.createPattern(backgroundImg, 'repeat');
|
319 |
+
ctx.fillStyle = pattern;
|
320 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
321 |
+
// Draw all game objects
|
322 |
+
ctx.save();
|
323 |
+
ctx.translate(player.x, player.y);
|
324 |
+
ctx.rotate(player.angle);
|
325 |
+
ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
|
326 |
+
ctx.restore();
|
327 |
+
drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
|
328 |
+
enemies.forEach(enemy => {
|
329 |
+
ctx.save();
|
330 |
+
ctx.translate(enemy.x, enemy.y);
|
331 |
+
ctx.rotate(enemy.angle);
|
332 |
+
ctx.drawImage(enemyImg, -enemy.width/2, -enemy.height/2, enemy.width, enemy.height);
|
333 |
+
ctx.restore();
|
334 |
+
drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 60, 5, 'red');
|
335 |
+
});
|
336 |
+
bullets.forEach(bullet => {
|
337 |
+
ctx.beginPath();
|
338 |
+
ctx.fillStyle = bullet.isEnemy ? 'red' : 'yellow';
|
339 |
+
ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
|
340 |
+
ctx.fill();
|
341 |
+
});
|
342 |
+
items.forEach(item => {
|
343 |
+
ctx.beginPath();
|
344 |
+
ctx.fillStyle = 'green';
|
345 |
+
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
|
346 |
+
ctx.fill();
|
347 |
+
});
|
348 |
+
ctx.fillStyle = 'white';
|
349 |
+
ctx.font = '24px Arial';
|
350 |
+
ctx.fillText(`Round ${currentRound}/10`, 10, 30);
|
351 |
+
if(isCountingDown) {
|
352 |
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
353 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
354 |
+
}
|
355 |
+
}
|
356 |
+
function gameLoop() {
|
357 |
+
updateGame();
|
358 |
+
drawGame();
|
359 |
+
requestAnimationFrame(gameLoop);
|
360 |
+
}
|
361 |
+
nextRoundBtn.addEventListener('click', () => {
|
362 |
+
currentRound++;
|
363 |
+
nextRoundBtn.style.display = 'none';
|
364 |
+
initRound();
|
365 |
+
});
|
366 |
+
restartBtn.addEventListener('click', () => {
|
367 |
+
currentRound = 1;
|
368 |
+
gameOver = false;
|
369 |
+
restartBtn.style.display = 'none';
|
370 |
+
initRound();
|
371 |
+
});
|
372 |
+
Promise.all([
|
373 |
+
new Promise(resolve => backgroundImg.onload = resolve),
|
374 |
+
new Promise(resolve => playerImg.onload = resolve),
|
375 |
+
new Promise(resolve => enemyImg.onload = resolve)
|
376 |
+
]).then(() => {
|
377 |
+
initRound();
|
378 |
+
gameLoop();
|
379 |
+
});
|
380 |
+
window.addEventListener('resize', () => {
|
381 |
+
canvas.width = window.innerWidth;
|
382 |
+
canvas.height = window.innerHeight;
|
383 |
+
});
|
384 |
+
</script>
|
385 |
+
</body>
|
386 |
+
</html>
|