-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
426 lines (396 loc) · 10.4 KB
/
index.js
File metadata and controls
426 lines (396 loc) · 10.4 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
//Canvas declarations
const canvas = document.querySelector("canvas");
canvas.width = window.innerWidth ;
canvas.height = window.innerHeight;
console.log(100/canvas.width);
//TODO:::Optimize for mobile responsiveness
addEventListener("resize", function () {
canvas.width = (window.innerWidth);
canvas.height = window.innerHeight;
bird.y=(canvas.height-(grassRatio*canvas.height))/2
gameScreen("gameStart");
});
const c = canvas.getContext("2d");
//
//Audio variable declaration
var collision=document.getElementById("collision");
var fly=document.getElementById("fly");
fly.volume =0.1;
collision.volume=0.3;
//
//Values for adjusting components for different window sizes
const soilRatio = 0.09283819628647215;
const grassRatio = 0.11936339522546419;
const randBuildingRatio=0.5305039787798409;
const fixedBuildingRatio = 0.06631299734748011;
const randIntervalRatio = 0.13262599469496023;
const fixedIntervalRatio=0.09946949602122016;
const obstacleWidthRatio = 0.06510416666666667;
//
//Values related to movement
var v = 3;
var birdv = 5;
var g = 0;
//
//Values related to game parameters
var score=0;
var gameOver=false;
var img = new Image();
var pic = 1;//Index of frame of bird animation to be displayed
var frames = 0;//Keeps track of current frame.
var bestScore=0
var obstacles = [];
var startGame = true;
var strip = { //Start values of canvas pen for drawing stripes
x: 0,
y: canvas.height - soilRatio * canvas.height,
};
var stripes = [];
var startingPoint = 0;
var cont = true;//Controls requestAnimationFrame
//
//Constructors
function Obstacle(x,y,height){
this.x=x;
this.y=y;
this.height=height;
this.crossed=false;
this.draw=function(){
if(this.y==0){
c.fillStyle = "#78CE35";
c.fillRect(this.x,this.y,obstacleWidthRatio*canvas.width,this.height-20);
c.strokeStyle="black";
c.strokeRect(
this.x,
this.y,
obstacleWidthRatio * canvas.width,
this.height - 20
);
c.fillRect(
this.x - 5,
this.height - 20,
obstacleWidthRatio * canvas.width+10,
20
);
c.strokeRect(
this.x - 5,
this.height - 20,
obstacleWidthRatio * canvas.width+10,
20
);
}
if(this.y==canvas.height-grassRatio*canvas.height){
c.fillStyle = "#78CE35";
c.fillRect(this.x,this.y,obstacleWidthRatio*canvas.width,-this.height+20);
c.strokeStyle = "black";
c.strokeRect(
this.x,
this.y,
obstacleWidthRatio * canvas.width,
-this.height + 20
);
c.fillRect(
this.x - 5,
canvas.height - grassRatio * canvas.height - this.height + 20,
obstacleWidthRatio * canvas.width+10,
-20
);
c.strokeRect(
this.x - 5,
canvas.height - grassRatio * canvas.height - this.height + 20,
obstacleWidthRatio * canvas.width+10,
-20
);
}
}
}
function Bird(x,y,dy,index){
this.x=x;
this.y=y;
this.dy=dy;
this.index=index;
this.g=g
this.draw=function(){ //Draws frame of bird animation according to picture index
img.src = `./images/sprites/Frame-${this.index}.png`;
var yval=this.y;
img.onload=function(){
c.drawImage(img,canvas.width/2,yval,40,40);
}
}
this.update=function(){
this.draw();
this.y+=this.dy;
this.dy+=this.g;
}
}
function Cloud(x,y){
this.x=x;
this.y=y;
this.draw=function(){
var xval=this.x;
var yval=this.y;
img.src = "./images/cloud.png";
img.onload=function(){
c.drawImage(img,300,400,120,120);
c.fillStyle="white";
c.fill();
}
}
}
function Stripe(x, y, dx) {
this.x = x;
this.y = y;
this.dx = dx;
this.draw = function () {
c.beginPath();
c.moveTo(this.x, canvas.height - soilRatio * canvas.height);
c.lineTo(
this.x + 15,
canvas.height -
soilRatio * canvas.height -
(grassRatio - soilRatio) * canvas.height
);
c.shadowBlur = 0;
c.strokeStyle = "black";
c.stroke();
c.closePath();
};
this.update = function () {
this.draw();
this.x -= v;
};
}
//
//Bird Creation
var bird = new Bird(canvas.width / 2, canvas.height / 2, 0, pic);
//
//Function For Creating Obstacles
function obstacleInit() { //Pushes an obstacle pair into obstacles array
var interval = Math.random() * 80 + 100;//Gap between two obstacles
var x = canvas.width + 200;//All obstacles are created at same spot
var h1 =
Math.random() * randBuildingRatio * canvas.height +
fixedBuildingRatio * canvas.height;
var h2 = canvas.height - grassRatio * canvas.height - interval - h1;
var o1 = new Obstacle(x, 0, h1);
var o2 = new Obstacle(x, canvas.height - grassRatio * canvas.height, h2);
obstacles.push(o1);
obstacles.push(o2);
}
//
//Event handler for movement of bird and starting of game
addEventListener("keydown",function(event){
if(event.which==32){ //Spacebar
//Animate function only called when startGame is true and spacebar is hit
//startGame is true only on load and when gameOver
//Reset of game on startGame
if(startGame){
score=0;
obstacles=[];
cont =true;
v = 3;
bird.y = canvas.height / 2;
birdv = 5;
bird.dy = 5;
bird.g = 0;
g = 0;
gameOver=false
animate();
startGame=false;//Ensures spacebar doesnt reset game
}
}
if(event.key=="ArrowUp"||event.key=="w"||event.which==32){
fly.pause()
fly.currentTime=0
bird.dy=-birdv;
bird.g = 0.25;
fly.play();
}
});
//
//Function for creating stripes
//Continually creates stripes at intervals of 30 upto a length of canvas.width+15
function stripeInit(){
while(strip.x<startingPoint+canvas.width+15){
var stripe = new Stripe(strip.x, strip.y, v);
stripes.push(stripe);
strip.x+=30
}
startingPoint=strip.x;
}
//
//Animate Function
function animate() {
if(cont){
requestAnimationFrame(animate);
}
setTimeout(function () {
gameScreen("")
c.font = "50px Cursive";
c.fillStyle = "white";
c.strokeStyle = "black";
c.fillText(`${score}`, bird.x, 100);
c.strokeText(`${score}`, bird.x, 100);
if (gameOver) {
//Text that displays only when gameOver
c.font = "40px Cursive";
c.fillText("Hit SpaceBar To ReStart!", bird.x - 200, 300);
c.strokeText("Hit SpaceBar To ReStart!", bird.x - 200, 300);
startGame = true;
}
bird.update();
}, 0);
if(frames==0||frames%100==0){ //Every 100 frames new stripes are formed
stripeInit();
}
if(frames==0||frames%200==0){
//Every 100 frames new obstacles are formed
obstacleInit();
}
if(cont){
//Every 10 frames bird is updated to show its next frame
if (frames == 0 || frames % 15 == 0) {
if(pic<=3){
pic+=1;
}
if(pic==4){
pic=1;
}
bird.index=pic;
}
}
// setTimeout(function () {
// //Score Display
// c.font="50px Cursive";
// c.fillStyle="white"
// c.strokeStyle="black"
// c.fillText(`${score}`,bird.x,100);
// c.strokeText(`${score}`, bird.x, 100);
// if(gameOver){ //Text that displays only when gameOver
// c.font = "40px Cursive";
// c.fillText("Hit SpaceBar To ReStart!", bird.x - 200, 300);
// c.strokeText("Hit SpaceBar To ReStart!", bird.x - 200, 300);
// startGame=true;
// }
// bird.update();
// }, 0);
//Collision Checking for each obstacle
for(var i=0;i<obstacles.length;i++){
if(bird.x>=obstacles[i].x&&bird.x<=obstacles[i].x+100){
if (obstacles[i].y == 0) {
if (
bird.y >= obstacles[i].y &&
bird.y <= obstacles[i].y + obstacles[i].height
) {
onGameOver();
}
} else {
if(bird.y>=obstacles[i].y-obstacles[i].height-40&&bird.y<=obstacles[i].y){
onGameOver()
}
}
}
if(!obstacles[i].crossed&&bird.x>obstacles[i].x+100){
score+=0.5;//Each obstacle crossed adds 0.5 to score.Since obstacles come in
//pairs,score is immediatley updated on screen as +1
obstacles[i].crossed=true;//This parameter ensures that once crossed,
//The obstacle no longer triggers score increment
}
}
//Condition for bird falling onto ground
if(bird.y>canvas.height-grassRatio*canvas.height){
onGameOver();
}
frames += 1;
}
//
//Function for Game over Conditions
function onGameOver(){
//Velocities of bird,Ground and gravity are set to 0.In addition to stopping animation
v = 0;
birdv = 0;
bird.dy = 0;
bird.g = 0;
g = 0;
cont = false;
if (score > bestScore) {
bestScore = score;
}
gameOver = true;
collision.play()
}
//
//Function to display Game screen
function gameScreen(status){
if(status=="gameStart"){
stripeInit();
}
//Canvas code for drawing background
c.shadowBlur = 0;
c.fillStyle = "rgb(14, 196, 200)";
c.fillRect(0, 0, canvas.width, canvas.height);
c.shadowBlur = 0;
c.fillStyle = "#DADA94";
c.fillRect(
0,
canvas.height - soilRatio * canvas.height,
canvas.width,
soilRatio * canvas.height
);
c.fillStyle = "#A3F85F";
c.shadowColor = "black";
c.shadowBlur = 2;
c.fillRect(
0,
canvas.height - grassRatio * canvas.height,
canvas.width,
(grassRatio - soilRatio) * canvas.height
);
c.strokeRect(
0,
canvas.height - grassRatio * canvas.height,
canvas.width,
(grassRatio - soilRatio) * canvas.height
);
//
//Draws And updates obstacles
for (var i = 0; i < obstacles.length; i++) {
if (obstacles[i].x < -120) {
obstacles.splice(i, 1);
i = i - 1;
} else {
obstacles[i].x -= v;
obstacles[i].draw();
}
}
//Draws and updates stripes
for (var i = 0; i < stripes.length; i++) {
if (stripes[i].x < -5) {
stripes.splice(i, 1);
i = i - 1;
} else {
stripes[i].update();
}
}
if(status=="gameStart"){
bird.draw()
//Canvas code for starting text
c.font = "50px Cursive";
c.fillStyle = "white";
c.strokeStyle = "black";
c.fillText(`${score}`, bird.x, 100);
c.strokeText(`${score}`, bird.x, 100);
c.font = "40px Cursive";
c.fillStyle = "white";
c.strokeStyle = "black";
c.fillText("Hit SpaceBar To Start!",bird.x-200,300);
c.strokeText("Hit SpaceBar To Start!", bird.x-200, 300);
}
c.font = "30px Cursive";
c.fillStyle = "azure";
c.strokeStyle = "black";
c.fillText(`Best:${bestScore}`,30,50);
}
//
//Game Screen On page load without animation
gameScreen("gameStart");