-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
286 lines (268 loc) · 8.62 KB
/
app.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
// variables for the canvas itself
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// variables for images
var titleImage = new Image();
titleImage.src = "images/title.png";
var playerImage = new Image();
playerImage.src = "images/apeBird.png";
var backgroundImage = new Image();
backgroundImage.src = "images/background.jpg";
var bananaImage = new Image();
bananaImage.src = "images/banana.png";
var bottomPipeImage = new Image();
bottomPipeImage.src = "images/bottomPipe.png";
var topPipeImage = new Image();
topPipeImage.src = "images/topPipe.png";
var gameOverImage = new Image();
gameOverImage.src = "images/gameOver.png";
// numerical values
var scrollingAmount = 0;
var jumpTime = 15;
var spawnInterval = 200;
var counter = 0;
var score = 0;
var refreshRate = 2;
var scoreInterval = 0;
// player object
var player = {x: 80, y: 250, width: 50, height: 50, incrementY : function(amount) {
this.y = this.y + amount;
}};
// jumping boolean
var goingUp = false;
// pipe objects
var bottomPipes = [{x: 900, y: 420, width: 100, height: 150, move : function() {
this.x -= refreshRate;
}}];
var topPipes = [{x: 900, y: 0, width: 100, height: 210, move : function() {
this.x -= refreshRate;
}}];
// banana objects
var bananas = [{x: 940, y: 310, width: 40, height: 40, move : function() {
this.x -= refreshRate;
}}];
// key listener values
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
var upPressed = false;
var enterPressed = false;
// game booleans
var gameStarted = false;
var isGameOver = false;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (gameStarted) {
// draw background
ctx.drawImage(backgroundImage, -1 * scrollingAmount, 0);
ctx.drawImage(backgroundImage, canvas.width - scrollingAmount, 0)
scrollingAmount += 1;
if (scrollingAmount > canvas.width) {
scrollingAmount = 0;
}
// draw player
ctx.drawImage(playerImage, player.x, player.y);
// update player position based on jump
if (upPressed || goingUp) {
if (goingUp) {
if (jumpTime > 13) {
player.incrementY(-9);
} else if (jumpTime > 10) {
player.incrementY(-7);
} else if (jumpTime > 7) {
player.incrementY(-5);
} else if (jumpTime > 4) {
player.incrementY(-3);
} else if (jumpTime == 1) {
player.incrementY(-1);
}
if (player.y < 0) {
player.y = 0;
goingUp = false;
}
// change direction if jumpTime == 0
if (jumpTime != 0) {
jumpTime--;
} else {
goingUp = false;
}
}
} else {
// falling
if (jumpTime >= 13) {
player.incrementY(8);
} else if (jumpTime > 10) {
player.incrementY(7);
} else if (jumpTime > 7) {
player.incrementY(5);
} else if (jumpTime > 4) {
player.incrementY(2);
} else if (jumpTime == 1) {
player.incrementY(1);
}
if (jumpTime != 15) {
jumpTime++;
}
// constrain to boundaries
if (player.y > canvas.height - 50) {
player.y = canvas.height - 50;
}
}
// generate pipes
if (counter == spawnInterval) {
var random = Math.floor((Math.random() * 80)) + 20;
var topPipeHeight = 0 - random;
var bottomPipeHeight = 450 - random;
bottomPipes.push({x: 900, y: bottomPipeHeight, width: 100, height: 150, move : function() {
this.x -= refreshRate;
}});
topPipes.push({x: 900, y: topPipeHeight, width: 100, height: 210, move : function() {
this.x -= refreshRate;
}});
// generate banana
bananas.push({x: 940, y: Math.floor((topPipeHeight + 150 + bottomPipeHeight) / 2), width: 40, height: 40, move : function() {
this.x -= refreshRate;
}})
counter = 0;
}
// draw pipes and banana
bottomPipes.forEach(element => {
element.move();
if (isColliding(element)) {
gameOver();
} else if (element.x < -element.width) {
// remove elements off screen
bottomPipes.splice(element, 1);
}
ctx.drawImage(bottomPipeImage, element.x, element.y)
});
topPipes.forEach(element => {
element.move();
if (isColliding(element)) {
gameOver();
} else if (element.x < -element.width) {
// remove elements off screen
topPipes.splice(element, 1);
}
ctx.drawImage(topPipeImage, element.x, element.y)
});
bananas.forEach(element => {
element.move();
// collect banans you collide with
if (isColliding(element)) {
bananas.splice(element, 1);
score++;
scoreInterval++;
} else if (element.x < -element.width) {
// remove elements off screen
bananas.splice(element, 1);
}
ctx.drawImage(bananaImage, element.x, element.y)
});
ctx.font = "40px Arial";
ctx.fillStyle = "white";
ctx.fillText("" + score, canvas.width / 2 - 20, 50);
counter++;
// make game harder
if (scoreInterval % 25 == 0 && scoreInterval != 0) {
refreshRate++;
scoreInterval = 0;
if (spawnInterval > 5) {
spawnInterval = Math.floor(400 / refreshRate);
}
}
} else if (!isGameOver) {
canvas.width = 500;
ctx.drawImage(titleImage, 0, 0);
ctx.font = "28px Arial";
ctx.fillStyle = "white";
ctx.fillText("Press enter to begin", 130, 440);
} else {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(gameOverImage, 0, 0);
ctx.font = "28px Arial";
ctx.fillStyle = "white";
ctx.fillText("Your Score: " + score, canvas.width / 2 - 105, 4 * canvas.height / 5);
ctx.fillText("Press enter to retry", canvas.width / 2 - 105, 4 * canvas.height / 5 + 30);
}
}
setInterval(draw, 10)
// key listener methods
function keyDownHandler(e) {
if(e.keyCode == 32 || e.key == "ArrowUp") {
upPressed = true;
goingUp = true;
jumpTime = 15;
}
else if(e.key == "Enter" && !gameStarted) {
enterPressed = true;
setup();
canvas.width = 900;
}
}
// handle releasing buttons
function keyUpHandler(e) {
if(e.keyCode == 32 || e.key == "ArrowUp") {
upPressed = false;
}
else if(e.key == "Enter") {
enterPressed = false;
}
}
// collision detection
function isColliding(element) {
// check left corners
if (player.x > element.x && player.x < (element.x + element.width)) {
// check top left corner
if (player.y > element.y && player.y < (element.y + element.height)) {
return true;
}// check bottom left corner
else if ((player.y + player.height) > element.y && (player.y + player.height) < (element.y + element.height)) {
return true;
}
} // check right corners
else if ((player.x + player.width) > element.x && (player.x + player.width) < (element.x + element.width)) {
// check top right corner
if (player.y > element.y && player.y < (element.y + element.height)) {
return true;
}// check bottom right corner
else if ((player.y + player.height) > element.y && (player.y + player.height) < (element.y + element.height)) {
return true;
}
}
return false;
}
// game over screen
function gameOver() {
isGameOver = true;
gameStarted = false;
canvas.width = canvas.height;
}
// sets up initial values
function setup() {
isGameOver = false;
score = 0;
scrollingAmount = 0;
jumpTime = 15;
spawnInterval = 200;
counter = 0;
score = 0;
refreshRate = 2;
scoreInterval = 0;
player = {x: 80, y: 250, width: 50, height: 50, incrementY : function(amount) {
this.y = this.y + amount;
}};
// jumping boolean
goingUp = false;
// pipe objects
bottomPipes = [{x: 900, y: 420, width: 100, height: 150, move : function() {
this.x -= refreshRate;
}}];
topPipes = [{x: 900, y: 0, width: 100, height: 210, move : function() {
this.x -= refreshRate;
}}];
// banana objects
bananas = [{x: 940, y: 310, width: 40, height: 40, move : function() {
this.x -= refreshRate;
}}];
gameStarted = true;
}