-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrogger.html
370 lines (321 loc) · 8.82 KB
/
frogger.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
<!DOCTYPE html>
<html>
<head>
<title>Basic Frogger HTML Game</title>
<meta charset="UTF-8">
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<canvas width="624" height="720" id="game"></canvas>
<script>
const canvas = document.getElementById('game');
const context = canvas.getContext('2d');
const grid = 48;
const gridGap = 10;
// a simple sprite prototype function
function Sprite(props) {
// shortcut for assigning all object properties to the sprite
Object.assign(this, props);
}
Sprite.prototype.render = function() {
context.fillStyle = this.color;
// draw a rectangle sprite
if (this.shape === 'rect') {
// by using a size less than the grid we can ensure there is a visual space
// between each row
context.fillRect(this.x, this.y + gridGap / 2, this.size, grid - gridGap);
}
// draw a circle sprite. since size is the diameter we need to divide by 2
// to get the radius. also the x/y position needs to be centered instead of
// the top-left corner of the sprite
else {
context.beginPath();
context.arc(
this.x + this.size / 2, this.y + this.size / 2,
this.size / 2 - gridGap / 2, 0, 2 * Math.PI
);
context.fill();
}
}
const frogger = new Sprite({
x: grid * 6,
y: grid * 13,
color: 'greenyellow',
size: grid,
shape: 'circle'
});
const scoredFroggers = [];
// a pattern describes each obstacle in the row
const patterns = [
// end bank is safe
null,
// log
{
spacing: [2], // how many grid spaces between each obstacle
color: '#c55843', // color of the obstacle
size: grid * 4, // width (rect) / diameter (circle) of the obstacle
shape: 'rect', // shape of the obstacle (rect or circle)
speed: 0.75 // how fast the obstacle moves and which direction
},
// turtle
{
spacing: [0,2,0,2,0,2,0,4],
color: '#de0004',
size: grid,
shape: 'circle',
speed: -1
},
// long log
{
spacing: [2],
color: '#c55843',
size: grid * 7,
shape: 'rect',
speed: 1.5
},
// log
{
spacing: [3],
color: '#c55843',
size: grid * 3,
shape: 'rect',
speed: 0.5
},
// turtle
{
spacing: [0,0,1],
color: '#de0004',
size: grid,
shape: 'circle',
speed: -1
},
// beach is safe
null,
// truck
{
spacing: [3,8],
color: '#c2c4da',
size: grid * 2,
shape: 'rect',
speed: -1
},
// fast car
{
spacing: [14],
color: '#c2c4da',
size: grid,
shape: 'rect',
speed: 0.75
},
// car
{
spacing: [3,3,7],
color: '#de3cdd',
size: grid,
shape: 'rect',
speed: -0.75
},
// bulldozer
{
spacing: [3,3,7],
color: '#0bcb00',
size: grid,
shape: 'rect',
speed: 0.5
},
// car
{
spacing: [4],
color: '#e5e401',
size: grid,
shape: 'rect',
speed: -0.5
},
// start zone is safe
null
];
// rows holds all the sprites for that row
const rows = [];
for (let i = 0; i < patterns.length; i++) {
rows[i] = [];
let x = 0;
let index = 0;
const pattern = patterns[i];
// skip empty patterns (safe zones)
if (!pattern) {
continue;
}
// allow there to be 1 extra pattern offscreen so the loop is seamless
// (especially for the long log)
let totalPatternWidth =
pattern.spacing.reduce((acc, space) => acc + space, 0) * grid +
pattern.spacing.length * pattern.size;
let endX = 0;
while (endX < canvas.width) {
endX += totalPatternWidth;
}
endX += totalPatternWidth;
// populate the row with sprites
while (x < endX) {
rows[i].push(new Sprite({
x,
y: grid * (i + 1),
index,
...pattern
}));
// move the next sprite over according to the spacing
const spacing = pattern.spacing;
x += pattern.size + spacing[index] * grid;
index = (index + 1) % spacing.length;
}
}
// game loop
function loop() {
requestAnimationFrame(loop);
context.clearRect(0,0,canvas.width,canvas.height);
// draw the game background
// water
context.fillStyle = '#000047';
context.fillRect(0, grid, canvas.width, grid * 6);
// end bank
context.fillStyle = '#1ac300';
context.fillRect(0, grid, canvas.width, 5);
context.fillRect(0, grid, 5, grid);
context.fillRect(canvas.width - 5, grid, 5, grid);
for (let i = 0; i < 4; i++) {
context.fillRect(grid + grid * 3 * i, grid, grid * 2, grid);
}
// beach
context.fillStyle = '#8500da';
context.fillRect(0, 7 * grid, canvas.width, grid);
// start zone
context.fillRect(0, canvas.height - grid * 2, canvas.width, grid);
// update and draw obstacles
for (let r = 0; r < rows.length; r++) {
const row = rows[r];
for (let i = 0; i < row.length; i++) {
const sprite = row[i]
sprite.x += sprite.speed;
sprite.render();
// loop sprite around the screen
// sprite is moving to the left and goes offscreen
if (sprite.speed < 0 && sprite.x < 0 - sprite.size) {
// find the rightmost sprite
let rightMostSprite = sprite;
for (let j = 0; j < row.length; j++) {
if (row[j].x > rightMostSprite.x) {
rightMostSprite = row[j];
}
}
// move the sprite to the next spot in the pattern so it continues
const spacing = patterns[r].spacing;
sprite.x =
rightMostSprite.x + rightMostSprite.size +
spacing[rightMostSprite.index] * grid;
sprite.index = (rightMostSprite.index + 1) % spacing.length;
}
// sprite is moving to the right and goes offscreen
if (sprite.speed > 0 && sprite.x > canvas.width) {
// find the leftmost sprite
let leftMostSprite = sprite;
for (let j = 0; j < row.length; j++) {
if (row[j].x < leftMostSprite.x) {
leftMostSprite = row[j];
}
}
// move the sprite to the next spot in the pattern so it continues
const spacing = patterns[r].spacing;
let index = leftMostSprite.index - 1;
index = index >= 0 ? index : spacing.length - 1;
sprite.x = leftMostSprite.x - spacing[index] * grid - sprite.size;
sprite.index = index;
}
}
}
// draw frogger
frogger.x += frogger.speed || 0;
frogger.render();
// draw scored froggers
scoredFroggers.forEach(frog => frog.render());
// check for collision with all sprites in the same row as frogger
const froggerRow = frogger.y / grid - 1 | 0;
let collision = false;
for (let i = 0; i < rows[froggerRow].length; i++) {
let sprite = rows[froggerRow][i];
// axis-aligned bounding box (AABB) collision check
// treat any circles as rectangles for the purposes of collision
if (frogger.x < sprite.x + sprite.size - gridGap &&
frogger.x + grid - gridGap > sprite.x &&
frogger.y < sprite.y + grid &&
frogger.y + grid > sprite.y) {
collision = true;
// reset frogger if got hit by car
if (froggerRow > rows.length / 2) {
frogger.x = grid * 6;
frogger.y = grid * 13;
}
// move frogger along with obstacle
else {
frogger.speed = sprite.speed;
}
}
}
if (!collision) {
// if fogger isn't colliding reset speed
frogger.speed = 0;
// frogger got to end bank (goal every 3 cols)
const col = (frogger.x + grid / 2) / grid | 0;
if (froggerRow === 0 && col % 3 === 0 &&
// check to see if there isn't a scored frog already there
!scoredFroggers.find(frog => frog.x === col * grid)) {
scoredFroggers.push(new Sprite({
...frogger,
x: col * grid,
y: frogger.y + 5
}));
}
// reset frogger if not on obstacle in river
if (froggerRow < rows.length / 2 - 1) {
frogger.x = grid * 6;
frogger.y = grid * 13;
}
}
}
// listen to keyboard events to move frogger
document.addEventListener('keydown', function(e) {
// left arrow key
if (e.which === 37) {
frogger.x -= grid;
}
// right arrow key
else if (e.which === 39) {
frogger.x += grid;
}
// up arrow key
else if (e.which === 38) {
frogger.y -= grid;
}
// down arrow key
else if (e.which === 40) {
frogger.y += grid;
}
// clamp frogger position to stay on screen
frogger.x = Math.min( Math.max(0, frogger.x), canvas.width - grid);
frogger.y = Math.min( Math.max(grid, frogger.y), canvas.height - grid * 2);
});
// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>