pipes done

This commit is contained in:
Darkin 2019-10-19 17:40:41 +02:00
parent 54e73b692a
commit c1c92e1d8e

51
game.js
View file

@ -13,13 +13,14 @@
state.curr = state.getReady; state.curr = state.getReady;
bird.y = 100; bird.y = 100;
pipe.pipes=[]; pipe.pipes=[];
score = 0;
break; break;
} }
}) })
sctx.fillStyle = "#30c0df"; sctx.fillStyle = "#30c0df";
let frames = 0; let frames = 0;
let dx = 2; let dx = 2;
let score = 0;
const state = { const state = {
curr : 0, curr : 0,
getReady : 0, getReady : 0,
@ -77,12 +78,15 @@
if(state.curr!=state.Play) return; if(state.curr!=state.Play) return;
if(frames%100==0) if(frames%100==0)
{ {
console.log("lol");
this.pipes.push({x:parseFloat(scrn.width),y:-210*Math.min(Math.random()+1,1.8)}); this.pipes.push({x:parseFloat(scrn.width),y:-210*Math.min(Math.random()+1,1.8)});
} }
this.pipes.forEach(pipe=>{ this.pipes.forEach(pipe=>{
pipe.x -= dx; pipe.x -= dx;
}) })
if(this.pipes.length&&this.pipes[0].x < -this.top.sprite.width)
{
this.pipes.shift();
}
} }
@ -112,6 +116,7 @@
sctx.restore(); sctx.restore();
}, },
update : function() { update : function() {
let r = parseFloat( this.animations[0].sprite.width)/2;
switch (state.curr) { switch (state.curr) {
case state.getReady : case state.getReady :
this.rotatation = 0; this.rotatation = 0;
@ -123,14 +128,24 @@
this.y += this.speed; this.y += this.speed;
this.setRotation() this.setRotation()
this.speed += this.gravity; this.speed += this.gravity;
let r = parseFloat( this.animations[0].sprite.width)/2; if(this.y + r >= gnd.y||this.collisioned())
if(this.y + r >= gnd.y)
state.curr = state.gameOver; state.curr = state.gameOver;
break; break;
case state.gameOver : case state.gameOver :
this.rotatation = 90;
this.frame = 1; this.frame = 1;
if(this.y + r < gnd.y) {
console.log(this.y+r,gnd.y,this.speed,this.gravity);
this.y += this.speed;
this.setRotation()
this.speed += this.gravity*2;
}
else {
this.speed = 0; this.speed = 0;
this.y=gnd.y-r;
this.rotatation=90;
}
break; break;
} }
this.frame = this.frame%this.animations.length; this.frame = this.frame%this.animations.length;
@ -147,6 +162,30 @@
} }
else if(this.speed > 0 ) { else if(this.speed > 0 ) {
this.rotatation = Math.min(90, 90 * this.speed/(this.thrust*2)); this.rotatation = Math.min(90, 90 * this.speed/(this.thrust*2));
}
},
collisioned : function(){
if(!pipe.pipes.length) return;
let bird = this.animations[0].sprite;
let x = pipe.pipes[0].x;
let y = pipe.pipes[0].y;
let r = bird.height/4 +bird.width/4;
let roof = y + parseFloat(pipe.top.sprite.height);
let floor = roof + pipe.gap;
let w = parseFloat(pipe.top.sprite.width);
if(this.x + r>= x)
{
if(this.x + r < x + w)
{
if(this.y - r <= roof || this.y + r>= floor)
{
return true;
}
}
} }
} }
}; };