-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgame.min.js
More file actions
1 lines (1 loc) · 3.18 KB
/
game.min.js
File metadata and controls
1 lines (1 loc) · 3.18 KB
1
var game=new Phaser.Game(400,490,Phaser.AUTO,"gameDiv");var bootState={preload:function(){game.load.spritesheet("bird","assets/bird/123.png",68,48,3);game.load.image("pipeTop","assets/pipe/top.png");game.load.image("pipeBottom","assets/pipe/bottom.png");game.load.image("background","assets/background.png");game.load.image("logo","assets/logo.png");game.load.audio("jump","assets/jump.wav")},create:function(){game.physics.startSystem(Phaser.Physics.ARCADE);game.state.start("menu")}};var menuState={create:function(){game.add.image(0,0,"background");game.add.image(12,52,"logo");var e=game.add.text(game.world.centerX,game.world.height-100,"press space to start",{font:"25px Arial",fill:"#000000"});e.anchor.setTo(.5,.5);var t=game.add.tween(e);t.to({y:game.world.height-130},400);t.to({y:game.world.height-100},400);t.loop();t.easing(Phaser.Easing.Sinusoidal.In);t.start();game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.addOnce(this.start,this)},start:function(){game.state.start("play")}};var playState={create:function(){game.add.image(0,0,"background");this.bird=game.add.sprite(100,145,"bird");this.bird.animations.add("flapWings");this.bird.animations.play("flapWings",10,true);game.physics.arcade.enable(this.bird);this.bird.body.gravity.y=1e3;var e=this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);e.onDown.add(this.jump,this);this.timer=game.time.events.loop(1600,this.addPipes,this);this.initializeScore();this.jumpSound=game.add.audio("jump");this.pipes={};this.pipeId=0},logStuff:function(){console.log(this.pipeId)},initializeScore:function(){this.score=0;this.labelScore=game.add.text(20,20,"0",{font:"30px Arial",fill:"#000000"})},update:function(){if(this.bird.angle<20)this.bird.angle+=1;if(this.bird.inWorld==false)this.restartGame();for(var e in this.pipes){if(this.pipes.hasOwnProperty(e)){var t=this.pipes[e];game.physics.arcade.overlap(this.bird,t,this.hitPipe,null,this)}}},jump:function(){if(this.bird.alive==false)return;this.jumpSound.play();this.bird.body.velocity.y=-350;this.bird.anchor.setTo(-.2,.5);game.add.tween(this.bird).to({angle:-20},100).start()},restartGame:function(){game.state.start("menu")},addPipes:function(){var e=140;var t=50+Math.floor(Math.random()*(490-300));var n=game.add.sprite(400,t-490,"pipeTop");var r=game.add.sprite(400,t+e,"pipeBottom");n.pipeId=this.pipeId;this.pipeId=(1+this.pipeId)%4;r.pipeId=this.pipeId;this.pipeId=(1+this.pipeId)%4;this.pipes[n.pipeId]=n;this.pipes[r.pipeId]=r;game.physics.arcade.enable(n);game.physics.arcade.enable(r);n.body.velocity.x=-200;r.body.velocity.x=-200;n.checkWorldBounds=true;r.checkWorldBounds=true;n.events.onOutOfBounds.add(this.pipeOutOfBounds,this);r.events.onOutOfBounds.add(this.pipeOutOfBounds,this);n.outOfBoundsKill=true;r.outOfBoundsKill=true;this.incrementScore()},pipeOutOfBounds:function(e){delete this.pipes[e.pipeId]},incrementScore:function(){this.score+=1;this.labelScore.text=this.score},hitPipe:function(){if(this.bird.alive==false)return;this.bird.alive=false;game.time.events.remove(this.timer);for(var e in this.pipes)if(this.pipes.hasOwnProperty(e))this.pipes[e].body.velocity.x=0}};game.state.add("boot",bootState);game.state.add("menu",menuState);game.state.add("play",playState);game.state.start("boot");