-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.html
378 lines (325 loc) · 11.3 KB
/
space.html
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
<!DOCTYPE html>
<html>
<head>
<title>Nick Ber.</title>
<link rel="icon" type="image/x-icon" href="assets/img/favicon.ico">
<embed src="space.mp3" loop="true" autostart="true" width="2"
height="0">
<style>
body { background: #000; color: #fff; }
#gameCanvas { background: #fff; }
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: black;
}
canvas {
border: 1px solid white;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
// SpaceInvaders.js
// Define the canvas and 2D context
let canvas = document.getElementById('gameCanvas');
let ctx = canvas.getContext('2d');
// Load the background image and define its vertical position
let backgroundImage = new Image();
backgroundImage.src = 'background.png';
let backgroundY = 0;
// Define the spaceship object and load its image
let spaceship = {
x: canvas.width / 2,
y: canvas.height - 60,
width: 50,
height: 50
};
let spaceshipImg = new Image();
spaceshipImg.src = 'spaceship2.png';
// Create the invaders array and load their images
let invaders = [];
let invaderImgs = [];
for(let type = 0; type < 3; type++) {
invaderImgs[type] = new Image();
invaderImgs[type].src = `invader${type + 1}.png`;
for(let i = 0; i < 10; i++) {
invaders.push({
x: i * 70,
y: 30 + (50 * type),
width: 40,
height: 40,
type: type
});
}
}
// Create the bullets and invader bullets arrays
let bullets = [];
let invaderBullets = [];
// Create the debris array
let debris = [];
// Define the invaders' horizontal direction (1 for right, -1 for left)
let invaderDirection = 1;
// Create the protective blocks
let blocks = [];
let subBlockSize = 10;
for(let x = 100; x <= 700; x += 200) {
for(let y = 400; y < 450; y += subBlockSize) {
for(let x2 = x; x2 < x + 50; x2 += subBlockSize) {
blocks.push({ x: x2, y: y, width: subBlockSize, height: subBlockSize });
}
}
}
// Audio setup for sound effects
let bulletSound = new Audio('bullet1.mp3');
let invaderDestroyedSound = new Audio('explosion.mp3');
// Set the initial bullet cooldown
let bulletCooldown = 0;
// Function to play the bullet sound effect
function playBulletSound() {
bulletSound.currentTime = 0;
bulletSound.play();
}
// Function to play the invader destroyed sound effect
function playInvaderDestroyedSound() {
invaderDestroyedSound.currentTime = 0;
invaderDestroyedSound.play();
}
// Initialize the score
let score = 0;
// Event listener for keyboard input
window.addEventListener('keydown', function(event) {
if(gameOver && event.code == 'Space') return; // Don't fire if game over
// Move spaceship left or right, or fire a bullet
switch(event.code) {
case 'ArrowLeft':
spaceship.x -= 20;
break;
case 'ArrowRight':
spaceship.x += 20;
break;
case 'Space':
if(bulletCooldown > 60) { // 180 frames ~ 3 seconds
bullets.push({
x: spaceship.x + spaceship.width / 2,
y: spaceship.y,
width: 5,
height: 10
});
bulletCooldown = 0;
// Play bullet sound effect when firing
playBulletSound();
}
break;
}
});
// Collision detection function
function collision(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;
}
// Set the initial game state to not over
let gameOver = false;
let gameOverTime;
// Main game loop
function gameLoop() {
let now = Date.now();
if(gameOver && now - gameOverTime > 3000) return; // Stop game 3 sec after game over
// Clear the canvas for the new frame
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the scrolling background
ctx.drawImage(backgroundImage, 0, backgroundY, canvas.width, canvas.height);
ctx.drawImage(backgroundImage, 0, backgroundY - canvas.height, canvas.width, canvas.height);
backgroundY++;
if(backgroundY >= canvas.height)
backgroundY = 0;
// Draw the spaceship unless the game is over
if(!gameOver) {
ctx.drawImage(spaceshipImg, spaceship.x, spaceship.y, spaceship.width, spaceship.height);
}
// Draw the invaders and handle their movement and shooting
let hitEdge = false;
for(let i = 0; i < invaders.length; i++) {
ctx.drawImage(invaderImgs[invaders[i].type], invaders[i].x, invaders[i].y, invaders[i].width, invaders[i].height);
// Move the invader in the current direction
invaders[i].x += invaderDirection * 0.4;
// Check if an invader hit the edge
if(invaders[i].x < 0 || invaders[i].x > canvas.width - invaders[i].width) {
hitEdge = true;
}
// Sometimes the invader shoots a bullet
if(Math.random() < 0.002) {
invaderBullets.push({
x: invaders[i].x + invaders[i].width / 2,
y: invaders[i].y + invaders[i].height,
width: 5,
height: 10
});
}
// If an invader touches a cover block, end the game
for(let j = 0; j < blocks.length; j++) {
if(collision(invaders[i], blocks[j])) {
gameOver = true;
gameOverTime = now;
return;
}
}
}
// If an invader hit the edge, move all invaders down and change direction
if(hitEdge) {
invaderDirection *= -1;
for(let i = 0; i < invaders.length; i++) {
invaders[i].y += 20;
}
}
// Draw the bullets and handle their movement
ctx.fillStyle = "#FF0000"; // Color of bullets
for(let i = 0; i < bullets.length; i++) {
ctx.fillRect(bullets[i].x, bullets[i].y, bullets[i].width, bullets[i].height);
bullets[i].y -= 2;
// Remove bullets that have gone off-screen
if(bullets[i].y < 0) {
bullets.splice(i, 1);
i--;
}
}
// Draw the invader bullets and handle their movement
ctx.fillStyle = "#00FF00"; // Color of invader bullets
for(let i = 0; i < invaderBullets.length; i++) {
ctx.fillRect(invaderBullets[i].x, invaderBullets[i].y, invaderBullets[i].width, invaderBullets[i].height);
invaderBullets[i].y += 2;
// Remove invader bullets that have gone off-screen
if(invaderBullets[i].y > canvas.height) {
invaderBullets.splice(i, 1);
i--;
}
}
// Handle bullet-invader collisions
for(let i = 0; i < bullets.length; i++) {
for(let j = 0; j < invaders.length; j++) {
if(collision(bullets[i], invaders[j])) {
// Remove the bullet and invader
bullets.splice(i, 1);
i--;
// Create debris
debris.push({
x: invaders[j].x,
y: invaders[j].y,
vx: (Math.random() * 2) - 1,
vy: -Math.random() * 2,
life: 1.0
});
invaders.splice(j, 1);
// Increment the score
score++;
// If an invader is destroyed, play the invader destroyed sound effect
playInvaderDestroyedSound();
// If all invaders are destroyed, end the game
if(invaders.length === 0) {
gameOver = true;
gameOverTime = now;
return;
}
break;
}
}
}
// Handle bullet-block collisions
for(let i = 0; i < bullets.length; i++) {
for(let j = 0; j < blocks.length; j++) {
if(collision(bullets[i], blocks[j])) {
// Remove the bullet and block
bullets.splice(i, 1);
i--;
blocks.splice(j, 1);
break;
}
}
}
// Handle invader bullet-block collisions
for(let i = 0; i < invaderBullets.length; i++) {
for(let j = 0; j < blocks.length; j++) {
if(collision(invaderBullets[i], blocks[j])) {
// Remove the invader bullet and block
invaderBullets.splice(i, 1);
i--;
blocks.splice(j, 1);
break;
}
}
}
// Handle invader bullet-spaceship collisions
for(let i = 0; i < invaderBullets.length; i++) {
if(collision(invaderBullets[i], spaceship)) {
// Remove the invader bullet and end the game
invaderBullets.splice(i, 1);
gameOver = true;
gameOverTime = now;
break;
}
}
// Handle spaceship bullet-invader bullet collisions
for(let i = 0; i < bullets.length; i++) {
for(let j = 0; j < invaderBullets.length; j++) {
if(collision(bullets[i], invaderBullets[j])) {
// Create debris at bullet collision
debris.push({
x: bullets[i].x,
y: bullets[i].y,
vx: (Math.random() * 2) - 1,
vy: -Math.random() * 2,
life: 1.0
});
// Remove both bullets
bullets.splice(i, 1);
invaderBullets.splice(j, 1);
i--;
break;
}
}
}
// Draw the protective blocks
ctx.fillStyle = "#03e9f4"; // Color of protective blocks
for(let i = 0; i < blocks.length; i++) {
ctx.fillRect(blocks[i].x, blocks[i].y, blocks[i].width, blocks[i].height);
}
// Draw the debris and handle their movement
ctx.fillStyle = "#FFFFFF"; // Color of debris
for(let i = 0; i < debris.length; i++) {
ctx.fillRect(debris[i].x, debris[i].y, 2, 2);
debris[i].x += debris[i].vx;
debris[i].y += debris[i].vy;
debris[i].life -= 0.01;
if(debris[i].life <= 0) {
debris.splice(i, 1);
i--;
}
}
// Draw the score
ctx.font = '20px Arial';
ctx.fillText('Score: ' + score, 10, 30);
// Handle game over state
if(gameOver) {
// Load and draw the picture when the game ends.
const img = new Image();
img.onload = function () {
context.drawImage(img, canvas.width / 2 - img.width / 2, canvas.height / 2 - img.height / 2);
};
img.src = 'gameover.png'; // The path to your picture.
}
// Increment the bullet cooldown
bulletCooldown++;
// Request the next frame
requestAnimationFrame(gameLoop);
}
// Start the game loop
gameLoop();
</script>
</body>
</html>