-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 Collision Spaceship Asteroids.html
348 lines (308 loc) · 12.9 KB
/
3 Collision Spaceship Asteroids.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Asteroids</title>
<style></style>
</head>
<body>
<canvas id="gameCanvas" width="760" height="570"></canvas>
<script>
const FPS = 30; // frames per second
const FRICTION = 0.7; // friction coefficient of space (0 = no friction, 1 = lots of friction)
const ROID_JAG = 0.4; // jaggedness of the asteroids (0 = none, 1 = lots)
const ROID_NUM = 3; // starting number of asteroids
const ROID_SIZE = 100; // starting size of asteroids in pixels
const ROID_SPD = 50; // max starting speed of asteroids in pixels per second
const ROID_VERT = 10; // average number of vertices on each asteroid
const SHIP_BLINK_DUR = 0.1; // duration in seconds of a single blink during ship's invisibility
const SHIP_EXPLODE_DUR = 0.3; // duration of the ship's explosion in seconds
const SHIP_INV_DUR = 3; // duration of the ship's invisibility in seconds
const SHIP_SIZE = 30; // ship height in pixels
const SHIP_THRUST = 5; // acceleration of the ship in pixels per second per second
const SHIP_TURN_SPD = 360; // turn speed in degrees per second
const SHOW_BOUNDING = false; // show or hide collision bounding
const SHOW_CENTRE_DOT = false; // show or hide ship's centre dot
/** @type {HTMLCanvasElement} */
var canv = document.getElementById("gameCanvas");
var ctx = canv.getContext("2d");
// set up the spaceship object
var ship = newShip();
// set up asteroids
var roids = [];
createAsteroidBelt();
// set up event handlers
document.addEventListener("keydown", keyDown);
document.addEventListener("keyup", keyUp);
// set up the game loop
setInterval(update, 1000 / FPS);
function createAsteroidBelt() {
roids = [];
var x, y;
for (var i = 0; i < ROID_NUM; i++) {
// random asteroid location (not touching spaceship)
do {
x = Math.floor(Math.random() * canv.width);
y = Math.floor(Math.random() * canv.height);
} while (distBetweenPoints(ship.x, ship.y, x, y) < ROID_SIZE * 2 + ship.r);
roids.push(newAsteroid(x, y));
}
}
function distBetweenPoints(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function explodeShip() {
ship.explodeTime = Math.ceil(SHIP_EXPLODE_DUR * FPS);
}
function keyDown(/** @type {KeyboardEvent} */ ev) {
switch(ev.keyCode) {
case 37: // left arrow (rotate ship left)
ship.rot = SHIP_TURN_SPD / 180 * Math.PI / FPS;
break;
case 38: // up arrow (thrust the ship forward)
ship.thrusting = true;
break;
case 39: // right arrow (rotate ship right)
ship.rot = -SHIP_TURN_SPD / 180 * Math.PI / FPS;
break;
}
}
function keyUp(/** @type {KeyboardEvent} */ ev) {
switch(ev.keyCode) {
case 37: // left arrow (stop rotating left)
ship.rot = 0;
break;
case 38: // up arrow (stop thrusting)
ship.thrusting = false;
break;
case 39: // right arrow (stop rotating right)
ship.rot = 0;
break;
}
}
function newAsteroid(x, y) {
var roid = {
x: x,
y: y,
xv: Math.random() * ROID_SPD / FPS * (Math.random() < 0.5 ? 1 : -1),
yv: Math.random() * ROID_SPD / FPS * (Math.random() < 0.5 ? 1 : -1),
a: Math.random() * Math.PI * 2, // in radians
r: ROID_SIZE / 2,
offs: [],
vert: Math.floor(Math.random() * (ROID_VERT + 1) + ROID_VERT / 2)
};
// populate the offsets array
for (var i = 0; i < roid.vert; i++) {
roid.offs.push(Math.random() * ROID_JAG * 2 + 1 - ROID_JAG);
}
return roid;
}
function newShip() {
return {
x: canv.width / 2,
y: canv.height / 2,
a: 90 / 180 * Math.PI, // convert to radians
r: SHIP_SIZE / 2,
blinkNum: Math.ceil(SHIP_INV_DUR / SHIP_BLINK_DUR),
blinkTime: Math.ceil(SHIP_BLINK_DUR * FPS),
explodeTime: 0,
rot: 0,
thrusting: false,
thrust: {
x: 0,
y: 0
}
}
}
function update() {
var blinkOn = ship.blinkNum % 2 == 0;
var exploding = ship.explodeTime > 0;
// draw space
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canv.width, canv.height);
// draw the asteroids
var a, r, x, y, offs, vert;
for (var i = 0; i < roids.length; i++) {
ctx.strokeStyle = "slategrey";
ctx.lineWidth = SHIP_SIZE / 20;
// get the asteroid properties
a = roids[i].a;
r = roids[i].r;
x = roids[i].x;
y = roids[i].y;
offs = roids[i].offs;
vert = roids[i].vert;
// draw the path
ctx.beginPath();
ctx.moveTo(
x + r * offs[0] * Math.cos(a),
y + r * offs[0] * Math.sin(a)
);
// draw the polygon
for (var j = 1; j < vert; j++) {
ctx.lineTo(
x + r * offs[j] * Math.cos(a + j * Math.PI * 2 / vert),
y + r * offs[j] * Math.sin(a + j * Math.PI * 2 / vert)
);
}
ctx.closePath();
ctx.stroke();
// show asteroid's collision circle
if (SHOW_BOUNDING) {
ctx.strokeStyle = "lime";
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2, false);
ctx.stroke();
}
}
// thrust the ship
if (ship.thrusting) {
ship.thrust.x += SHIP_THRUST * Math.cos(ship.a) / FPS;
ship.thrust.y -= SHIP_THRUST * Math.sin(ship.a) / FPS;
// draw the thruster
if (!exploding && blinkOn) {
ctx.fillStyle = "red";
ctx.strokeStyle = "yellow";
ctx.lineWidth = SHIP_SIZE / 10;
ctx.beginPath();
ctx.moveTo( // rear left
ship.x - ship.r * (2 / 3 * Math.cos(ship.a) + 0.5 * Math.sin(ship.a)),
ship.y + ship.r * (2 / 3 * Math.sin(ship.a) - 0.5 * Math.cos(ship.a))
);
ctx.lineTo( // rear centre (behind the ship)
ship.x - ship.r * 5 / 3 * Math.cos(ship.a),
ship.y + ship.r * 5 / 3 * Math.sin(ship.a)
);
ctx.lineTo( // rear right
ship.x - ship.r * (2 / 3 * Math.cos(ship.a) - 0.5 * Math.sin(ship.a)),
ship.y + ship.r * (2 / 3 * Math.sin(ship.a) + 0.5 * Math.cos(ship.a))
);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
} else {
// apply friction (slow the ship down when not thrusting)
ship.thrust.x -= FRICTION * ship.thrust.x / FPS;
ship.thrust.y -= FRICTION * ship.thrust.y / FPS;
}
// draw the triangular ship
if (!exploding) {
if (blinkOn) {
ctx.strokeStyle = "white";
ctx.lineWidth = SHIP_SIZE / 20;
ctx.beginPath();
ctx.moveTo( // nose of the ship
ship.x + 4 / 3 * ship.r * Math.cos(ship.a),
ship.y - 4 / 3 * ship.r * Math.sin(ship.a)
);
ctx.lineTo( // rear left
ship.x - ship.r * (2 / 3 * Math.cos(ship.a) + Math.sin(ship.a)),
ship.y + ship.r * (2 / 3 * Math.sin(ship.a) - Math.cos(ship.a))
);
ctx.lineTo( // rear right
ship.x - ship.r * (2 / 3 * Math.cos(ship.a) - Math.sin(ship.a)),
ship.y + ship.r * (2 / 3 * Math.sin(ship.a) + Math.cos(ship.a))
);
ctx.closePath();
ctx.stroke();
}
// handle blinking
if (ship.blinkNum > 0) {
// reduce the blink time
ship.blinkTime--;
// reduce the blink num
if (ship.blinkTime == 0) {
ship.blinkTime = Math.ceil(SHIP_BLINK_DUR * FPS);
ship.blinkNum--;
}
}
} else {
// draw the explosion (concentric circles of different colours)
ctx.fillStyle = "darkred";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r * 1.7, 0, Math.PI * 2, false);
ctx.fill();
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r * 1.4, 0, Math.PI * 2, false);
ctx.fill();
ctx.fillStyle = "orange";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r * 1.1, 0, Math.PI * 2, false);
ctx.fill();
ctx.fillStyle = "yellow";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r * 0.8, 0, Math.PI * 2, false);
ctx.fill();
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r * 0.5, 0, Math.PI * 2, false);
ctx.fill();
}
// show ship's collision circle
if (SHOW_BOUNDING) {
ctx.strokeStyle = "lime";
ctx.beginPath();
ctx.arc(ship.x, ship.y, ship.r, 0, Math.PI * 2, false);
ctx.stroke();
}
// show ship's centre dot
if (SHOW_CENTRE_DOT) {
ctx.fillStyle = "red";
ctx.fillRect(ship.x - 1, ship.y - 1, 2, 2);
}
// check for asteroid collisions (when not exploding)
if (!exploding) {
// only check when not blinking
if (ship.blinkNum == 0) {
for (var i = 0; i < roids.length; i++) {
if (distBetweenPoints(ship.x, ship.y, roids[i].x, roids[i].y) < ship.r + roids[i].r) {
explodeShip();
}
}
}
// rotate the ship
ship.a += ship.rot;
// move the ship
ship.x += ship.thrust.x;
ship.y += ship.thrust.y;
} else {
// reduce the explode time
ship.explodeTime--;
// reset the ship after the explosion has finished
if (ship.explodeTime == 0) {
ship = newShip();
}
}
// handle edge of screen
if (ship.x < 0 - ship.r) {
ship.x = canv.width + ship.r;
} else if (ship.x > canv.width + ship.r) {
ship.x = 0 - ship.r;
}
if (ship.y < 0 - ship.r) {
ship.y = canv.height + ship.r;
} else if (ship.y > canv.height + ship.r) {
ship.y = 0 - ship.r;
}
// move the asteroids
for (var i = 0; i < roids.length; i++) {
roids[i].x += roids[i].xv;
roids[i].y += roids[i].yv;
// handle asteroid edge of screen
if (roids[i].x < 0 - roids[i].r) {
roids[i].x = canv.width + roids[i].r;
} else if (roids[i].x > canv.width + roids[i].r) {
roids[i].x = 0 - roids[i].r
}
if (roids[i].y < 0 - roids[i].r) {
roids[i].y = canv.height + roids[i].r;
} else if (roids[i].y > canv.height + roids[i].r) {
roids[i].y = 0 - roids[i].r
}
}
}
</script>
</body>
</html>