Spaces:
Running
Running
cutechicken
commited on
Update index.html
Browse files- index.html +280 -296
index.html
CHANGED
@@ -1,381 +1,365 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
|
|
4 |
<style>
|
5 |
body {
|
6 |
margin: 0;
|
7 |
overflow: hidden;
|
8 |
-
background: #1a1a1a;
|
9 |
-
font-family: Arial;
|
10 |
-
}
|
11 |
-
#hud {
|
12 |
-
position: fixed;
|
13 |
-
top: 20px;
|
14 |
-
left: 20px;
|
15 |
-
color: white;
|
16 |
-
}
|
17 |
-
#healthBar {
|
18 |
-
width: 200px;
|
19 |
-
height: 20px;
|
20 |
background: #333;
|
21 |
-
border-radius: 10px;
|
22 |
-
overflow: hidden;
|
23 |
}
|
24 |
-
#
|
25 |
-
|
26 |
-
height: 100%;
|
27 |
-
background: #2ecc71;
|
28 |
-
transition: width 0.2s;
|
29 |
}
|
30 |
-
#
|
31 |
position: fixed;
|
32 |
-
top:
|
33 |
-
right:
|
34 |
color: white;
|
35 |
background: rgba(0,0,0,0.7);
|
36 |
-
padding:
|
37 |
border-radius: 5px;
|
|
|
|
|
38 |
}
|
39 |
-
|
40 |
-
position: fixed;
|
41 |
-
top: 20px;
|
42 |
-
left: 50%;
|
43 |
-
transform: translateX(-50%);
|
44 |
-
color: white;
|
45 |
-
font-size: 24px;
|
46 |
-
}
|
47 |
-
#gameOver {
|
48 |
position: fixed;
|
49 |
top: 50%;
|
50 |
left: 50%;
|
51 |
transform: translate(-50%, -50%);
|
52 |
-
|
|
|
|
|
53 |
color: white;
|
54 |
-
padding: 20px;
|
55 |
-
border-radius: 10px;
|
56 |
-
text-align: center;
|
57 |
-
display: none;
|
58 |
-
}
|
59 |
-
#restart {
|
60 |
-
background: #2ecc71;
|
61 |
border: none;
|
62 |
-
color: white;
|
63 |
-
padding: 10px 20px;
|
64 |
border-radius: 5px;
|
65 |
cursor: pointer;
|
66 |
-
|
|
|
67 |
}
|
68 |
</style>
|
69 |
</head>
|
70 |
<body>
|
71 |
-
<
|
72 |
-
|
73 |
-
<
|
74 |
-
|
75 |
-
|
76 |
-
</div>
|
77 |
-
<div id="round">Round 1</div>
|
78 |
-
<div id="controls">
|
79 |
-
WASD - Move<br>
|
80 |
-
Mouse - Aim<br>
|
81 |
-
Space - Shoot
|
82 |
-
</div>
|
83 |
-
<div id="gameOver">
|
84 |
-
Game Over!<br>
|
85 |
-
<button id="restart" onclick="restartGame()">Restart Game</button>
|
86 |
</div>
|
|
|
|
|
|
|
87 |
|
88 |
<script>
|
89 |
-
const canvas = document.getElementById('
|
90 |
const ctx = canvas.getContext('2d');
|
|
|
|
|
91 |
|
92 |
canvas.width = window.innerWidth;
|
93 |
canvas.height = window.innerHeight;
|
94 |
|
95 |
-
|
96 |
-
const
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
};
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
});
|
136 |
}
|
137 |
}
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
}
|
|
|
167 |
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
188 |
if(enemy.health <= 0) {
|
189 |
-
|
190 |
-
|
191 |
-
// Drop health item
|
192 |
-
game.items.push({
|
193 |
-
x: enemy.x,
|
194 |
-
y: enemy.y,
|
195 |
-
type: 'health'
|
196 |
-
});
|
197 |
}
|
198 |
-
|
199 |
}
|
200 |
-
|
201 |
-
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
enemy.x += enemy.direction.x * ENEMY_SPEED;
|
207 |
-
enemy.y += enemy.direction.y * ENEMY_SPEED;
|
208 |
-
|
209 |
-
// Bounce off walls
|
210 |
-
if(enemy.x < 0 || enemy.x > canvas.width) enemy.direction.x *= -1;
|
211 |
-
if(enemy.y < 0 || enemy.y > canvas.height) enemy.direction.y *= -1;
|
212 |
-
|
213 |
-
// Shooting
|
214 |
-
if(Date.now() - enemy.lastShot > 1000) {
|
215 |
-
const angle = Math.atan2(
|
216 |
-
game.player.y - enemy.y,
|
217 |
-
game.player.x - enemy.x
|
218 |
-
);
|
219 |
-
enemy.bullets.push({
|
220 |
-
x: enemy.x,
|
221 |
-
y: enemy.y,
|
222 |
-
angle: angle
|
223 |
-
});
|
224 |
-
enemy.lastShot = Date.now();
|
225 |
-
}
|
226 |
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
bullet.x += Math.cos(bullet.angle) * BULLET_SPEED/2;
|
231 |
-
bullet.y += Math.sin(bullet.angle) * BULLET_SPEED/2;
|
232 |
-
|
233 |
-
// Check player hit
|
234 |
-
const dx = bullet.x - game.player.x;
|
235 |
-
const dy = bullet.y - game.player.y;
|
236 |
-
if(Math.sqrt(dx*dx + dy*dy) < PLAYER_SIZE/2) {
|
237 |
-
game.player.health -= 50;
|
238 |
-
enemy.bullets.splice(i, 1);
|
239 |
-
document.getElementById('health').style.width =
|
240 |
-
(game.player.health/game.player.maxHealth * 100) + '%';
|
241 |
-
|
242 |
-
if(game.player.health <= 0) {
|
243 |
-
document.getElementById('gameOver').style.display = 'block';
|
244 |
-
}
|
245 |
-
}
|
246 |
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
|
|
|
|
253 |
});
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
if(Math.sqrt(dx*dx + dy*dy) < PLAYER_SIZE) {
|
261 |
-
game.player.health = Math.min(
|
262 |
-
game.player.maxHealth,
|
263 |
-
game.player.health + 200
|
264 |
-
);
|
265 |
-
document.getElementById('health').style.width =
|
266 |
-
(game.player.health/game.player.maxHealth * 100) + '%';
|
267 |
-
game.items.splice(i, 1);
|
268 |
}
|
269 |
-
|
|
|
270 |
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
document.getElementById('health').style.width = '100%';
|
278 |
-
game.player.bulletsPerShot = 1; // Reset bullets per shot
|
279 |
-
spawnEnemies();
|
280 |
}
|
281 |
}
|
282 |
}
|
283 |
|
284 |
-
function
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
|
|
|
|
|
|
287 |
// Draw player
|
288 |
-
ctx.
|
289 |
-
ctx.
|
290 |
-
ctx.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
-
// Draw
|
293 |
ctx.fillStyle = 'yellow';
|
294 |
-
|
295 |
ctx.beginPath();
|
296 |
ctx.arc(bullet.x, bullet.y, 3, 0, Math.PI * 2);
|
297 |
ctx.fill();
|
298 |
});
|
299 |
|
300 |
-
// Draw enemies
|
301 |
-
game.enemies.forEach(enemy => {
|
302 |
-
// Enemy
|
303 |
-
ctx.fillStyle = 'white';
|
304 |
-
ctx.fillText('🐱', enemy.x - 15, enemy.y + 15);
|
305 |
-
|
306 |
-
// Health bar
|
307 |
-
ctx.fillStyle = '#600';
|
308 |
-
ctx.fillRect(enemy.x - 25, enemy.y - 30, 50, 5);
|
309 |
-
ctx.fillStyle = '#f00';
|
310 |
-
ctx.fillRect(enemy.x - 25, enemy.y - 30,
|
311 |
-
(enemy.health/enemy.maxHealth) * 50, 5);
|
312 |
-
|
313 |
-
// Enemy bullets
|
314 |
-
ctx.fillStyle = 'red';
|
315 |
-
enemy.bullets.forEach(bullet => {
|
316 |
-
ctx.beginPath();
|
317 |
-
ctx.arc(bullet.x, bullet.y, 3, 0, Math.PI * 2);
|
318 |
-
ctx.fill();
|
319 |
-
});
|
320 |
-
});
|
321 |
-
|
322 |
// Draw items
|
323 |
ctx.fillStyle = 'green';
|
324 |
-
|
325 |
-
ctx.
|
|
|
|
|
326 |
});
|
|
|
|
|
|
|
|
|
|
|
327 |
}
|
328 |
|
329 |
function gameLoop() {
|
330 |
-
|
331 |
-
|
332 |
-
draw();
|
333 |
-
}
|
334 |
requestAnimationFrame(gameLoop);
|
335 |
}
|
336 |
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
y: canvas.height/2,
|
342 |
-
speed: 5,
|
343 |
-
health: 1000,
|
344 |
-
maxHealth: 1000,
|
345 |
-
bullets: [],
|
346 |
-
bulletsPerShot: 1
|
347 |
-
},
|
348 |
-
enemies: [],
|
349 |
-
items: [],
|
350 |
-
round: 1,
|
351 |
-
keys: game.keys,
|
352 |
-
mouse: game.mouse,
|
353 |
-
lastShot: 0,
|
354 |
-
shooting: false
|
355 |
-
};
|
356 |
-
document.getElementById('health').style.width = '100%';
|
357 |
-
document.getElementById('round').textContent = 'Round 1';
|
358 |
-
document.getElementById('gameOver').style.display = 'none';
|
359 |
-
spawnEnemies();
|
360 |
-
}
|
361 |
-
|
362 |
-
// Event listeners
|
363 |
-
window.addEventListener('keydown', e => game.keys[e.key] = true);
|
364 |
-
window.addEventListener('keyup', e => game.keys[e.key] = false);
|
365 |
-
window.addEventListener('mousemove', e => {
|
366 |
-
game.mouse.x = e.clientX;
|
367 |
-
game.mouse.y = e.clientY;
|
368 |
});
|
369 |
-
|
370 |
-
|
|
|
|
|
|
|
|
|
371 |
});
|
372 |
-
|
373 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
});
|
375 |
|
376 |
-
|
377 |
-
|
378 |
-
|
|
|
379 |
</script>
|
380 |
</body>
|
381 |
-
</html
|
|
|
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 |
}
|
11 |
+
#gameCanvas {
|
12 |
+
background-repeat: repeat;
|
|
|
|
|
|
|
13 |
}
|
14 |
+
#instructions {
|
15 |
position: fixed;
|
16 |
+
top: 10px;
|
17 |
+
right: 10px;
|
18 |
color: white;
|
19 |
background: rgba(0,0,0,0.7);
|
20 |
+
padding: 10px;
|
21 |
border-radius: 5px;
|
22 |
+
font-family: Arial;
|
23 |
+
z-index: 1000;
|
24 |
}
|
25 |
+
.button {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
position: fixed;
|
27 |
top: 50%;
|
28 |
left: 50%;
|
29 |
transform: translate(-50%, -50%);
|
30 |
+
padding: 20px 40px;
|
31 |
+
font-size: 24px;
|
32 |
+
background: #4CAF50;
|
33 |
color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
border: none;
|
|
|
|
|
35 |
border-radius: 5px;
|
36 |
cursor: pointer;
|
37 |
+
display: none;
|
38 |
+
z-index: 1000;
|
39 |
}
|
40 |
</style>
|
41 |
</head>
|
42 |
<body>
|
43 |
+
<div id="instructions">
|
44 |
+
Controls:<br>
|
45 |
+
WASD - Move tank<br>
|
46 |
+
Mouse - Aim turret<br>
|
47 |
+
Space - Fire
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
</div>
|
49 |
+
<button id="nextRound" class="button">Next Round</button>
|
50 |
+
<button id="restart" class="button">Restart Game</button>
|
51 |
+
<canvas id="gameCanvas"></canvas>
|
52 |
|
53 |
<script>
|
54 |
+
const canvas = document.getElementById('gameCanvas');
|
55 |
const ctx = canvas.getContext('2d');
|
56 |
+
const nextRoundBtn = document.getElementById('nextRound');
|
57 |
+
const restartBtn = document.getElementById('restart');
|
58 |
|
59 |
canvas.width = window.innerWidth;
|
60 |
canvas.height = window.innerHeight;
|
61 |
|
62 |
+
// Load images
|
63 |
+
const fieldImg = new Image();
|
64 |
+
fieldImg.src = 'field.png';
|
65 |
+
|
66 |
+
const bodyImg = new Image();
|
67 |
+
bodyImg.src = 'body.png';
|
68 |
+
|
69 |
+
const turretImg = new Image();
|
70 |
+
turretImg.src = 'turret.png';
|
71 |
+
|
72 |
+
const enemyImg = new Image();
|
73 |
+
enemyImg.src = 'enemy.png';
|
74 |
+
|
75 |
+
// Audio
|
76 |
+
const playerFireSound = new Audio('fire.mp3');
|
77 |
+
const enemyFireSound = new Audio('fire2.mp3');
|
78 |
+
|
79 |
+
// Game state
|
80 |
+
let currentRound = 1;
|
81 |
+
let gameOver = false;
|
82 |
+
let enemies = [];
|
83 |
+
let bullets = [];
|
84 |
+
let playerBullets = [];
|
85 |
+
let items = [];
|
86 |
+
|
87 |
+
// Tank properties
|
88 |
+
const player = {
|
89 |
+
x: canvas.width/2,
|
90 |
+
y: canvas.height/2,
|
91 |
+
speed: 5,
|
92 |
+
angle: 0,
|
93 |
+
turretAngle: 0,
|
94 |
+
width: 60,
|
95 |
+
height: 80,
|
96 |
+
health: 1000,
|
97 |
+
maxHealth: 1000
|
98 |
};
|
99 |
|
100 |
+
class Enemy {
|
101 |
+
constructor() {
|
102 |
+
this.x = Math.random() * canvas.width;
|
103 |
+
this.y = Math.random() * canvas.height;
|
104 |
+
this.health = 1000;
|
105 |
+
this.maxHealth = 1000;
|
106 |
+
this.speed = 2;
|
107 |
+
this.lastShot = 0;
|
108 |
+
this.shootInterval = 2000;
|
109 |
+
this.angle = 0;
|
110 |
+
}
|
111 |
+
|
112 |
+
update() {
|
113 |
+
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
|
114 |
+
this.x += Math.cos(this.angle) * this.speed * 0.5;
|
115 |
+
this.y += Math.sin(this.angle) * this.speed * 0.5;
|
116 |
+
|
117 |
+
this.x = Math.max(0, Math.min(canvas.width, this.x));
|
118 |
+
this.y = Math.max(0, Math.min(canvas.height, this.y));
|
119 |
+
|
120 |
+
const now = Date.now();
|
121 |
+
if (now - this.lastShot > this.shootInterval) {
|
122 |
+
this.shoot();
|
123 |
+
this.lastShot = now;
|
124 |
+
}
|
125 |
+
}
|
126 |
+
|
127 |
+
shoot() {
|
128 |
+
enemyFireSound.currentTime = 0;
|
129 |
+
enemyFireSound.play();
|
130 |
+
bullets.push({
|
131 |
+
x: this.x,
|
132 |
+
y: this.y,
|
133 |
+
angle: this.angle,
|
134 |
+
speed: 5,
|
135 |
+
isEnemy: true
|
136 |
});
|
137 |
}
|
138 |
}
|
139 |
|
140 |
+
// Draw background pattern
|
141 |
+
function drawBackground() {
|
142 |
+
const pattern = ctx.createPattern(fieldImg, 'repeat');
|
143 |
+
ctx.fillStyle = pattern;
|
144 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
145 |
+
}
|
146 |
+
|
147 |
+
// Game controls
|
148 |
+
const keys = {};
|
149 |
+
let mouseX = 0;
|
150 |
+
let mouseY = 0;
|
151 |
+
let lastShot = 0;
|
152 |
+
const shootInterval = 1000;
|
153 |
+
|
154 |
+
document.addEventListener('keydown', (e) => {
|
155 |
+
keys[e.key] = true;
|
156 |
+
});
|
157 |
+
|
158 |
+
document.addEventListener('keyup', (e) => {
|
159 |
+
keys[e.key] = false;
|
160 |
+
});
|
161 |
+
|
162 |
+
canvas.addEventListener('mousemove', (e) => {
|
163 |
+
mouseX = e.clientX;
|
164 |
+
mouseY = e.clientY;
|
165 |
+
});
|
166 |
+
|
167 |
+
function fireBullet() {
|
168 |
+
const now = Date.now();
|
169 |
+
if (now - lastShot > shootInterval && keys[' ']) {
|
170 |
+
playerFireSound.currentTime = 0;
|
171 |
+
playerFireSound.play();
|
172 |
+
playerBullets.push({
|
173 |
+
x: player.x,
|
174 |
+
y: player.y,
|
175 |
+
angle: player.turretAngle,
|
176 |
+
speed: 10
|
177 |
+
});
|
178 |
+
lastShot = now;
|
179 |
}
|
180 |
+
}
|
181 |
|
182 |
+
function initRound() {
|
183 |
+
enemies = [];
|
184 |
+
for(let i = 0; i < 5 * currentRound; i++) {
|
185 |
+
enemies.push(new Enemy());
|
186 |
+
}
|
187 |
+
player.health = player.maxHealth;
|
188 |
+
bullets = [];
|
189 |
+
playerBullets = [];
|
190 |
+
items = [];
|
191 |
+
}
|
192 |
|
193 |
+
function spawnHealthItem(x, y) {
|
194 |
+
items.push({
|
195 |
+
x: x,
|
196 |
+
y: y,
|
197 |
+
width: 20,
|
198 |
+
height: 20
|
199 |
+
});
|
200 |
+
}
|
201 |
+
|
202 |
+
function updateGame() {
|
203 |
+
if(gameOver) return;
|
204 |
+
|
205 |
+
if(keys['w']) player.y = Math.max(player.y - player.speed, player.height/2);
|
206 |
+
if(keys['s']) player.y = Math.min(player.y + player.speed, canvas.height - player.height/2);
|
207 |
+
if(keys['a']) player.x = Math.max(player.x - player.speed, player.width/2);
|
208 |
+
if(keys['d']) player.x = Math.min(player.x + player.speed, canvas.width - player.width/2);
|
209 |
+
|
210 |
+
player.turretAngle = Math.atan2(mouseY - player.y, mouseX - player.x);
|
211 |
+
|
212 |
+
if(keys[' ']) fireBullet();
|
213 |
+
|
214 |
+
enemies.forEach(enemy => enemy.update());
|
215 |
|
216 |
+
// Update bullets and collision detection
|
217 |
+
playerBullets = playerBullets.filter(bullet => {
|
218 |
+
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
219 |
+
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
220 |
+
|
221 |
+
enemies = enemies.filter(enemy => {
|
222 |
+
const dist = Math.hypot(bullet.x - enemy.x, bullet.y - enemy.y);
|
223 |
+
if(dist < 30) {
|
224 |
+
enemy.health -= enemy.maxHealth / 2;
|
225 |
if(enemy.health <= 0) {
|
226 |
+
spawnHealthItem(enemy.x, enemy.y);
|
227 |
+
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
}
|
229 |
+
return true;
|
230 |
}
|
231 |
+
return true;
|
232 |
+
});
|
233 |
|
234 |
+
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
235 |
+
bullet.y >= 0 && bullet.y <= canvas.height;
|
236 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
|
238 |
+
bullets = bullets.filter(bullet => {
|
239 |
+
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
240 |
+
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
const dist = Math.hypot(bullet.x - player.x, bullet.y - player.y);
|
243 |
+
if(dist < 30) {
|
244 |
+
player.health -= 100;
|
245 |
+
if(player.health <= 0) {
|
246 |
+
gameOver = true;
|
247 |
+
restartBtn.style.display = 'block';
|
248 |
}
|
249 |
+
return false;
|
250 |
}
|
251 |
+
|
252 |
+
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
253 |
+
bullet.y >= 0 && bullet.y <= canvas.height;
|
254 |
});
|
255 |
|
256 |
+
items = items.filter(item => {
|
257 |
+
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
258 |
+
if(dist < 30) {
|
259 |
+
player.health = Math.min(player.health + 200, player.maxHealth);
|
260 |
+
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
}
|
262 |
+
return true;
|
263 |
+
});
|
264 |
|
265 |
+
if(enemies.length === 0) {
|
266 |
+
if(currentRound < 10) {
|
267 |
+
nextRoundBtn.style.display = 'block';
|
268 |
+
} else {
|
269 |
+
gameOver = true;
|
270 |
+
restartBtn.style.display = 'block';
|
|
|
|
|
|
|
271 |
}
|
272 |
}
|
273 |
}
|
274 |
|
275 |
+
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
276 |
+
const barX = x - width/2;
|
277 |
+
const barY = y - 50;
|
278 |
+
ctx.fillStyle = '#333';
|
279 |
+
ctx.fillRect(barX, barY, width, height);
|
280 |
+
ctx.fillStyle = color;
|
281 |
+
ctx.fillRect(barX, barY, width * (health/maxHealth), height);
|
282 |
+
}
|
283 |
|
284 |
+
function drawGame() {
|
285 |
+
drawBackground();
|
286 |
+
|
287 |
// Draw player
|
288 |
+
ctx.save();
|
289 |
+
ctx.translate(player.x, player.y);
|
290 |
+
ctx.drawImage(bodyImg, -player.width/2, -player.height/2, player.width, player.height);
|
291 |
+
ctx.rotate(player.turretAngle);
|
292 |
+
ctx.drawImage(turretImg, -player.width/2, -player.height/2, player.width, player.height);
|
293 |
+
ctx.restore();
|
294 |
+
|
295 |
+
// Draw player health bar
|
296 |
+
drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
|
297 |
+
|
298 |
+
// Draw enemies
|
299 |
+
enemies.forEach(enemy => {
|
300 |
+
ctx.save();
|
301 |
+
ctx.translate(enemy.x, enemy.y);
|
302 |
+
ctx.rotate(enemy.angle);
|
303 |
+
ctx.drawImage(enemyImg, -30, -40, 60, 80);
|
304 |
+
ctx.restore();
|
305 |
+
drawHealthBar(enemy.x, enemy.y, enemy.health, enemy.maxHealth, 60, 5, 'red');
|
306 |
+
});
|
307 |
|
308 |
+
// Draw bullets
|
309 |
ctx.fillStyle = 'yellow';
|
310 |
+
[...playerBullets, ...bullets].forEach(bullet => {
|
311 |
ctx.beginPath();
|
312 |
ctx.arc(bullet.x, bullet.y, 3, 0, Math.PI * 2);
|
313 |
ctx.fill();
|
314 |
});
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
// Draw items
|
317 |
ctx.fillStyle = 'green';
|
318 |
+
items.forEach(item => {
|
319 |
+
ctx.beginPath();
|
320 |
+
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
|
321 |
+
ctx.fill();
|
322 |
});
|
323 |
+
|
324 |
+
// Draw round number
|
325 |
+
ctx.fillStyle = 'white';
|
326 |
+
ctx.font = '24px Arial';
|
327 |
+
ctx.fillText(`Round ${currentRound}/10`, 10, 30);
|
328 |
}
|
329 |
|
330 |
function gameLoop() {
|
331 |
+
updateGame();
|
332 |
+
drawGame();
|
|
|
|
|
333 |
requestAnimationFrame(gameLoop);
|
334 |
}
|
335 |
|
336 |
+
nextRoundBtn.addEventListener('click', () => {
|
337 |
+
currentRound++;
|
338 |
+
nextRoundBtn.style.display = 'none';
|
339 |
+
initRound();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
340 |
});
|
341 |
+
|
342 |
+
restartBtn.addEventListener('click', () => {
|
343 |
+
currentRound = 1;
|
344 |
+
gameOver = false;
|
345 |
+
restartBtn.style.display = 'none';
|
346 |
+
initRound();
|
347 |
});
|
348 |
+
|
349 |
+
Promise.all([
|
350 |
+
new Promise(resolve => fieldImg.onload = resolve),
|
351 |
+
new Promise(resolve => bodyImg.onload = resolve),
|
352 |
+
new Promise(resolve => turretImg.onload = resolve),
|
353 |
+
new Promise(resolve => enemyImg.onload = resolve)
|
354 |
+
]).then(() => {
|
355 |
+
initRound();
|
356 |
+
gameLoop();
|
357 |
});
|
358 |
|
359 |
+
window.addEventListener('resize', () => {
|
360 |
+
canvas.width = window.innerWidth;
|
361 |
+
canvas.height = window.innerHeight;
|
362 |
+
});
|
363 |
</script>
|
364 |
</body>
|
365 |
+
</html>
|