mirror of
https://github.com/danbulant/bobek
synced 2026-07-05 19:10:51 +00:00
bird rotation
This commit is contained in:
parent
1d6c48dd93
commit
73ba70e25b
2 changed files with 30 additions and 8 deletions
38
game.js
38
game.js
|
|
@ -1,3 +1,4 @@
|
||||||
|
const RAD = Math.PI/180;
|
||||||
const scrn = document.getElementById('canvas');
|
const scrn = document.getElementById('canvas');
|
||||||
const sctx = scrn.getContext("2d");
|
const sctx = scrn.getContext("2d");
|
||||||
scrn.addEventListener("click",()=>{
|
scrn.addEventListener("click",()=>{
|
||||||
|
|
@ -10,7 +11,6 @@
|
||||||
break;
|
break;
|
||||||
case state.gameOver :
|
case state.gameOver :
|
||||||
state.curr = state.getReady;
|
state.curr = state.getReady;
|
||||||
bird.y = 100;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -70,31 +70,42 @@
|
||||||
{sprite : new Image()},
|
{sprite : new Image()},
|
||||||
{sprite : new Image()},
|
{sprite : new Image()},
|
||||||
],
|
],
|
||||||
x : 13,
|
rotatation : 0,
|
||||||
|
x : 50,
|
||||||
y :100,
|
y :100,
|
||||||
speed : 0,
|
speed : 0,
|
||||||
gravity : .25,
|
gravity : .125,
|
||||||
|
thrust : 3.6,
|
||||||
frame:0,
|
frame:0,
|
||||||
draw : function() {
|
draw : function() {
|
||||||
sctx.drawImage(this.animations[this.frame].sprite,this.x,this.y);
|
let h = this.animations[this.frame].sprite.height;
|
||||||
|
let w = this.animations[this.frame].sprite.width;
|
||||||
|
sctx.save();
|
||||||
|
sctx.translate(this.x,this.y);
|
||||||
|
sctx.rotate(this.rotatation*RAD);
|
||||||
|
sctx.drawImage(this.animations[this.frame].sprite,-w/2,-h/2);
|
||||||
|
sctx.restore();
|
||||||
},
|
},
|
||||||
update : function() {
|
update : function() {
|
||||||
|
console.log(this.speed ,this.rotatation);
|
||||||
switch (state.curr) {
|
switch (state.curr) {
|
||||||
case state.getReady :
|
case state.getReady :
|
||||||
|
bird.y = 100;
|
||||||
|
this.rotatation = 0;
|
||||||
this.y += (frames%10==0) ? (Math.random()-0.5)*2 : 0;
|
this.y += (frames%10==0) ? (Math.random()-0.5)*2 : 0;
|
||||||
this.frame += (frames%10==0) ? 1 : 0;
|
this.frame += (frames%10==0) ? 1 : 0;
|
||||||
break;
|
break;
|
||||||
case state.Play :
|
case state.Play :
|
||||||
this.frame += (frames%5==0) ? 1 : 0;
|
this.frame += (frames%5==0) ? 1 : 0;
|
||||||
this.y += this.speed;
|
this.y += this.speed;
|
||||||
|
this.setRotation()
|
||||||
this.speed += this.gravity;
|
this.speed += this.gravity;
|
||||||
let r = parseFloat( this.animations[0].sprite.height);
|
let r = parseFloat( this.animations[0].sprite.width)/2;
|
||||||
console.log(r);
|
|
||||||
if(this.y + r >= gnd.y)
|
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;
|
||||||
this.speed = 0;
|
this.speed = 0;
|
||||||
break;
|
break;
|
||||||
|
|
@ -102,7 +113,18 @@
|
||||||
this.frame = this.frame%this.animations.length;
|
this.frame = this.frame%this.animations.length;
|
||||||
},
|
},
|
||||||
flap : function(){
|
flap : function(){
|
||||||
this.speed = -4.6;
|
if(this.y > 0)
|
||||||
|
this.speed = -this.thrust;
|
||||||
|
},
|
||||||
|
setRotation : function(){
|
||||||
|
if(this.speed <= 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
this.rotatation = Math.max(-25, -25 * this.speed/(-1*this.thrust));
|
||||||
|
}
|
||||||
|
else if(this.speed > 0 ) {
|
||||||
|
this.rotatation = Math.min(90, 90 * this.speed/(this.thrust*2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const UI = {
|
const UI = {
|
||||||
|
|
|
||||||
BIN
img/gameover.png
BIN
img/gameover.png
Binary file not shown.
|
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 7.8 KiB |
Loading…
Reference in a new issue