forked from Hello1024/TheChase
/
game.js
executable file
·431 lines (374 loc) · 12.2 KB
/
game.js
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
424
425
426
427
428
429
430
431
/* Local Vars:
* models : Object[] --
* lengths : Object[] --
* rows : Int[] -- y-coords of rows starting with first traffic row
* context : Canvas Context -- used for drawing to main canvas.
*/
var lengths = [{width: 179, height: 21}, {width: 118, height: 21}, {width: 85, height: 22}];
var rows = [473-271, 443-271, 413-271, 383-271, 353-271, 323-271, 0];
var context = null;
var show_rules = function() {
document.getElementById('start_image').style.visibility="hidden";
document.getElementById('rules_image').style.visibility="visible";
}
var start_game = function() {
document.getElementById('rules_image').style.visibility="hidden";
game = new Game();
$(document).keydown(function(e) {
if (game.paused) return;
var arrow_key = get_arrow_key(e);
if (arrow_key) {
e.preventDefault();
}
if (game.dead === -1 && game.lives > 0) {
if (arrow_key === 'u'){
up();
} else if (arrow_key === 'd'){
down();
} else if (arrow_key === 'l'){
left();
} else if (arrow_key === 'r'){
right();
}
}
});
board = document.getElementById('game');
context = board.getContext('2d');
new Audio('Sirens.mp3').play();
sprites = new Image();
deadsprite = new Image();
sprites.src = 'assets/thechase_sprites_v2.png';
deadsprite.src = 'assets/dead_frog.png';
sprites.onload = function() {
draw_bg();
draw_info();
make_cars();
draw_frog();
setInterval(game_loop, 50);
};
};
var game_loop = function() {
if (!game.paused) {
draw_bg();
draw_info();
draw_cars();
draw_police();
if (game.lives > 0) {
game.score += 1;
draw_frog();
} else {
game_over();
}
}
};
var get_arrow_key = function(e) {
/*
Args:
e -- event
Returns: the name of the arrow key that was pressed when a key is pressed or null.
*/
switch(e.keyCode) {
case 37:
return 'l';
case 38:
return 'u';
case 39:
return 'r';
case 40:
return 'd';
}
return null;
};
//drawer functions: bg, info, frogger, cars
var draw_bg = function() {
context.fillStyle='#D8D5C4';
context.fillRect(0,0,399,344);
//context.drawImage(sprites, 0, 0, 399, 113, 0, 0, 399, 113);
context.drawImage(sprites, 0, 119, 399, 34, 0, 0, 399, 34);
context.drawImage(sprites, 0, 119, 399, 34, 0, 495-271, 399, 34);
};
var draw_info = function() {
draw_lives();
context.fillStyle = '#600000';
context.font = 'bold 10pt arial';
context.fillText('Score: ', 4, 560-271);
context.fillText('Highscore: ', 200, 560-271);
draw_score();
};
var draw_lives = function() {
var x = 4;
var y = 532-271;
if ((game.score - (game.extra * 10000)) >= 10000 && game.lives < 4) {
game.extra++;
}
for (var i = 0; i<(game.lives + game.extra); i++){
context.drawImage(sprites, 13, 334, 17, 23, x, y, 11, 15);
x += 14;
}
};
var draw_level = function() {
context.font = 'bold 15pt arial';
context.fillText(game.level, 131, 545-271);
};
var draw_score = function() {
context.font = 'bold 10pt arial';
context.fillText(game.score, 49, 560-271);
if (window.localStorage['highscore']) {
highscore = localStorage['highscore'];
} else highscore = 0;
context.fillText(highscore, 272, 560-271);
};
var draw_frog = function() {
if (game.dead > 0) {
// @4,2 ; 19x24
context.drawImage(deadsprite, 4, 2, 19, 24, game.posX, game.posY, 19, 24);
game.dead--;
}
else if (game.dead === 0) {
game.reset();
}
else if (car_collision()) {
hit_car();
}
// police collision
else if (game.posX < game.policePosX + 30) {
sploosh();
}
else {
if (game.facing === 'u') {
context.drawImage(sprites, 12, 369, 23, 17, game.posX, game.posY, 23, 17);
game.width = 23, game.height = 17;
}
else if (game.facing === 'd') {
context.drawImage(sprites, 80, 369, 23, 17, game.posX, game.posY, 23, 17);
game.width = 23, game.height = 17;
}
else if (game.facing === 'l') {
context.drawImage(sprites, 80, 335, 19, 23, game.posX, game.posY, 19, 23);
game.width = 19, game.height = 23;
}
else if (game.facing === 'r') {
context.drawImage(sprites, 12, 335, 19, 23, game.posX, game.posY, 19, 23);
game.width = 19, game.height = 23;
}
}
};
var draw_police = function() {
context.drawImage(sprites, 230, 315, 100, 180, game.policePosX, 473-150-271, 100, 180);
};
var draw_cars = function() {
for (var i=0; i<cars.length; i++) {
cars[i].move();
if (cars[i].out_of_bounds()) {
cars[i] = make_car(cars[i].lane, null, cars[i].model);
}
if (!cars[i].invisible)
cars[i].draw();
}
};
var game_over = function() {
if (game.score >= highscore) {
localStorage['highscore'] = game.score;
context.font = 'bold 32pt arial';
context.fillStyle = '#922';
context.fillText('HIGHSCORE!', 6, 280);
}
document.getElementById('game_over_image').style.display = 'block';
document.getElementById('game').style.opacity = '0.3';
};
//move
var up = function() {
if (bounds_check(game.posX, game.posY-30)) {
game.posY -= 30;
game.current++;
}
game.facing = 'u';
};
var down = function() {
if (bounds_check(game.posX, game.posY+30)) {
game.posY += 30;
game.current--;
}
game.facing = 'd';
};
var left = function() {
if (bounds_check(game.posX-30, game.posY)) game.posX -= 30;
game.facing = 'l';
};
var right = function() {
if (bounds_check(game.posX+30, game.posY)) game.posX += 30;
game.facing = 'r';
};
var bounds_check = function(x, y) {
if (y > 300-271 && y < 480-271 && x > 0 && x < 339) {
return true;
}
return false;
};
var collides = function(x1, y1, w1, h1, x2, y2, w2, h2) {
return (((x1 <= x2+w2 && x1 >=x2) && (y1 <= y2+h2 && y1 >= y2)) ||
((x1+w1 <= x2+w2 && x1+w1 >= x2) && (y1 <= y2+h2 && y1 >= y2)) ||
((x1 <= x2+w2 && x1 >=x2) && (y1+h1 <= y2+h2 && y1+h1 >= y2)) ||
((x1+w1 <= x2+w2 && x1+w1 >= x2) && (y1+h1 <= y2+h2 && y1+h1 >= y2)));
}
var car_collision = function() {
if (game.posY < 505-271 && game.posY > 270-271) {
for (var i=0; i<cars.length; i++) {
if (!cars[i].invisible && collides(
game.posX,
game.posY,
game.width,
game.height,
cars[i].posX,
cars[i].posY,
cars[i].width,
cars[i].height)) {
// we can't hit more stuff till the old one is resolved
if (game.last_hit) return false;
game.last_hit = cars[i];
return true;
}
}
}
game.last_hit = false;
return false;
};
var sploosh = function() {
game.lives--;
game.dead = 20;
new Audio('Prison Cell Door sound effect.mp3').play();
};
var hit_car = function() {
game.paused = true;
document.getElementById('questionimg').src = models[game.last_hit.model].file;
document.getElementById('question').style.display = 'block';
document.getElementById('answerbox').value = '';
document.getElementById('answerbox').focus();
setTimeout(() => {
document.getElementById('question').style.display = 'none';
var answer = document.getElementById('answerbox').value;
document.getElementById('answerbox').blur();
if (models[game.last_hit.model].answer) {
if (answer == models[game.last_hit.model].answer ) {
// Correct
game.policePosX -= 3;
game.score += answer.length*200;
} else {
// wrong
game.policePosX += 30;
}
}
game.last_hit.invisible = true;
game.paused = false;
}, 7000);
};
// object initializers - cars
var make_cars = function() {
cars = [];
for (var i=0; i < 30; i++) {
cars.push(make_random_car());
}
cars.forEach(c => c.invisible=true);
};
var make_random_car = function() {
return make_car(Math.floor(Math.random()*6), Math.random()*399, Math.floor(Math.random()*models.length));
};
var make_car = function(row, x, model) {
return new Car(x==null?399:x, rows[row], row, model==null?1:model);
};
/* game "classes" - game, car
* Car models:
* 0: pink sedan
* 1: white sedan
* 2: yellow sedan
* 3: white bulldozer
* 4: white truck
*/
var Car = function(x, y, lane, model) {
this.posX = x;
this.posY = y;
this.lane = lane;
this.speed = Math.random()+0.1;
this.model = model;
this.width = models[model].width;
this.height = models[model].height;
this.move = function() {
this.posX = this.posX - (models[model].dir * this.speed * (game.posX/100));
};
this.draw = function() {
if (models[this.model].image) {
context.drawImage(models[this.model].image, this.posX, this.posY-8, 40, 30);
} else {
models[this.model].image = new Image();
models[this.model].image.src = models[this.model].file;
}
};
this.out_of_bounds = function() {
return ((this.posX + this.width) < 0);
};
};
var Game = function() {
this.lives = 3;
this.extra = 0;
this.level = 1;
this.score = 0;
this.policePosX = -50;
this.reset = function () {
this.posY = 473-271;
this.posX = 187;
this.facing = 'u';
this.current = -1;
this.highest = -1;
this.dead = -1;
}
this.paused = false;
this.reset();
}
window.addEventListener('load', function(){
var touchsurface = document.getElementById('game_div'),
startX,
startY,
Xdist,
Ydist,
threshold = 100, //required min distance traveled to be considered swipe
allowedTime = 400, // maximum time allowed to travel that distance
elapsedTime,
startTime
function handleswipe(swipeleftBol, swiperightBol, swipeupBol, swipedownBol){
if (game.dead === -1 && game.lives > 0) {
if (swipeupBol){
up();
} else if (swipedownBol){
down();
} else if (swipeleftBol){
left();
} else if (swiperightBol){
right();
}
}
}
touchsurface.addEventListener('touchstart', function(e){
var touchobj = e.changedTouches[0]
dist = 0
startX = touchobj.pageX
startY = touchobj.pageY
startTime = new Date().getTime() // record time when finger first makes contact with surface
e.preventDefault();
}, false)
touchsurface.addEventListener('touchmove', function(e){
e.preventDefault() // prevent scrolling when inside DIV
}, false)
touchsurface.addEventListener('touchend', function(e){
var touchobj = e.changedTouches[0]
Xdist = touchobj.pageX - startX // get total dist traveled by finger while in contact with surface
Ydist = touchobj.pageY - startY // get total dist traveled by finger while in contact with surface
elapsedTime = new Date().getTime() - startTime // get time elapsed
// check that elapsed time is within specified, horizontal dist traveled >= threshold, and vertical dist traveled <= 100
var swiperightBol = (elapsedTime <= allowedTime && Xdist >= threshold && Math.abs(Ydist) <= 50);
var swipeleftBol = (elapsedTime <= allowedTime && Xdist <= -threshold && Math.abs(Ydist) <= 50);
var swipedownBol = (elapsedTime <= allowedTime && Ydist >= threshold && Math.abs(Xdist) <= 50);
var swipeupBol = (elapsedTime <= allowedTime && Ydist <= -threshold && Math.abs(Xdist) <= 50);
handleswipe(swipeleftBol, swiperightBol, swipeupBol, swipedownBol)
e.preventDefault()
}, false)
}, false) // end window.onload