mirror of
https://github.com/danbulant/bobek
synced 2026-07-07 12:01:13 +00:00
game states
This commit is contained in:
parent
a73e672ea9
commit
723e97214f
1 changed files with 92 additions and 29 deletions
115
game.js
115
game.js
|
|
@ -1,8 +1,29 @@
|
||||||
const scrn = document.getElementById('canvas');
|
const scrn = document.getElementById('canvas');
|
||||||
const sctx = scrn.getContext("2d");
|
const sctx = scrn.getContext("2d");
|
||||||
|
scrn.addEventListener("click",()=>{
|
||||||
|
switch (state.curr) {
|
||||||
|
case state.getReady :
|
||||||
|
state.curr = state.Play;
|
||||||
|
bird.x = bird.x*4;
|
||||||
|
break;
|
||||||
|
case state.Play :
|
||||||
|
bird.flap();
|
||||||
|
break;
|
||||||
|
case state.gameOver :
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
sctx.fillStyle = "#30c0df";
|
sctx.fillStyle = "#30c0df";
|
||||||
let frames = 0;
|
let frames = 0;
|
||||||
const gnd = {
|
|
||||||
|
const state = {
|
||||||
|
curr : 0,
|
||||||
|
getReady : 0,
|
||||||
|
Play : 1,
|
||||||
|
gameOver : 2,
|
||||||
|
|
||||||
|
}
|
||||||
|
const gnd = {
|
||||||
animations :
|
animations :
|
||||||
[
|
[
|
||||||
{sprite : new Image()},
|
{sprite : new Image()},
|
||||||
|
|
@ -12,15 +33,26 @@ const gnd = {
|
||||||
y :0,
|
y :0,
|
||||||
frame : 0,
|
frame : 0,
|
||||||
draw : function() {
|
draw : function() {
|
||||||
y = parseFloat(scrn.height-this.animations[this.frame].sprite.height);
|
this.y = parseFloat(scrn.height-this.animations[this.frame].sprite.height);
|
||||||
sctx.drawImage(this.animations[this.frame].sprite,this.x,y);
|
sctx.drawImage(this.animations[this.frame].sprite,this.x,this.y);
|
||||||
},
|
},
|
||||||
update : function(){
|
update : function() {
|
||||||
this.frame += (frames%10==0) ? 1 : 0;
|
|
||||||
|
switch (state.curr) {
|
||||||
|
case state.getReady :
|
||||||
|
this.frame += (frames%10==0) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case state.Play :
|
||||||
|
this.frame += (frames%5==0) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case state.gameOver :
|
||||||
|
this.frame = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
this.frame = this.frame%this.animations.length;
|
this.frame = this.frame%this.animations.length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const bg = {
|
const bg = {
|
||||||
sprite : new Image(),
|
sprite : new Image(),
|
||||||
x : 0,
|
x : 0,
|
||||||
y :0,
|
y :0,
|
||||||
|
|
@ -28,18 +60,8 @@ const bg = {
|
||||||
y = parseFloat(scrn.height-this.sprite.height);
|
y = parseFloat(scrn.height-this.sprite.height);
|
||||||
sctx.drawImage(this.sprite,this.x,y);
|
sctx.drawImage(this.sprite,this.x,y);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const gr = {
|
const bird = {
|
||||||
sprite : new Image(),
|
|
||||||
x : 0,
|
|
||||||
y :0,
|
|
||||||
draw : function() {
|
|
||||||
y = parseFloat(scrn.height-this.sprite.height)/2;
|
|
||||||
x = parseFloat(scrn.width-this.sprite.width)/2;
|
|
||||||
sctx.drawImage(this.sprite,x,y);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const bird = {
|
|
||||||
animations :
|
animations :
|
||||||
[
|
[
|
||||||
{sprite : new Image()},
|
{sprite : new Image()},
|
||||||
|
|
@ -49,27 +71,67 @@ const bird = {
|
||||||
],
|
],
|
||||||
x : 13,
|
x : 13,
|
||||||
y :100,
|
y :100,
|
||||||
|
speed : 0,
|
||||||
|
gravity : .25,
|
||||||
frame:0,
|
frame:0,
|
||||||
draw : function() {
|
draw : function() {
|
||||||
sctx.drawImage(this.animations[this.frame].sprite,this.x,this.y);
|
sctx.drawImage(this.animations[this.frame].sprite,this.x,this.y);
|
||||||
},
|
},
|
||||||
update : function() {
|
update : function() {
|
||||||
this.frame += (frames%10==0) ? 1 : 0;
|
|
||||||
|
switch (state.curr) {
|
||||||
|
case state.getReady :
|
||||||
|
this.y += (frames%10==0) ? (Math.random()-0.5)*2 : 0;
|
||||||
|
this.frame += (frames%10==0) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case state.Play :
|
||||||
|
this.frame += (frames%5==0) ? 1 : 0;
|
||||||
|
this.y += this.speed;
|
||||||
|
this.speed += this.gravity;
|
||||||
|
let r = parseFloat( this.animations[0].sprite.height);
|
||||||
|
console.log(r);
|
||||||
|
if(this.y + r > gnd.y)
|
||||||
|
state.curr = state.gameOver;
|
||||||
|
break;
|
||||||
|
case state.gameOver :
|
||||||
|
this.frame = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
this.frame = this.frame%this.animations.length;
|
this.frame = this.frame%this.animations.length;
|
||||||
this.y += (frames%10==0) ? (Math.random()-0.5)*2 : 0;
|
},
|
||||||
|
flap : function(){
|
||||||
|
this.speed = -4.6;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const UI = {
|
||||||
|
getReady : {sprite : new Image()},
|
||||||
|
gameOver : {sprite : new Image()},
|
||||||
|
draw : function() {
|
||||||
|
switch (state.curr) {
|
||||||
|
case state.getReady :
|
||||||
|
curr = this.getReady;
|
||||||
|
break;
|
||||||
|
case state.gameOver :
|
||||||
|
curr = this.gameOver;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
y = parseFloat(scrn.height-curr.sprite.height)/2;
|
||||||
|
x = parseFloat(scrn.width-curr.sprite.width)/2;
|
||||||
|
sctx.drawImage(curr.sprite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
gnd.animations[0].sprite.src="img/ground/g0.png";
|
gnd.animations[0].sprite.src="img/ground/g0.png";
|
||||||
gnd.animations[1].sprite.src="img/ground/g1.png";
|
gnd.animations[1].sprite.src="img/ground/g1.png";
|
||||||
bg.sprite.src="img/BG.png";
|
bg.sprite.src="img/BG.png";
|
||||||
gr.sprite.src="img/getready.png";
|
UI.getReady.sprite.src="img/getready.png";
|
||||||
|
UI.gameOver.sprite.src="img/gameOver.png";
|
||||||
bird.animations[0].sprite.src="img/bird/b0.png";
|
bird.animations[0].sprite.src="img/bird/b0.png";
|
||||||
bird.animations[1].sprite.src="img/bird/b1.png";
|
bird.animations[1].sprite.src="img/bird/b1.png";
|
||||||
bird.animations[2].sprite.src="img/bird/b2.png";
|
bird.animations[2].sprite.src="img/bird/b2.png";
|
||||||
bird.animations[3].sprite.src="img/bird/b0.png";
|
bird.animations[3].sprite.src="img/bird/b0.png";
|
||||||
console.log(gnd.sprite);
|
|
||||||
|
|
||||||
gameLoop();
|
gameLoop();
|
||||||
|
|
||||||
|
|
@ -92,6 +154,7 @@ gameLoop();
|
||||||
bg.draw();
|
bg.draw();
|
||||||
gnd.draw();
|
gnd.draw();
|
||||||
bird.draw();
|
bird.draw();
|
||||||
gr.draw();
|
if(state.curr != state.Play)
|
||||||
|
UI.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue