diff --git a/game.js b/game.js index b1ed960..1da4e50 100644 --- a/game.js +++ b/game.js @@ -2,16 +2,25 @@ const sctx = scrn.getContext("2d"); sctx.fillStyle = "#30c0df"; let frames = 0; - const gnd = { - sprite : new Image(), +const gnd = { + animations : + [ + {sprite : new Image()}, + {sprite : new Image()}, + ], x : 0, y :0, + frame : 0, draw : function() { - y = parseFloat(scrn.height-this.sprite.height); - sctx.drawImage(this.sprite,this.x,y); + y = parseFloat(scrn.height-this.animations[this.frame].sprite.height); + sctx.drawImage(this.animations[this.frame].sprite,this.x,y); + }, + update : function(){ + this.frame += (frames%10==0) ? 1 : 0; + this.frame = this.frame%this.animations.length; } }; - const bg = { +const bg = { sprite : new Image(), x : 0, y :0, @@ -43,11 +52,17 @@ const bird = { frame:0, draw : function() { sctx.drawImage(this.animations[this.frame].sprite,this.x,this.y); + }, + update : function() { + this.frame += (frames%10==0) ? 1 : 0; + this.frame = this.frame%this.animations.length; + this.y += (frames%10==0) ? (Math.random()-0.5)*2 : 0; } }; -gnd.sprite.src="img/ground.png"; +gnd.animations[0].sprite.src="img/ground/g0.png"; +gnd.animations[1].sprite.src="img/ground/g1.png"; bg.sprite.src="img/BG.png"; gr.sprite.src="img/getready.png"; bird.animations[0].sprite.src="img/bird/b0.png"; @@ -68,9 +83,8 @@ gameLoop(); function update() { - bird.frame += (frames%5==0) ? 1 : 0; - bird.frame = bird.frame%4; - bird.y += (frames%5==0) ? (Math.random()-0.5)*2 : 0; + bird.update(); + gnd.update(); } function draw() { diff --git a/img/ground.png b/img/ground/g0.png similarity index 100% rename from img/ground.png rename to img/ground/g0.png diff --git a/img/ground/g1.png b/img/ground/g1.png new file mode 100644 index 0000000..4d71e8f Binary files /dev/null and b/img/ground/g1.png differ