File size: 13,121 Bytes
340ccdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a91bfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340ccdc
 
 
8c0bdf9
 
 
340ccdc
 
 
 
 
8a91bfb
340ccdc
 
 
 
 
 
 
 
 
8c0bdf9
340ccdc
 
 
 
 
 
 
 
 
 
 
 
8a91bfb
8c0bdf9
340ccdc
 
 
 
 
 
 
 
5d2ed11
 
 
 
 
 
 
 
 
 
 
 
 
340ccdc
 
 
 
 
 
 
 
 
 
 
 
 
 
8c0bdf9
340ccdc
 
 
 
 
 
 
 
8c0bdf9
 
 
340ccdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d2ed11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340ccdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a91bfb
340ccdc
 
 
8c0bdf9
 
 
340ccdc
 
 
8a91bfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c0bdf9
 
 
 
8a91bfb
340ccdc
 
 
716212a
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Space Invaders</title>
    <style>
        body {
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: black;
            color: white;
            font-family: Arial, sans-serif;
        }

        .game {
            position: relative;
            width: 600px;
            height: 600px;
            background-color: #111;
            overflow: hidden;
        }

        .player {
            position: absolute;
            bottom: 20px;
            left: 50%;
            width: 40px;
            height: 40px;
            background-color: white;
            transform: translateX(-50%);
        }

        .invaders {
            position: absolute;
            top: 20px;
            left: 0;
            width: 100%;
            display: flex;
            flex-wrap: wrap;
        }

        .invader {
            width: 40px;
            height: 40px;
            background-color: red;
            margin: 5px;
        }

        .bullet {
            position: absolute;
            width: 5px;
            height: 20px;
            background-color: yellow;
        }

        .scoreboard {
            position: absolute;
            top: 10px;
            left: 10px;
            font-size: 20px;
        }

        .start-menu {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        .start-menu button {
            margin: 10px;
            padding: 10px 20px;
            font-size: 16px;
        }

        .game-over-message {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            text-align: center;
            display: none;
        }

        .restart-button {
            position: absolute;
            top: 60%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            text-align: center;
            display: none;
            padding: 10px 20px;
            background-color: #333;
            color: white;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <!-- Added audio element for background music -->
    <audio id="backgroundMusic" src="Jupiter.mp3" loop></audio>

    <div class="game">
        <div class="scoreboard">Score: 0</div>
        <div class="player"></div>
        <div class="invaders"></div>
        <div class="game-over-message">Game Over, <span id="username-display"></span>! Your score is: <span id="score-display"></span></div>
        <button class="restart-button">Restart</button>
        <div class="start-menu">
            <h2>Enter Your Name</h2>
            <input type="text" id="username" placeholder="Your name">
            <h2>Select Difficulty</h2>
            <button data-speed="1">Slow</button>
            <button data-speed="2">Medium</button>
            <button data-speed="3">Fast</button>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const game = document.querySelector('.game');
            const player = document.querySelector('.player');
            const invadersContainer = document.querySelector('.invaders');
            const scoreboard = document.querySelector('.scoreboard');
            const startMenu = document.querySelector('.start-menu');
            const buttons = document.querySelectorAll('.start-menu button');
            const usernameInput = document.getElementById('username');
            const gameOverMessage = document.querySelector('.game-over-message');
            const usernameDisplay = document.getElementById('username-display');
            const scoreDisplay = document.getElementById('score-display');
            const restartButton = document.querySelector('.restart-button');
            const backgroundMusic = document.getElementById('backgroundMusic');

            const playerSpeed = 10;
            let playerPosition = 280;
            let score = 0;
            let invaderSpeed = 2;
            let invaderInterval;
            let username = '';

            // Determine if touch events are supported
            const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;

            if (isTouchDevice) {
                // Touch events for mobile devices
                game.addEventListener('touchstart', handleTouchStart);
                game.addEventListener('touchmove', handleTouchMove);
                game.addEventListener('touchend', handleTouchEnd);
            } else {
                // Keyboard events for desktop
                document.addEventListener('keydown', movePlayer);
            }

            buttons.forEach(button => {
                button.addEventListener('click', (e) => {
                    username = usernameInput.value.trim();
                    if (username === '') {
                        alert('Please enter your name.');
                        return;
                    }
                    invaderSpeed = parseInt(e.target.getAttribute('data-speed'));
                    startMenu.style.display = 'none';
                    startGame();
                });
            });

            function startGame() {
                // Adjust invader speed based on difficulty level
                if (invaderSpeed === 1) {
                    invaderSpeed = 8; // Medium level
                } else if (invaderSpeed === 3) {
                    invaderSpeed = 14; // Fast level (3 times faster)
                }
                createInvaders();
                invaderInterval = setInterval(moveInvaders, 500);
                setInterval(createInvaders, 5000); // Create new invaders every 5 seconds

                // Play background music
                backgroundMusic.play();
            }

            function createInvaders() {
                const invaderCount = 30;
                const invaderWidth = 50; // Including margin
                const maxCols = Math.floor(game.offsetWidth / invaderWidth);

                for (let i = 0; i < invaderCount; i++) {
                    const invader = document.createElement('div');
                    invader.classList.add('invader');
                    invader.style.position = 'absolute';
                    invader.style.left = (i % maxCols) * invaderWidth + 'px';
                    invader.style.top = Math.floor(i / maxCols) * 50 + 'px';
                    invadersContainer.appendChild(invader);
                }
            }

            // Handle touch events for player movement and shooting
            let touchStartX = null;
            function handleTouchStart(event) {
                const touch = event.touches[0];
                touchStartX = touch.clientX;
            }

            function handleTouchMove(event) {
                event.preventDefault();
            }

            function handleTouchEnd(event) {
                const touch = event.changedTouches[0];
                const touchEndX = touch.clientX;

                // Determine swipe direction or tap for shooting
                if (touchEndX < touchStartX) {
                    // Swipe left
                    movePlayer({ key: 'ArrowLeft' });
                } else if (touchEndX > touchStartX) {
                    // Swipe right
                    movePlayer({ key: 'ArrowRight' });
                } else {
                    // Tap for shooting
                    shoot();
                }
            }

            // Move player based on keyboard events
            function movePlayer(e) {
                switch (e.key) {
                    case 'ArrowLeft':
                        if (playerPosition > 0) {
                            playerPosition -= playerSpeed;
                            player.style.left = playerPosition + 'px';
                        }
                        break;
                    case 'ArrowRight':
                        if (playerPosition < game.offsetWidth - player.offsetWidth) {
                            playerPosition += playerSpeed;
                            player.style.left = playerPosition + 'px';
                        }
                        break;
                    case ' ':
                        shoot();
                        break;
                }
            }

            // Shoot bullet
            function shoot() {
                const bullet = document.createElement('div');
                bullet.classList.add('bullet');
                bullet.style.left = playerPosition + 17.5 + 'px'; // Center bullet to player
                bullet.style.bottom = '60px'; // Starting position above player
                game.appendChild(bullet);
                moveBullet(bullet);
            }

            // Move bullet
            function moveBullet(bullet) {
                const bulletInterval = setInterval(() => {
                    const bulletBottom = parseInt(bullet.style.bottom.replace('px', ''));
                    if (bulletBottom >= game.offsetHeight) {
                        clearInterval(bulletInterval);
                        bullet.remove();
                    } else {
                        bullet.style.bottom = bulletBottom + 5 + 'px';
                    }
                    checkBulletCollision(bullet, bulletInterval);
                }, 20);
            }

            // Check bullet collision with invaders
            function checkBulletCollision(bullet, bulletInterval) {
                const bulletRect = bullet.getBoundingClientRect();
                document.querySelectorAll('.invader').forEach(invader => {
                    const invaderRect = invader.getBoundingClientRect();
                    if (bulletRect.top <= invaderRect.bottom && bulletRect.bottom >= invaderRect.top &&
                        bulletRect.left <= invaderRect.right && bulletRect.right >= invaderRect.left) {
                        bullet.remove();
                        invader.remove();
                        clearInterval(bulletInterval);
                        score++;
                        scoreboard.textContent = `Score: ${score}`;
                    }
                });
            }

            // Move invaders
            function moveInvaders() {
                const invaders = Array.from(document.querySelectorAll('.invader'));
                invaders.forEach(invader => {
                    const currentTop = parseInt(invader.style.top.replace('px', ''));
                    invader.style.top = currentTop + invaderSpeed + 'px';
                });

                // Check if invaders reach the bottom
                invaders.forEach(invader => {
                    const invaderBottom = parseInt(invader.style.top.replace('px', '')) + invader.offsetHeight;
                    if (invaderBottom >= game.offsetHeight) {
                        gameOverMessage.style.display = 'block';
                        restartButton.style.display = 'block';
                        usernameDisplay.textContent = username;
                        scoreDisplay.textContent = score;
                        clearInterval(invaderInterval);

                        // Stop background music on game over
                        backgroundMusic.pause();
                    }
                });
            }

            // Add event listener to restart button
            restartButton.addEventListener('click', () => {
                restartGame();
            });

            // Function to restart the game
            function restartGame() {
                // Reset game variables
                playerPosition = 280;
                score = 0;
                invaderSpeed = 2;
                username = '';

                // Hide game over message and restart button
                gameOverMessage.style.display = 'none';
                restartButton.style.display = 'none';

                // Reset player position
                player.style.left = playerPosition + 'px';

                // Reset scoreboard
                scoreboard.textContent = `Score: ${score}`;

                // Remove all invaders and bullets
                document.querySelectorAll('.invader').forEach(invader => {
                    invader.remove();
                });
                document.querySelectorAll('.bullet').forEach(bullet => {
                    bullet.remove();
                });

                // Show start menu
                startMenu.style.display = 'block';

                // Restart background music
                backgroundMusic.currentTime = 0;
                backgroundMusic.play();
            }
        });
    </script>
</body>
</html>