-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.js
More file actions
443 lines (371 loc) · 12.9 KB
/
game.js
File metadata and controls
443 lines (371 loc) · 12.9 KB
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
432
433
434
435
436
437
438
439
440
441
442
var canvas = null;
var context = null;
function Initialize()
{
console.log("starting");
canvas = document.getElementById( "gamespace" );
if ( !canvas.getContext )
{
alert( "This browser does not support the canvas element." );
return;
}
context = canvas.getContext( "2d" );
//Detect keypresses for controls
document.onkeydown = takeControl;
document.onkeyup = endControl;
//set up Start Screen
//InitializeGameState("");
}
//////////////////////////////////////////////////////////////
// Define Images for dog, Dropper, Missile, and Explosion
var sploid = [
"images/sploid1.png",
"images/sploid2.png",
"images/sploid3.png",
"images/sploid4.png",
"images/sploid5.png",
"images/sploid6.png",
"images/sploid7.png",
"images/sploid8.png",
"images/sploid9.png",
"images/sploid10.png"
];
var dogFrames = [
"images/dog1.png",
"images/dog2.png",
"images/dog3.png",
"images/dog4.png",
"images/dog5.png",
"images/dog6.png",
"images/dog7.png",
"images/dog8.png",
"images/dog9.png",
"images/dog10.png",
"images/dog11.png",
];
var dropArray = [
["yummy", "images/bone.png", "bone"],
["yummy", "images/ball.png", "ball"],
["yummy", "images/bacon.png", "bacon"],
["yummy", "images/sock.png", "sock"],
["yucky", "images/broccoli.png", "broccoli"],
["yucky", "images/lemon.png", "lemon"],
["yucky", "images/chili.png", "chili"]
// ["yummy", "images/hambone.png", "hambone"]
];
var missileImg = new Image();
missileImg.src = "images/missile.png";
//preload explosion images
var splosion = [];
for(var i = 1; i <= 10; i++){
splosion[i] = new Image();
splosion[i].src = sploid[i-1];
}
//preload dog images
var dogs = [];
for(var i = 0; i < dogFrames.length; i++){
dogs[i] = new Image();
dogs[i].src = dogFrames[i];
}
dogs[dogFrames.length - 1].addEventListener("load", function(){
InitializeGameState("");
});
// reload dropper images
var dropperImages = [];
for(var p = 0; p < dropArray.length; p++){
dropperImages[p] = new Image();
dropperImages[p].src = dropArray[p][1];
}
//////////////////////////////////////////////////////////////
// Define Objects for dog, Dropper, Missile, and Explosion
var missile = function(){
this.me = "missile";
this.img = missileImg;
this.x = 0;
this.y = 0;
}
var Dog = function(){
this.me = "dog";
this.img = dogs[0];
this.x = 0;
this.y =0;
this.curframe = 1;
}
var explosion = function(x,y){
this.frames = splosion;
this.me = "explosion";
this.x = x;
this.y = y;
this.img = {
width: 0,
height: 0
}
this.curframe = 1;
}
var Dropper = function(type, img, name){
this.me = type; //"yummy" or "yucky"
this.img = img;
this.name = name;
this.x = 0;
this.y = 0;
this.side = 0;
}
////////////////////////////////////////
// Define controls
var dirs = {
l: false, // left arrow down?
r: false, // right arrow down?
f: false, // fire key down?
}
function takeControl(e){ // our key down function
if (e.keyCode == 37) dirs.l = true;
if (e.keyCode == 39) dirs.r = true;
if (e.keyCode == 32) dirs.f = true;
if (e.keyCode == 38) fire = true;
}
function endControl(e){ // our key up function
if (e.keyCode == 37) dirs.l = false;
if (e.keyCode == 39) dirs.r = false;
if (e.keyCode == 32) dirs.f = false;
}
/////////////////////////////////////////////////////////////////////////////////
// Define global game variables
var dog, missiles, maxMissiles, missileDelay, dropperDelay, missileSpeed, lastMissile, lastDropper, dogspeed, dropperMaxSpeed, dropperMinDelay, killCount, dodgeCount, dogFrame;
var score = 0;
var gameover = true;
/////////////////////////////////////////////////////////////////////////////////
// Set up the start screen
function InitializeGameState(replay)
{
console.log("running gs");
if(replay !=="again") replay = "";
//Set startup values for replays...
dog = new Dog(); // replace the explosion with a new dog
droppers = [] ; // array to hold droppers
missiles = []; // array to hold missiles
maxMissiles = 2; // the max missiles that can be on the screen at a time
missileDelay = 7; // minimum frame delay between firing missiles
dropperDelay = 90; // frame delay between dropper spawns, goes down as you play
missileSpeed = 6; // pixels a missile travels upward in one frame
lastMissile = 0; // counter to enforce missile delay
lastDropper = 0; //counter to enforce dropper delay
dogspeed = 4; // number of pixels dog can go horizontally in a frame
dropperMaxSpeed = 50; // maximum dropper speed;
dropperMinDelay = 7; // minimum number of frames between dropper spawn
killCount = 0;
dodgeCount = 0;
// set dog in bottom middle
dog.x = (canvas.width - dog.img.width) / 2;
dog.y = (canvas.height - dog.img.height);
context.fillStyle = "#cfebfd";
context.fillRect(0,0,canvas.width,canvas.height);
context.font = "20pt 'Arial Black'";
txt = "Click to play" + replay;
var txtX = context.measureText(txt).width;
context.fillStyle = "#000";
context.fillText(txt, (canvas.width - txtX)/2, 80);
context.fillText(score.toString(), 5, 35);
context.drawImage(dog.img, dog.x, dog.y);
canvas.addEventListener('click',StartGame)
}
/////////////////////////////////////////////////////////////////////////////////
// First frame of game, call Game Loop
function StartGame(){
canvas.removeEventListener('click',StartGame)
score = 0;
context.fillStyle = "#000";
context.fillRect(0,0,canvas.width,canvas.height);
context.drawImage(dog.img, dog.x, dog.y);
gameover = false;
Tick();
}
/////////////////////////////////////////////////////////////////////////////////
// Game Loop
function Tick()
{
//set the background fill and print the score
context.fillStyle = "#cfebfd";
context.fillRect(0,0,canvas.width,canvas.height);
context.fillStyle = "#000";
context.fillText(score.toString(), 5, 35);
//update the positions of the different sprites and check for collisions
updateMissiles();
updatedog();
updateDroppers();
checkCollisions();
//go back for another round
if(!gameover) window.requestAnimFrame(Tick);
}
/////////////////////////////////////////////////////////////////////////////////
// Sprite Updaters
function updateMissiles(){
lastMissile++;
if(dirs.f == true){ // spacebar has been pressed
if((lastMissile > missileDelay) && (missiles.length < maxMissiles)){
missiles.push(initMissile());
lastMissile = 0;
dirs.f = false;
}
}
missiles.forEach(function(shot, index, group){
if(shot.me === "explosion"){
shot = updateExplosion(shot);
if(shot.curframe == 10) {
missiles.remove(index);
}
} else {
shot.y = shot.y - missileSpeed;
if (shot.y < (0 - shot.img.height)) group.remove(index);
context.drawImage(shot.img, shot.x, shot.y);
}
});
}
function initMissile(){ // initializes a new missile object based on dog position
var thismissile = new missile();
thismissile.x = ((dog.x + (dog.img.width/2)) - (thismissile.img.width / 2));
thismissile.y = (canvas.height - thismissile.img.height) - 40;
return thismissile;
}
function updatedog(){
if (dog.me === "explosion") {
updateExplosion(dog);
if(dog.curframe > 10) endGame();
return;
}
if((dirs.l)||(dirs.r)){
dog.curframe++;
if (dog.curframe == dogs.length){
dog.curframe = 0;
}
dog.img = dogs[dog.curframe];
}
if(dirs.l) dog.x -= dogspeed;
if(dirs.r) dog.x += dogspeed;
//make sure we're not going offscreen
if(dog.x < 0) dog.x = 0;
if(dog.x > (canvas.width - dog.img.width)) dog.x = canvas.width - dog.img.width;
// draw the dog
context.drawImage(dog.img, dog.x, dog.y);
}
function updateDroppers(){
// For every 5 droppers killed or dodged, frames between spawn decreases by 1
// Speed of droppers in vertical pixels is (1 / frames between spawn) * 120;
calcdelay = Math.ceil(dropperDelay - ((killCount + dodgeCount)/5));
if(calcdelay < dropperMinDelay) calcdelay = dropperMinDelay;
// increment number of ticks since last spawn. If it's more than the delay, spawn
lastDropper += 1;
if(lastDropper > calcdelay){
// spawn a dropper
lastDropper = 0;
var myrand = Math.floor(Math.random() * (dropArray.length + 1));
if (myrand >= dropArray.length) myrand = myrand - (Math.floor(Math.random() * (dropArray.length - 2)) +1);
var temp = new Dropper(dropArray[myrand][0],dropperImages[myrand],dropArray[myrand][2]);
// set entry point & horizontal speed
temp.y = -15;
temp.x = Math.random() * (canvas.width - (temp.img.width + 15) + 12)
var coinflip = Math.random();
if(coinflip > .5){
temp.side = 0 - (((Math.random() * 15) + 1)/3);
} else {
temp.side = 0 + (((Math.random() * 15) + 1)/3);
}
droppers.push(temp);
}
//move all the droppers in the droppers array this round
var enspeed = (1/calcdelay) * 120;
droppers.forEach(function(dropper, index, dropperList){
if(dropper.me === "explosion"){ // if an dropper has been converted to an explosion, animate it
dropper = updateExplosion(dropper);
if(dropper.curframe == 10) { // if the explosion animation is over, remove the dropper from droppers
dropperList.remove(index);
}
} else {
//update position
dropper.y += enspeed;
dropper.x += dropper.side;
if(dropper.x < 0){
dropper.x = 0;
dropper.side = 0 - dropper.side;
}
if((dropper.x + dropper.img.width) > canvas.width){
dropper.x = canvas.width - dropper.img.width;
dropper.side = 0 - dropper.side;
}
context.drawImage(dropper.img, dropper.x, dropper.y);
//check for falling off screen
if(dropper.y > (canvas.height + 20)) {
if(dropper.me == "yucky"){
score = score - 3;
} else {
score = score - 1;
}
dropperList.remove(index);
dodgeCount++;
}
}
});
}
function updateExplosion(item){
item.curframe = item.curframe + .5;
context.drawImage(item.frames[Math.floor(parseInt(item.curframe))], item.x, item.y);
return item;
}
function checkCollisions(){
droppers.forEach(function(dropper, enindex, dropperList){
if(intersects(dropper.x, dropper.y, dropper.img.width, dropper.img.height, dog.x, dog.y, dog.img.width, dog.img.height)){
killCount++;
if(dropper.me == "yucky"){
droppers[enindex] = new explosion(dropper.x,dropper.y);
dog = new explosion(dog.x+10, dog.y+20)
} else {
score +=5;
dropperList.remove(enindex);
}
}
missiles.forEach(function(shot, index, group){
if((intersects(dropper.x, dropper.y, dropper.img.width, dropper.img.height, shot.x, shot.y, shot.img.width, shot.img.height)) && (dropper.me != "explosion")){
killCount++;
droppers[enindex] = new explosion(dropper.x,dropper.y);
missiles[index] = new explosion(dropper.x + 15,dropper.y + 10);
if(dropper.me == "yucky"){
score += 5;
} else {
score = score - 3;
}
}
});
});
}
function endGame(){
gameover = true;
InitializeGameState(" again");
}
/////////////////////////////////////////////////////////////////////////////////
// Helper Functions
/* Request Animation Frame fallback (thanks Paul Irish) */
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// Array Remove - By John Resig (MIT Licensed)
// used to remove dead/offscreen droppers and missiles from their arrays
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
// detect if two rectangles intersect (borrowed from stack overflow)
function intersects(x1, y1, w1, h1, x2, y2, w2, h2) {
w2 += x2;
w1 += x1;
if (x2 > w1 || x1 > w2) return false;
h2 += y2;
h1 += y1;
if (y2 > h1 || y1 > h2) return false;
return true;
}
Initialize();