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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</html>
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Space Invaders</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
display: flex;
|
11 |
+
justify-content: center;
|
12 |
+
align-items: center;
|
13 |
+
height: 100vh;
|
14 |
+
background-color: black;
|
15 |
+
color: white;
|
16 |
+
font-family: Arial, sans-serif;
|
17 |
+
}
|
18 |
+
|
19 |
+
.game {
|
20 |
+
position: relative;
|
21 |
+
width: 600px;
|
22 |
+
height: 600px;
|
23 |
+
background-color: #111;
|
24 |
+
overflow: hidden;
|
25 |
+
}
|
26 |
+
|
27 |
+
.player {
|
28 |
+
position: absolute;
|
29 |
+
bottom: 20px;
|
30 |
+
left: 50%;
|
31 |
+
width: 40px;
|
32 |
+
height: 40px;
|
33 |
+
background-color: white;
|
34 |
+
transform: translateX(-50%);
|
35 |
+
}
|
36 |
+
|
37 |
+
.invaders {
|
38 |
+
position: absolute;
|
39 |
+
top: 20px;
|
40 |
+
left: 0;
|
41 |
+
width: 100%;
|
42 |
+
display: flex;
|
43 |
+
flex-wrap: wrap;
|
44 |
+
}
|
45 |
+
|
46 |
+
.invader {
|
47 |
+
width: 40px;
|
48 |
+
height: 40px;
|
49 |
+
background-color: red;
|
50 |
+
margin: 5px;
|
51 |
+
}
|
52 |
+
|
53 |
+
.bullet {
|
54 |
+
position: absolute;
|
55 |
+
width: 5px;
|
56 |
+
height: 20px;
|
57 |
+
background-color: yellow;
|
58 |
+
}
|
59 |
+
|
60 |
+
.scoreboard {
|
61 |
+
position: absolute;
|
62 |
+
top: 10px;
|
63 |
+
left: 10px;
|
64 |
+
font-size: 20px;
|
65 |
+
}
|
66 |
+
|
67 |
+
.start-menu {
|
68 |
+
position: absolute;
|
69 |
+
top: 50%;
|
70 |
+
left: 50%;
|
71 |
+
transform: translate(-50%, -50%);
|
72 |
+
text-align: center;
|
73 |
+
}
|
74 |
+
|
75 |
+
.start-menu button {
|
76 |
+
margin: 10px;
|
77 |
+
padding: 10px 20px;
|
78 |
+
font-size: 16px;
|
79 |
+
}
|
80 |
+
|
81 |
+
.game-over-message {
|
82 |
+
position: absolute;
|
83 |
+
top: 50%;
|
84 |
+
left: 50%;
|
85 |
+
transform: translate(-50%, -50%);
|
86 |
+
font-size: 24px;
|
87 |
+
text-align: center;
|
88 |
+
display: none;
|
89 |
+
}
|
90 |
+
</style>
|
91 |
+
</head>
|
92 |
+
<body>
|
93 |
+
<div class="game">
|
94 |
+
<div class="scoreboard">Score: 0</div>
|
95 |
+
<div class="player"></div>
|
96 |
+
<div class="invaders"></div>
|
97 |
+
<div class="game-over-message">Game Over, <span id="username-display"></span>! Your score is: <span id="score-display"></span></div>
|
98 |
+
<div class="start-menu">
|
99 |
+
<h2>Enter Your Name</h2>
|
100 |
+
<input type="text" id="username" placeholder="Your name">
|
101 |
+
<h2>Select Difficulty</h2>
|
102 |
+
<button data-speed="1">Slow</button>
|
103 |
+
<button data-speed="2">Medium</button>
|
104 |
+
<button data-speed="3">Fast</button>
|
105 |
+
</div>
|
106 |
+
</div>
|
107 |
+
<script>
|
108 |
+
document.addEventListener('DOMContentLoaded', () => {
|
109 |
+
const game = document.querySelector('.game');
|
110 |
+
const player = document.querySelector('.player');
|
111 |
+
const invadersContainer = document.querySelector('.invaders');
|
112 |
+
const scoreboard = document.querySelector('.scoreboard');
|
113 |
+
const startMenu = document.querySelector('.start-menu');
|
114 |
+
const buttons = document.querySelectorAll('.start-menu button');
|
115 |
+
const usernameInput = document.getElementById('username');
|
116 |
+
const gameOverMessage = document.querySelector('.game-over-message');
|
117 |
+
const usernameDisplay = document.getElementById('username-display');
|
118 |
+
const scoreDisplay = document.getElementById('score-display');
|
119 |
+
|
120 |
+
const playerSpeed = 10;
|
121 |
+
let playerPosition = 280;
|
122 |
+
let score = 0;
|
123 |
+
let invaderSpeed = 2;
|
124 |
+
let invaderInterval;
|
125 |
+
let username = '';
|
126 |
+
|
127 |
+
buttons.forEach(button => {
|
128 |
+
button.addEventListener('click', (e) => {
|
129 |
+
username = usernameInput.value.trim();
|
130 |
+
if (username === '') {
|
131 |
+
alert('Please enter your name.');
|
132 |
+
return;
|
133 |
+
}
|
134 |
+
invaderSpeed = parseInt(e.target.getAttribute('data-speed'));
|
135 |
+
startMenu.style.display = 'none';
|
136 |
+
startGame();
|
137 |
+
});
|
138 |
+
});
|
139 |
+
|
140 |
+
function startGame() {
|
141 |
+
if (invaderSpeed === 1) {
|
142 |
+
invaderSpeed = 8; // Medium level
|
143 |
+
} else if (invaderSpeed === 3) {
|
144 |
+
invaderSpeed = 14; // Fast level (3 times faster)
|
145 |
+
}
|
146 |
+
createInvaders();
|
147 |
+
document.addEventListener('keydown', movePlayer);
|
148 |
+
invaderInterval = setInterval(moveInvaders, 500);
|
149 |
+
setInterval(createInvaders, 5000); // Create new invaders every 5 seconds
|
150 |
+
}
|
151 |
+
|
152 |
+
function createInvaders() {
|
153 |
+
const invaderCount = 30;
|
154 |
+
const invaderWidth = 50; // Including margin
|
155 |
+
const maxCols = Math.floor(game.offsetWidth / invaderWidth);
|
156 |
+
|
157 |
+
for (let i = 0; i < invaderCount; i++) {
|
158 |
+
const invader = document.createElement('div');
|
159 |
+
invader.classList.add('invader');
|
160 |
+
invader.style.position = 'absolute';
|
161 |
+
invader.style.left = (i % maxCols) * invaderWidth + 'px';
|
162 |
+
invader.style.top = Math.floor(i / maxCols) * 50 + 'px';
|
163 |
+
invadersContainer.appendChild(invader);
|
164 |
+
}
|
165 |
+
}
|
166 |
+
|
167 |
+
// Move player
|
168 |
+
function movePlayer(e) {
|
169 |
+
switch (e.key) {
|
170 |
+
case 'ArrowLeft':
|
171 |
+
if (playerPosition > 0) {
|
172 |
+
playerPosition -= playerSpeed;
|
173 |
+
player.style.left = playerPosition + 'px';
|
174 |
+
}
|
175 |
+
break;
|
176 |
+
case 'ArrowRight':
|
177 |
+
if (playerPosition < game.offsetWidth - player.offsetWidth) {
|
178 |
+
playerPosition += playerSpeed;
|
179 |
+
player.style.left = playerPosition + 'px';
|
180 |
+
}
|
181 |
+
break;
|
182 |
+
case ' ':
|
183 |
+
shoot();
|
184 |
+
break;
|
185 |
+
}
|
186 |
+
}
|
187 |
+
|
188 |
+
// Shoot bullet
|
189 |
+
function shoot() {
|
190 |
+
const bullet = document.createElement('div');
|
191 |
+
bullet.classList.add('bullet');
|
192 |
+
bullet.style.left = playerPosition + 17.5 + 'px'; // Center bullet to player
|
193 |
+
bullet.style.bottom = '60px'; // Starting position above player
|
194 |
+
game.appendChild(bullet);
|
195 |
+
moveBullet(bullet);
|
196 |
+
}
|
197 |
+
|
198 |
+
// Move bullet
|
199 |
+
function moveBullet(bullet) {
|
200 |
+
const bulletInterval = setInterval(() => {
|
201 |
+
const bulletBottom = parseInt(bullet.style.bottom.replace('px', ''));
|
202 |
+
if (bulletBottom >= game.offsetHeight) {
|
203 |
+
clearInterval(bulletInterval);
|
204 |
+
bullet.remove();
|
205 |
+
} else {
|
206 |
+
bullet.style.bottom = bulletBottom + 5 + 'px';
|
207 |
+
}
|
208 |
+
checkBulletCollision(bullet, bulletInterval);
|
209 |
+
}, 20);
|
210 |
+
}
|
211 |
+
|
212 |
+
// Check bullet collision with invaders
|
213 |
+
function checkBulletCollision(bullet, bulletInterval) {
|
214 |
+
const bulletRect = bullet.getBoundingClientRect();
|
215 |
+
document.querySelectorAll('.invader').forEach(invader => {
|
216 |
+
const invaderRect = invader.getBoundingClientRect();
|
217 |
+
if (bulletRect.top <= invaderRect.bottom && bulletRect.bottom >= invaderRect.top &&
|
218 |
+
bulletRect.left <= invaderRect.right && bulletRect.right >= invaderRect.left) {
|
219 |
+
bullet.remove();
|
220 |
+
invader.remove();
|
221 |
+
clearInterval(bulletInterval);
|
222 |
+
score++;
|
223 |
+
scoreboard.textContent = `Score: ${score}`;
|
224 |
+
}
|
225 |
+
});
|
226 |
+
}
|
227 |
+
|
228 |
+
// Move invaders
|
229 |
+
function moveInvaders() {
|
230 |
+
const invaders = Array.from(document.querySelectorAll('.invader'));
|
231 |
+
invaders.forEach(invader => {
|
232 |
+
const currentTop = parseInt(invader.style.top.replace('px', ''));
|
233 |
+
invader.style.top = currentTop + invaderSpeed + 'px';
|
234 |
+
});
|
235 |
+
|
236 |
+
// Check if invaders reach the bottom
|
237 |
+
invaders.forEach(invader => {
|
238 |
+
const invaderBottom = parseInt(invader.style.top.replace('px', '')) + invader.offsetHeight;
|
239 |
+
if (invaderBottom >= game.offsetHeight) {
|
240 |
+
gameOverMessage.style.display = 'block';
|
241 |
+
usernameDisplay.textContent = username;
|
242 |
+
scoreDisplay.textContent = score;
|
243 |
+
clearInterval(invaderInterval);
|
244 |
+
}
|
245 |
+
});
|
246 |
+
}
|
247 |
+
});
|
248 |
+
</script>
|
249 |
+
</body>
|
250 |
</html>
|