-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
423 lines (342 loc) · 10.9 KB
/
index.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Asteroids</title>
<style>
canvas {
display: block;
margin: 0 auto;
background-color: black;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<audio id="shoot" src="pew-pew.wav" preload="auto"></audio>
<audio id="thrustSound" src="thrust.wav" preload="auto"></audio>
<audio id="hit" src="pew-pew.wav" preload="auto"></audio>
<audio id="gameOverSound" src="mixkit-player-losing-or-failing-2042.wav" preload="auto"></audio>
<audio id="backgrond" src="pew-pew.wav" preload="auto"></audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let gameOver = false;
let score = 0;
let dirtyMode = false;
let keys = {
ArrowLeft: false,
ArrowRight: false,
ArrowUp: false,
};
// sounds
const thrustSound = document.getElementById('thrustSound');
function drawScore() {
ctx.font = '16px sans-serif';
ctx.fillStyle = 'white';
ctx.fillText('Score: ' + score, 10, 20);
}
// ship
// ====
let ship = {
x: canvas.width / 2,
y: canvas.height / 2,
angle: 0,
velocityX: 0,
velocityY: 0,
maxSpeed: 5,
acceleration: 0.2,
rotationSpeed: 0.1,
friction: 0.99,
// apply thrust at current ship angle
thrust() {
//thrustSound.currentTime = 0.5;
//thrustSound.play();
this.velocityX += Math.cos(this.angle) * this.acceleration;
this.velocityY += Math.sin(this.angle) * this.acceleration;
// calculate speed, applying max limit
const speed = Math.sqrt(Math.pow(this.velocityX, 2) + Math.pow(this.velocityY, 2));
if (speed > this.maxSpeed) {
this.velocityX *= this.maxSpeed / speed;
this.velocityY *= this.maxSpeed / speed;
}
},
// rotate left (-1) or right (1)
rotate(direction) {
this.angle += direction * this.rotationSpeed;
},
update() {
// update ship coordinates
this.x += this.velocityX;
this.y += this.velocityY;
// apply space friction
this.velocityX *= this.friction;
this.velocityY *= this.friction;
// wrap around edges
if (this.x < 0) this.x = canvas.width;
if (this.x > canvas.width) this.x = 0;
if (this.y < 0) this.y = canvas.height;
if (this.y > canvas.height) this.y = 0;
},
// draw ship shape
draw() {
if (!dirtyMode) {
// normal ship
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.beginPath();
ctx.moveTo(15, 0);
ctx.lineTo(-15, 10);
ctx.lineTo(-10, 0);
ctx.lineTo(-15, -10);
ctx.closePath();
ctx.strokeStyle = 'white';
ctx.stroke();
ctx.restore();
} else {
// not normal ship
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.beginPath();
//ctx.moveTo(this.x, this.y);
// balls
ctx.moveTo(-10, 0);
ctx.arc(-10, -10, 10, 2, (Math.PI * 2)+0.5);
ctx.moveTo(0, 10);
ctx.arc(-10, 10, 10, -0.4, 4.5);
// shaft
ctx.moveTo(0, 5);
ctx.lineTo(24, 5);
ctx.moveTo(0, -5);
ctx.lineTo(24, -5);
ctx.moveTo(20, 0);
ctx.moveTo(35, 0);
ctx.arc(30, 0, 8, 0, Math.PI * 2);
// pubes
ctx.moveTo(-13, -15)
ctx.lineTo(-23, -15)
ctx.moveTo(-15, -10)
ctx.lineTo(-25, -10)
ctx.moveTo(-13, -5)
ctx.lineTo(-23, -5)
ctx.moveTo(-13, 15)
ctx.lineTo(-23, 15)
ctx.moveTo(-15, 10)
ctx.lineTo(-25, 10)
ctx.moveTo(-13, 5)
ctx.lineTo(-23, 5)
ctx.moveTo(0,0)
//ctx.moveTo(15, 0);
//ctx.lineTo(-15, 10);
//ctx.lineTo(-10, 0);
//ctx.lineTo(-15, -10);
ctx.strokeStyle = '#ffd7ae';
ctx.stroke();
ctx.closePath();
ctx.restore();
}
},
// test for asteroid collsion
checkCollision() {
for (let asteroid of asteroids.items) {
let dx = this.x - asteroid.x;
let dy = this.y - asteroid.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= asteroid.size) {
return true;
}
}
return false;
},
};
// asteroids
// =========
let asteroids = {
items: [],
spawnInterval: 2000,
lastSpawnTime: 0,
update() {
let currentTime = performance.now();
if (currentTime - this.lastSpawnTime > this.spawnInterval) {
let new_asteroid = {
angle: Math.random() * Math.PI * 2,
size: Math.random() * 30 + 20,
}
if (Math.random() > 0.5) {
new_asteroid.x = Math.random() * canvas.width;
new_asteroid.y = Math.random() > 0.5 ? 0 - new_asteroid.size : canvas.height + new_asteroid.size;
} else {
new_asteroid.x = Math.random() > 0.5 ? 0 - new_asteroid.size : canvas.width + new_asteroid.size;
new_asteroid.y = Math.random() * canvas.height;
}
this.items.push(new_asteroid);
//this.items.push(() => {
// let new_asteroid = {
// angle: Math.random() * Math.PI * 2,
// size: Math.random() * 30 + 20,
// }
// if (Math.random() > 0.5) {
// new_asteroid.x = Math.random() * canvas.width;
// new_asteroid.y = Math.random() > 0.5 ? 0 : canvas.height;
// } else {
// new_asteroid.x = Math.random() > 0.5 ? 0 : canvas.width;
// new_asteroid.y = Math.random() * canvas.height;
// }
// return new_asteroid;
//});
//this.items.push({
// x: Math.random() * canvas.width,
// y: Math.random() * canvas.height,
// angle: Math.random() * Math.PI * 2,
// size: Math.random() * 30 + 20,
//});
this.lastSpawnTime = currentTime;
}
for (let asteroid of this.items) {
asteroid.x += Math.cos(asteroid.angle);
asteroid.y += Math.sin(asteroid.angle);
// wrap around edges
if (asteroid.x < 0 - asteroid.size) asteroid.x = canvas.width + asteroid.size;
if (asteroid.x > canvas.width + asteroid.size) asteroid.x = 0 - asteroid.size;
if (asteroid.y < 0 - asteroid.size) asteroid.y = canvas.height + asteroid.size;
if (asteroid.y > canvas.height + asteroid.size) asteroid.y = 0 - asteroid.size;
}
},
draw() {
// draw each asteroid
ctx.strokeStyle = 'white';
for (let asteroid of this.items) {
ctx.beginPath();
ctx.arc(asteroid.x, asteroid.y, asteroid.size, 0, Math.PI * 2);
ctx.stroke();
if (dirtyMode) {
ctx.beginPath();
ctx.arc(asteroid.x, asteroid.y+5, asteroid.size/4, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.arc(asteroid.x, asteroid.y+5, asteroid.size/8, -0.5, Math.PI+0.5);
ctx.stroke();
}
}
},
};
// shots
// =====
let shots = {
items: [],
speed: 5,
update() {
for (let i = 0; i < this.items.length; i++) {
let shot = this.items[i];
shot.x += Math.cos(shot.angle) * this.speed;
shot.y += Math.sin(shot.angle) * this.speed;
if (
shot.x < 0 ||
shot.x > canvas.width ||
shot.y < 0 ||
shot.y > canvas.height
) {
this.items.splice(i, 1);
i--;
continue;
}
for (let j = 0; j < asteroids.items.length; j++) {
let asteroid = asteroids.items[j];
let dx = shot.x - asteroid.x;
let dy = shot.y - asteroid.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= asteroid.size) {
this.items.splice(i, 1);
i--;
if (asteroid.size >= 20) {
let smallerAsteroidCount = 2;
for (let k = 0; k < smallerAsteroidCount; k++) {
let newAngle = asteroid.angle + Math.random() * 0.5 - 0.25;
let newSize = asteroid.size / 2;
asteroids.items.push({ x: asteroid.x, y: asteroid.y, angle: newAngle, size: newSize });
}
}
if (asteroid.size >= 40) {
score += 2;
} else {
score += 3;
}
asteroids.items.splice(j, 1);
j--;
break;
}
}
}
},
draw() {
ctx.fillStyle = 'white';
for (let shot of this.items) {
ctx.beginPath();
ctx.arc(shot.x, shot.y, 2, 0, Math.PI * 2);
ctx.fill();
}
},
};
// game loop
// =========
function gameLoop() {
// test for game over collision
if (ship.checkCollision()) {
gameOver = true;
}
if (gameOver) {
ctx.font = '48px sans-serif';
ctx.fillStyle = 'white';
ctx.fillText('Game Over', canvas.width / 2 - 100, canvas.height / 2);
//const gameOverSound = document.getElementById('gameOverSound');
//gameOverSound.currentTime = 0;
//gameOverSound.play();
return;
}
// handle controls
if (keys.ArrowLeft) {
ship.rotate(-1);
}
if (keys.ArrowRight) {
ship.rotate(1);
}
if (keys.ArrowUp) {
ship.thrust();
}
// update and redraw ship, asteroids, shots, and score
ctx.clearRect(0, 0, canvas.width, canvas.height);
ship.update();
ship.draw();
asteroids.update();
asteroids.draw();
shots.update();
shots.draw();
drawScore();
requestAnimationFrame(gameLoop);
}
window.addEventListener('keydown', (e) => {
if (e.code in keys) {
keys[e.code] = true;
} else if (e.code === 'Space') {
// limit concurrent shots
if (shots.items.length < 3) {
shots.items.push({
x: ship.x,
y: ship.y,
angle: ship.angle,
});
}
}
});
window.addEventListener('keyup', (e) => {
if (e.code in keys) {
keys[e.code] = false;
}
});
gameLoop();
</script>
</body>
</html>