File size: 13,824 Bytes
7790c5f
 
 
 
 
d9c436b
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c436b
 
7790c5f
 
 
 
 
 
 
 
 
4dada42
 
7790c5f
 
 
 
 
4dada42
 
7790c5f
 
d9c436b
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c436b
 
 
 
 
 
 
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c436b
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c436b
 
 
 
 
7790c5f
d9c436b
 
 
7790c5f
 
 
 
 
 
 
 
4dada42
 
 
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dada42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c436b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7790c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dada42
 
 
 
7790c5f
 
 
 
 
 
 
d9c436b
 
 
 
7790c5f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tank Game with Random Spawns</title>
    <style>
        body { margin: 0; overflow: hidden; font-family: Arial, sans-serif; }
        #gameArea { width: 100vw; height: 100vh; background-color: #f0f0f0; }
        #score { position: absolute; top: 10px; left: 10px; font-size: 24px; font-weight: bold; }
        #level { position: absolute; top: 10px; right: 10px; font-size: 24px; font-weight: bold; }
        #nextLevel { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 48px; font-weight: bold; display: none; }
    </style>
</head>
<body>
    <div id="score">Score: 0</div>
    <div id="level">Level: 1</div>
    <div id="nextLevel">Next Level</div>
    <svg id="gameArea"></svg>
    <script>
        const svg = document.getElementById('gameArea');
        const scoreElement = document.getElementById('score');
        const levelElement = document.getElementById('level');
        const nextLevelElement = document.getElementById('nextLevel');
        const svgNS = "http://www.w3.org/2000/svg";

        let GAME_WIDTH = window.innerWidth;
        let GAME_HEIGHT = window.innerHeight;
        const TANK_SIZE = 30;
        const BULLET_SIZE = 5;
        const TANK_SPEED = 2;
        const AI_TANK_SPEED = 1.5;
        const BULLET_SPEED = 5;
        const MAX_HIT_POINTS = 20;
        const MAX_TANKS = 5;
        const MAX_TANKS_ON_SCREEN = 50;
        const SPAWN_DELAY = 5000; // 5 seconds in milliseconds
        const SHOCKWAVE_RADIUS = 150;
        const SHOCKWAVE_DURATION = 1000; // 1 second in milliseconds

        let score = 0;
        let level = 1;
        let spawnQueue = [];
        let lastSpawnTime = 0;
        let shockwaveActive = false;
        let shockwaveStartTime = 0;

        class Tank {
            constructor(color, isPlayer = false) {
                this.color = color;
                this.isPlayer = isPlayer;
                this.hitPoints = MAX_HIT_POINTS;
                this.element = document.createElementNS(svgNS, "g");
                this.tankBody = document.createElementNS(svgNS, "polygon");
                this.shield = document.createElementNS(svgNS, "circle");
                this.hitPointsText = document.createElementNS(svgNS, "text");
                
                this.tankBody.setAttribute("fill", color);
                this.shield.setAttribute("fill", "none");
                this.shield.setAttribute("stroke", color);
                this.shield.setAttribute("stroke-width", this.hitPoints);
                this.shield.setAttribute("r", TANK_SIZE + 10);
                
                this.hitPointsText.setAttribute("text-anchor", "middle");
                this.hitPointsText.setAttribute("fill", "black");
                this.hitPointsText.setAttribute("font-size", "12px");
                
                this.element.appendChild(this.shield);
                this.element.appendChild(this.tankBody);
                this.element.appendChild(this.hitPointsText);
                svg.appendChild(this.element);
                
                this.velocity = { x: 0, y: 0 };
                this.isAlive = true;
                this.respawn();
            }

            respawn() {
                this.x = Math.random() * (GAME_WIDTH - 2 * TANK_SIZE) + TANK_SIZE;
                this.y = Math.random() * (GAME_HEIGHT - 2 * TANK_SIZE) + TANK_SIZE;
                this.angle = Math.random() * 2 * Math.PI;
            }

            update(playerX, playerY) {
                if (!this.isAlive) return;

                if (!this.isPlayer) {
                    const dx = playerX - this.x;
                    const dy = playerY - this.y;
                    this.angle = Math.atan2(dy, dx);
                    this.x += Math.cos(this.angle) * AI_TANK_SPEED;
                    this.y += Math.sin(this.angle) * AI_TANK_SPEED;
                } else {
                    this.x += this.velocity.x;
                    this.y += this.velocity.y;
                }

                // Bounce off walls
                if (this.x < TANK_SIZE || this.x > GAME_WIDTH - TANK_SIZE) {
                    this.velocity.x *= -1;
                    this.x = Math.max(TANK_SIZE, Math.min(GAME_WIDTH - TANK_SIZE, this.x));
                }
                if (this.y < TANK_SIZE || this.y > GAME_HEIGHT - TANK_SIZE) {
                    this.velocity.y *= -1;
                    this.y = Math.max(TANK_SIZE, Math.min(GAME_HEIGHT - TANK_SIZE, this.y));
                }

                const points = [
                    [TANK_SIZE, 0],
                    [-TANK_SIZE / 2, -TANK_SIZE / 2],
                    [-TANK_SIZE / 2, TANK_SIZE / 2]
                ].map(([x, y]) => {
                    const rotatedX = x * Math.cos(this.angle) - y * Math.sin(this.angle);
                    const rotatedY = x * Math.sin(this.angle) + y * Math.cos(this.angle);
                    return `${rotatedX},${rotatedY}`;
                }).join(" ");

                this.tankBody.setAttribute("points", points);
                this.shield.setAttribute("cx", 0);
                this.shield.setAttribute("cy", 0);
                this.shield.setAttribute("stroke-width", this.hitPoints);
                this.hitPointsText.textContent = this.hitPoints;
                this.hitPointsText.setAttribute("x", 0);
                this.hitPointsText.setAttribute("y", -TANK_SIZE - 5);
                
                this.element.setAttribute("transform", `translate(${this.x},${this.y}) rotate(${this.angle * 180 / Math.PI})`);
            }

            shoot() {
                return new Bullet(this.x + Math.cos(this.angle) * TANK_SIZE, 
                                  this.y + Math.sin(this.angle) * TANK_SIZE, 
                                  this.angle, 
                                  this.color,
                                  this.isPlayer);
            }

            takeDamage(damage) {
                this.hitPoints -= damage;
                if (this.hitPoints <= 0) {
                    this.explode();
                }
            }

            explode() {
                if (!this.isAlive) return;
                this.isAlive = false;
                this.element.remove();
                updateScore(1);
            }
        }

        class Bullet {
            constructor(x, y, angle, color, isPlayerBullet) {
                this.x = x;
                this.y = y;
                this.angle = angle;
                this.color = color;
                this.isPlayerBullet = isPlayerBullet;
                this.element = document.createElementNS(svgNS, "circle");
                this.element.setAttribute("r", BULLET_SIZE);
                this.element.setAttribute("fill", color);
                svg.appendChild(this.element);
            }

            update() {
                this.x += Math.cos(this.angle) * BULLET_SPEED;
                this.y += Math.sin(this.angle) * BULLET_SPEED;
                this.element.setAttribute("cx", this.x);
                this.element.setAttribute("cy", this.y);

                return this.x > 0 && this.x < GAME_WIDTH && this.y > 0 && this.y < GAME_HEIGHT;
            }

            remove() {
                this.element.remove();
            }
        }

        const player = new Tank("blue", true);
        let aiTanks = [];

        function queueEnemySpawn() {
            const tanksToSpawn = MAX_TANKS - aiTanks.length;
            for (let i = 0; i < tanksToSpawn; i++) {
                spawnQueue.push({
                    color: `hsl(${Math.random() * 360}, 100%, 50%)`
                });
            }
        }

        function spawnEnemies() {
            const currentTime = Date.now();
            if (currentTime - lastSpawnTime > SPAWN_DELAY && spawnQueue.length > 0 && aiTanks.length < MAX_TANKS_ON_SCREEN) {
                const spawnData = spawnQueue.shift();
                let newTank = new Tank(spawnData.color);
                
                // Ensure the new tank doesn't overlap with existing tanks
                while (aiTanks.some(tank => distance(newTank, tank) < 3 * TANK_SIZE)) {
                    newTank.respawn();
                }
                
                aiTanks.push(newTank);
                lastSpawnTime = currentTime;
            }
        }

        let bullets = [];
        let keys = {};

        document.addEventListener('keydown', (event) => {
            keys[event.key.toLowerCase()] = true;
            if (event.code === 'Space' && !shockwaveActive) {
                activateShockwave();
            }
        });

        document.addEventListener('keyup', (event) => {
            keys[event.key.toLowerCase()] = false;
        });

        svg.addEventListener('mousemove', (event) => {
            const rect = svg.getBoundingClientRect();
            const mouseX = event.clientX - rect.left;
            const mouseY = event.clientY - rect.top;
            player.angle = Math.atan2(mouseY - player.y, mouseX - player.x);
        });

        svg.addEventListener('click', () => {
            bullets.push(player.shoot());
        });

        function updatePlayerPosition() {
            let dx = 0, dy = 0;
            if (keys['w']) dy -= TANK_SPEED;
            if (keys['s']) dy += TANK_SPEED;
            if (keys['a']) dx -= TANK_SPEED;
            if (keys['d']) dx += TANK_SPEED;

            // Normalize diagonal movement
            if (dx !== 0 && dy !== 0) {
                dx /= Math.sqrt(2);
                dy /= Math.sqrt(2);
            }

            player.velocity = { x: dx, y: dy };
        }

        function checkCollisions() {
            bullets.forEach((bullet, bulletIndex) => {
                if (bullet.isPlayerBullet) {
                    aiTanks.forEach((tank, tankIndex) => {
                        if (tank.isAlive && distance(bullet, tank) < TANK_SIZE) {
                            bullet.remove();
                            bullets.splice(bulletIndex, 1);
                            tank.takeDamage(1);
                        }
                    });
                }
            });
        }

        function distance(obj1, obj2) {
            return Math.sqrt((obj1.x - obj2.x)**2 + (obj1.y - obj2.y)**2);
        }

        function updateScore(points) {
            score += points;
            scoreElement.textContent = `Score: ${score}`;
        }

        function updateLevel() {
            level++;
            levelElement.textContent = `Level: ${level}`;
            nextLevelElement.style.display = 'block';
            setTimeout(() => {
                nextLevelElement.style.display = 'none';
                queueEnemySpawn();
            }, 2000);
        }

        function activateShockwave() {
            shockwaveActive = true;
            shockwaveStartTime = Date.now();

            const shockwave = document.createElementNS(svgNS, "circle");
            shockwave.setAttribute("cx", player.x);
            shockwave.setAttribute("cy", player.y);
            shockwave.setAttribute("r", 0);
            shockwave.setAttribute("fill", "none");
            shockwave.setAttribute("stroke", "rgba(0, 255, 255, 0.5)");
            shockwave.setAttribute("stroke-width", "5");
            svg.appendChild(shockwave);

            const expandShockwave = setInterval(() => {
                const currentRadius = parseFloat(shockwave.getAttribute("r"));
                if (currentRadius < SHOCKWAVE_RADIUS) {
                    shockwave.setAttribute("r", currentRadius + 5);
                } else {
                    clearInterval(expandShockwave);
                    setTimeout(() => {
                        shockwave.remove();
                    }, 200);
                }
            }, 20);

            // Check for enemy tanks within shockwave radius
            aiTanks.forEach(tank => {
                if (distance(player, tank) <= SHOCKWAVE_RADIUS) {
                    tank.explode();
                    updateScore(10);
                }
            });
        }

        function handleResize() {
            GAME_WIDTH = window.innerWidth;
            GAME_HEIGHT = window.innerHeight;
            svg.setAttribute("width", GAME_WIDTH);
            svg.setAttribute("height", GAME_HEIGHT);

            // Reposition all tanks
            [player, ...aiTanks].forEach(tank => {
                tank.x = Math.min(Math.max(tank.x, TANK_SIZE), GAME_WIDTH - TANK_SIZE);
                tank.y = Math.min(Math.max(tank.y, TANK_SIZE), GAME_HEIGHT - TANK_SIZE);
            });
        }

        window.addEventListener('resize', handleResize);

        function gameLoop() {
            updatePlayerPosition();
            player.update();
            aiTanks = aiTanks.filter(tank => tank.isAlive);
            aiTanks.forEach(tank => {
                tank.update(player.x, player.y);
                if (Math.random() < 0.02) {
                    bullets.push(tank.shoot());
                }
            });

            bullets = bullets.filter(bullet => bullet.update());
            checkCollisions();
            spawnEnemies();

            if (shockwaveActive && Date.now() - shockwaveStartTime > SHOCKWAVE_DURATION) {
                shockwaveActive = false;
            }

            if (aiTanks.length === 0 && spawnQueue.length === 0) {
                updateLevel();
            }

            requestAnimationFrame(gameLoop);
        }

        // Set initial SVG size
        svg.setAttribute("width", GAME_WIDTH);
        svg.setAttribute("height", GAME_HEIGHT);

        queueEnemySpawn(); // Initial spawn queue
        gameLoop();
    </script>
</body>
</html>