-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspace_invaders.html
More file actions
405 lines (349 loc) · 12.6 KB
/
space_invaders.html
File metadata and controls
405 lines (349 loc) · 12.6 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!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;
padding: 0;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: 'Courier New', monospace;
}
#gameContainer {
text-align: center;
}
canvas {
border: 2px solid #0f0;
display: block;
margin: 0 auto;
background-color: #000;
}
#score {
color: #0f0;
font-size: 20px;
margin-bottom: 10px;
}
#gameOver {
color: #f00;
font-size: 24px;
margin-top: 20px;
display: none;
}
#instructions {
color: #0f0;
font-size: 14px;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="gameOver">GAME OVER<br>Press R to Restart</div>
<div id="instructions">Use ← → Arrow Keys to Move, SPACE to Shoot</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
// Game state
let score = 0;
let gameRunning = true;
let animationId;
// Player
const player = {
x: canvas.width / 2 - 25,
y: canvas.height - 60,
width: 50,
height: 30,
speed: 5
};
// Bullets
const bullets = [];
const bulletSpeed = 10;
const bulletWidth = 4;
const bulletHeight = 10;
// Enemies
const enemies = [];
const enemyRows = 5;
const enemyCols = 11;
const enemyWidth = 40;
const enemyHeight = 30;
const enemyPadding = 10;
const enemyOffsetTop = 60;
const enemyOffsetLeft = 70;
let enemyDirection = 1;
let enemySpeed = 0.5;
let enemyDropDistance = 30;
// Enemy bullets
const enemyBullets = [];
const enemyBulletSpeed = 4;
let lastEnemyBulletTime = 0;
const enemyBulletInterval = 1000; // milliseconds
// Input handling
const keys = {};
let lastShootTime = 0;
const shootCooldown = 300; // milliseconds
// Initialize enemies
function initEnemies() {
for (let row = 0; row < enemyRows; row++) {
for (let col = 0; col < enemyCols; col++) {
enemies.push({
x: col * (enemyWidth + enemyPadding) + enemyOffsetLeft,
y: row * (enemyHeight + enemyPadding) + enemyOffsetTop,
width: enemyWidth,
height: enemyHeight,
alive: true,
points: (5 - row) * 10 // Different points for different rows
});
}
}
}
// Handle keyboard input
document.addEventListener('keydown', (e) => {
keys[e.key] = true;
if (e.key === 'r' || e.key === 'R') {
if (!gameRunning) {
restartGame();
}
}
});
document.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
// Update player position
function updatePlayer() {
if (keys['ArrowLeft'] && player.x > 0) {
player.x -= player.speed;
}
if (keys['ArrowRight'] && player.x < canvas.width - player.width) {
player.x += player.speed;
}
const currentTime = Date.now();
if (keys[' '] && currentTime - lastShootTime > shootCooldown) {
shootBullet();
lastShootTime = currentTime;
}
}
// Shoot bullet
function shootBullet() {
bullets.push({
x: player.x + player.width / 2 - bulletWidth / 2,
y: player.y,
width: bulletWidth,
height: bulletHeight,
speed: bulletSpeed
});
}
// Update bullets
function updateBullets() {
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].y -= bullets[i].speed;
// Remove bullet if it goes off screen
if (bullets[i].y < 0) {
bullets.splice(i, 1);
}
}
}
// Update enemies
function updateEnemies() {
let shouldDrop = false;
let leftMost = canvas.width;
let rightMost = 0;
for (let enemy of enemies) {
if (enemy.alive) {
enemy.x += enemySpeed * enemyDirection;
leftMost = Math.min(leftMost, enemy.x);
rightMost = Math.max(rightMost, enemy.x + enemy.width);
// Check if enemy reached player level
if (enemy.y + enemy.height >= player.y) {
gameOver();
return;
}
}
}
// Check if enemies hit boundaries
if (leftMost <= 0 || rightMost >= canvas.width) {
enemyDirection *= -1;
shouldDrop = true;
}
// Drop enemies down
if (shouldDrop) {
for (let enemy of enemies) {
enemy.y += enemyDropDistance;
}
}
}
// Enemy shooting
function updateEnemyBullets() {
const currentTime = Date.now();
// Random enemy shoots occasionally
if (currentTime - lastEnemyBulletTime > enemyBulletInterval) {
const aliveEnemies = enemies.filter(e => e.alive);
if (aliveEnemies.length > 0) {
const shooter = aliveEnemies[Math.floor(Math.random() * aliveEnemies.length)];
enemyBullets.push({
x: shooter.x + shooter.width / 2 - 2,
y: shooter.y + shooter.height,
width: 4,
height: 10,
speed: enemyBulletSpeed
});
lastEnemyBulletTime = currentTime;
}
}
// Update existing enemy bullets
for (let i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].y += enemyBullets[i].speed;
// Check collision with player
if (checkCollision(enemyBullets[i], player)) {
gameOver();
enemyBullets.splice(i, 1);
return;
}
// Remove bullet if it goes off screen
if (enemyBullets[i].y > canvas.height) {
enemyBullets.splice(i, 1);
}
}
}
// Check collision between two rectangles
function checkCollision(rect1, rect2) {
return rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y;
}
// Check collisions
function checkCollisions() {
// Check player bullets vs enemies
for (let i = bullets.length - 1; i >= 0; i--) {
for (let j = enemies.length - 1; j >= 0; j--) {
if (enemies[j].alive && checkCollision(bullets[i], enemies[j])) {
enemies[j].alive = false;
bullets.splice(i, 1);
score += enemies[j].points;
updateScore();
break;
}
}
}
// Check if all enemies are dead
if (enemies.every(e => !e.alive)) {
victory();
}
}
// Update score display
function updateScore() {
scoreElement.textContent = `Score: ${score}`;
}
// Game over
function gameOver() {
gameRunning = false;
gameOverElement.style.display = 'block';
cancelAnimationFrame(animationId);
}
// Victory
function victory() {
gameRunning = false;
gameOverElement.innerHTML = `VICTORY!<br>Final Score: ${score}<br>Press R to Play Again`;
gameOverElement.style.color = '#0f0';
gameOverElement.style.display = 'block';
cancelAnimationFrame(animationId);
}
// Restart game
function restartGame() {
score = 0;
updateScore();
gameOverElement.style.display = 'none';
gameOverElement.style.color = '#f00';
gameRunning = true;
// Reset player
player.x = canvas.width / 2 - 25;
// Clear arrays
bullets.length = 0;
enemyBullets.length = 0;
enemies.length = 0;
// Reinitialize
initEnemies();
// Start game loop
gameLoop();
}
// Draw functions
function drawPlayer() {
ctx.fillStyle = '#0f0';
ctx.beginPath();
ctx.moveTo(player.x + player.width / 2, player.y);
ctx.lineTo(player.x, player.y + player.height);
ctx.lineTo(player.x + player.width, player.y + player.height);
ctx.closePath();
ctx.fill();
}
function drawBullets() {
ctx.fillStyle = '#0f0';
for (let bullet of bullets) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
}
function drawEnemies() {
for (let enemy of enemies) {
if (enemy.alive) {
// Different colors for different rows
const colors = ['#f00', '#f80', '#ff0', '#0f0', '#0ff'];
ctx.fillStyle = colors[Math.floor((enemy.y - enemyOffsetTop) / (enemyHeight + enemyPadding))] || '#f00';
// Draw enemy as rectangle
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
// Add some detail
ctx.fillStyle = '#000';
ctx.fillRect(enemy.x + 5, enemy.y + 5, 10, 10);
ctx.fillRect(enemy.x + 25, enemy.y + 5, 10, 10);
}
}
}
function drawEnemyBullets() {
ctx.fillStyle = '#f00';
for (let bullet of enemyBullets) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
}
// Game loop
function gameLoop() {
if (!gameRunning) return;
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw stars for background
ctx.fillStyle = '#fff';
for (let i = 0; i < 20; i++) {
const x = (i * 73) % canvas.width;
const y = (i * 37) % canvas.height;
ctx.fillRect(x, y, 1, 1);
}
// Update game objects
updatePlayer();
updateBullets();
updateEnemies();
updateEnemyBullets();
checkCollisions();
// Draw game objects
drawEnemies();
drawBullets();
drawEnemyBullets();
drawPlayer();
// Continue loop
animationId = requestAnimationFrame(gameLoop);
}
// Initialize and start game
initEnemies();
gameLoop();
</script>
</body>
</html>