-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappybird.html
198 lines (178 loc) · 3.83 KB
/
flappybird.html
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
<!DOCTYPE html>
<html>
<head>
<title>Flappy Bird</title>
</head>
<body>
<canvas id="cnv" width="500" height="500"></canvas>
<h3 style="color: blue;">Score: <span id="score">0</span></h3>
<h3 style="color: red; display: none;" id="gameover">Game Over. Press Enter to Restart</h3>
<h3>Use space bar to jump. Enter to restart. Shift to end game.</h3>
<script type="text/javascript">
var canvas = document.getElementById("cnv");
var ctx = canvas.getContext("2d");
var gameEnded = false;
var score = 0;
var bird = {
x: 50,
y: 100,
vy: 2,
radius: 10,
color: 'green',
draw: function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
};
var pipe = {
x: 450,
y: 0,
bottomy:0,
w: 40,
h: 0,
diff: 80,
bottomh:0,
color: 'white',
draw: function() {
ctx.beginPath();
ctx.fillStyle=this.color;
ctx.fillRect(this.x,this.y,this.w,this.h);
ctx.fillRect(this.x,this.bottomy,this.w,this.bottomh);
ctx.closePath();
}
};
var pipes = [];
/**
* Draw the bird
*/
function drawBird(){
bird.draw();
if(bird.y <= 480){
bird.y += bird.vy;
}
}
/**
*
*/
function hop(){
if(bird.y >=20){
bird.y -= 30;
}
}
/**
* Draw the pipes
*/
function drawPipes(){
for(var i=0;i<pipes.length;i++){
var tempPipe = pipes[i];
tempPipe.draw();
if(checkCollision(tempPipe)){
stop();
break;
}
tempPipe.x --;
if(tempPipe.x == 0 ){
score ++;
}
if(tempPipe.x==300){
createPipes();
}
}
}
/**
* Check collisios
*/
function checkCollision(tempPipe){
if(bird.x+bird.radius >= tempPipe.x && bird.x <= tempPipe.x+tempPipe.w){
//&& (bird.y <= tempPipe.y + tempPipe.h || bird.y + bird.radius >= ){
if(bird.y-bird.radius <= tempPipe.y+tempPipe.h
|| bird.y+bird.radius >= tempPipe.h+tempPipe.diff){
tempPipe.color = "red";
tempPipe.draw();
return true;
}
}
}
/**
* Create moving Pipes of random sizes
*/
function createPipes(){
var pipeTemp = Object.create(pipe);
pipeTemp.h = Math.floor(Math.random()*350+1);
pipeTemp.bottomy = pipeTemp.h + pipeTemp.diff;
pipeTemp.bottomh = canvas.width-(pipeTemp.h+pipeTemp.diff);
console.log(pipeTemp);
pipes.push(pipeTemp);
}
/**
* Clear the canvas
*/
function clearCanvas(){
ctx.beginPath();
ctx.fillStyle="black";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.closePath();
}
/**
* Update the score
*/
function updateScore(){
document.getElementById("score").innerHTML = score;
}
/**
* Main method to start game
*/
function play(){
clearCanvas();
drawBird();
drawPipes();
updateScore();
if(!gameEnded)
interval = window.requestAnimationFrame(play);
}
//add the keydown for arrow key bindings
document.addEventListener('keydown', logKey);
/**
* Add key bindings
*/
function logKey(e) {
switch(e.keyCode){
case 16 : stop(); break; //shift
case 32 : hop(); break; //space
case 13 : restart(); break; //enter
}
}
//create initial one pipe
createPipes();
/**
* The play interval
*/
var interval = window.requestAnimationFrame(play);
/**
* Stop the game
*/
function stop(){
document.getElementById("gameover").style.display = "block";
window.cancelAnimationFrame(interval);
gameEnded = true;
}
/**
* Restart the game
*/
function restart(){
window.cancelAnimationFrame(interval);
pipes = [];
score = 0;
createPipes();
bird.x = 50;
bird.y = 100;
gameEnded = false;
interval = window.requestAnimationFrame(play);
document.getElementById("gameover").style.display = "none";
}
</script>
</body>
</html>