-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappyBird.js
111 lines (79 loc) · 2.27 KB
/
flappyBird.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
var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");
// images
var bird = new Image();
var bg = new Image();
var fg = new Image();
var pipeNorth = new Image();
var pipeSouth = new Image();
bg.src = "images/bg.png";
pipeNorth.src = "images/pipeNorth.png";
pipeSouth.src = "images/pipeSouth.png";
fg.src = "images/fg.png";
bird.src = "images/bird.png";
// variables
var gap = 90;
var constant;
pipeNorth.height = 242
constant = pipeNorth.height + gap;
var bX = 10;
var bY = 150;
var gravity = 1.5;
var score = 0;
// audio
var fly = new Audio();
var scor = new Audio();
fly.src = "sounds/fly.mp3";
scor.src = "sounds/score.mp3";
// on keydown
document.addEventListener("keydown", moveUp);
document.querySelector(".red-button").addEventListener("click", moveUp);
document.querySelector("#canvas").addEventListener("click", moveUp);
function moveUp() {
bY -= 30;
fly.play();
}
// pipe coordinates
var pipe = [];
pipe[0] = {
x: cvs.width,
y: 0
}
// draw images
function imageDrawer(img, x, y) {
//img.onload = function() {
ctx.drawImage(img, x, y);
//}
}
function draw() {
imageDrawer(bg, 0, 0);
for(var i = 0; i < pipe.length; i++) {
imageDrawer(pipeNorth, pipe[i].x, pipe[i].y);
imageDrawer(pipeSouth, pipe[i].x, pipe[i].y+constant);
pipe[i].x--;
if(pipe[i].x == 125) {
pipe.push({
x: cvs.width,
y: Math.floor(Math.random()*pipeNorth.height-pipeNorth.height)
});
}
// detect collision
if(bX + bird.width >= pipe[i].x && bX <= pipe[i].x + pipeNorth.width
&& (bY <= pipe[i].y + pipeNorth.height || bY + bird.height >= pipe[i].y + constant)
|| bY + bird.height >= cvs.height - fg.height) {
location.reload(); //reload the page
}
if(pipe[i].x == 5) {
score++;
scor.play();
}
}
imageDrawer(fg, 0, cvs.height-fg.height);
imageDrawer(bird, bX, bY);
bY += gravity;
ctx.fillStyle = "#000";
ctx.font = "20px Helvetica"
ctx.fillText("Score: " + score, 10, cvs.height - 20);
requestAnimationFrame(draw);
}
draw();