-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
321 lines (270 loc) · 7.45 KB
/
index.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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<title>Flappy Bird Game</title>
<script src="sprite.js"></script>
<style>
canvas {
display: block;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<script>
var
canvas,
ctx,
width,
height,
fgpos=0,
frames=0,
score=0,
best=0,
currentstate,
states={
Splash:0,Game:1,Score:2
},
bird={
x:60,
y:100,
frame:0,
velocity:0,
animation:[0,1,2,1],
rotation:0,
gravity:0.25,
_jump:4.6,
radius: 12,
jump:function(){
this.velocity=-this._jump;
},
update:function(){
var n=(currentstate===states.Splash)?10:5;
this.frame += (frames%n)===0 ? 1:0;
/*For 10 frames bird remains in one picture ie s_bird[0]
If the frame number is multiple of 10 , then frame=frame+1; else frame=frame+0;
hence bird flies slowly.
*/
/*
but as it is not Splash state ,it will change picture after every 5 frames
*/
//console.log("frame no."+frames + " <frame var:>"+this.frame);
this.frame= this.frame %this.animation.length;
/*So that frame don't exceed s_bird[0] s_bird[1] s_bird[2] s_bird[3],so mod it by 4*/
if(currentstate===states.Splash){
this.y=height-280+5*Math.cos(frames/10);
this.rotation=0;
}
else {
this.velocity+=this.gravity;//negativity of velocity is decreasing
//console.log("velocity"+this.velocity);
this.y+=this.velocity;/*till velocity is -ve => y will decreases => bird goes up
then velocity is +ve => y will start increasing => bird goes down */
if(this.y >=height-s_fg.height-10){
this.y=height-s_fg.height-10;
if(currentstate===states.Game)
currentstate=states.Score;
this.velocity=this._jump;//Why have this ??Any Need
}
if(this.velocity>=this._jump){
this.frame=1;
this.rotation=Math.min(Math.PI/6,this.rotation+0.3);
}else{
this.rotation=-0.3;
}
}//else
},
draw:function(ctx){
ctx.save();
ctx.translate(this.x,this.y);
ctx.rotate(this.rotation);
var n=this.animation[this.frame];
s_bird[n].draw(ctx, -s_bird[n].width/2 , -s_bird[n].height/2);
/* create a red circle of radius 12 .
1.> Notice that radius of 12 is exactly bird area
2.> Also that ctx has now translated to x,y position .therfore bird is at 0,0 position
ctx.fillStyle="#f00";
ctx.beginPath();
ctx.arc(0,0,this.radius,0,2*Math.PI);
ctx.fill();*/
ctx.restore();
}
},
pipes={
_pipes:[],
reset:function(){
this._pipes=[];
},
update:function(){
if(frames%100===0){
var _y=height-(s_pipeSouth.height+s_fg.height+120+200*Math.random());
this._pipes.push({
x:500,
y:_y,
width:s_pipeSouth.width,
height:s_pipeSouth.height
});
}
//console.log(this._pipes.length);
for(var i=0,len=this._pipes.length;i<len;i++)
{
var p=this._pipes[i];
//restrict and collision of bird with pipes
//finding (distance of bird from pipe)^2
if(i===0){
score+=(p.x===bird.x )?1:0 ;
var cx=Math.min(Math.max(bird.x,p.x),p.x+p.width);
var cy1=Math.min(Math.max(bird.y,p.y),p.y+p.height);
var cy2=Math.min(Math.max(bird.y,p.y+p.height+80),p.y+2*p.height+80);
var dx=(bird.x)-cx;
var dy1=bird.y-cy1;
var dy2=bird.y-cy2;
var d1=dx*dx + dy1+dy1;
var d2=dx*dx + dy2*dy2;
//console.log("NextF"+frames+"bird x:"+bird.x+" y:"+bird.y);
//console.log(" cx:"+cx+" cy1:"+cy1+" cy2:"+cy2);
//console.log(" dx:"+dx+" dy1:"+dy1+" dy2:"+dy2);
//console.log(" d1:"+d1+" d2:"+d2);
var r=bird.radius+bird.radius;
if(r>d1 || r>d2){
currentstate=states.Score;
}
}
///
p.x-=2;//each pipe is moved left by 2 units
if(p.x<-50)
{
this._pipes.splice(i,1);//pop one element
i--;
len--;
}
}
},//update
draw:function(ctx){
for(var i=0,len=this._pipes.length;i<len;i++){
var p=this._pipes[i];
s_pipeSouth.draw(ctx,p.x,p.y);
s_pipeNorth.draw(ctx,p.x,p.y+80+p.height);
// console.log(p.x +","+ p.y+",frame:"+frames);
}
}
};
function onpress(evt){
switch(currentstate){
case states.Splash:
currentstate=states.Game;
bird.jump();
break;
case states.Game:
bird.jump();
break;
case states.Score:
var mx=evt.offsetX , my=evt.offsetY;
if(mx==null || my==null)
{mx=evt.touches[0].clientX;
my=evt.touches[0].clientY;
}
if(okbtn.x < mx && mx <okbtn.x+okbtn.width &&
okbtn.y < my && my <okbtn.y+okbtn.height )
{
pipes.reset();
score=0;
currentstate=states.Splash;
}
break;
}
}
function main(){
currentstate=states.Splash;
canvas=document.createElement("canvas");
width=window.innerWidth;
height=window.innerHeight;
var evt="touchstart";
if(width>=500)//if window is large.
{width=320;
height=480;
canvas.style.border="1px solid #020";
evt="mousedown";
}
document.addEventListener(evt,onpress);
canvas.width=width;
canvas.height=height;
ctx=canvas.getContext("2d");
document.body.appendChild(canvas);
//Set up Image
var img=new Image();
img.onload=function(){
initSprites(this );
ctx.fillStyle=s_bg.color;//u can use s_bg color only after u init Sprites
okbtn={
x:(width-s_buttons.Ok.width)/2,
y:height-200,
width:s_buttons.Ok.width,
height:s_buttons.Ok.height
}
run();
}
img.src="sheet.png";
}
function run()//run all other methods
{
var loop=function(){
update();
render();
window.requestAnimationFrame(loop,canvas);
}
window.requestAnimationFrame(loop,canvas);
}
function update(){
frames++;
if(currentstate!== states.Score){
fgpos=(fgpos-2)%14;//if you not use % part, then it will finish the fg after some frames.
//console.log(fgpos);
//console.log(fgpos+s_fg.width);
}
else
{ best=Math.max(best,score);}
if(currentstate===states.Game)
{pipes.update();}
bird.update();
}
function render(){
//makeBackgroundImage
//ctx.fillStyle=s_bg.color; //writing here is also ok but above it will have larger scope;
ctx.fillRect(0,0,width,height);//and fill the whole canvas with fillStyle;
s_bg.draw(ctx,0,height-s_bg.height);
s_bg.draw(ctx,s_bg.width,height-s_bg.height);
//drawBird
bird.draw(ctx);
pipes.draw(ctx);
//makeForegroundImage
s_fg.draw(ctx,fgpos,height-s_fg.height);
s_fg.draw(ctx,fgpos+s_fg.width,height-s_fg.height);
var width2=width/2;
if(currentstate===states.Splash){
s_splash.draw(ctx,width2-s_splash.width/2,height-300);
s_text.GetReady.draw(ctx,width2-s_text.GetReady.width/2,height-400);
}
if(currentstate===states.Score){
s_text.GameOver.draw(ctx,width2-s_text.GameOver.width/2,height-400);
s_score.draw(ctx,width2-s_score.width/2,height-340);
s_buttons.Ok.draw(ctx,okbtn.x,okbtn.y);
s_numberS.draw(ctx,width2-47,height-304,score,null,10);
s_numberS.draw(ctx,width2-47,height-262,best,null,10);
}else{//we want score to be in Splash and Game states only !!!not in score state
s_numberB.draw(ctx,null ,20,score,width2,null);
}
}
main();
</script>
</body>
</html>