Skip to content

Instantly share code, notes, and snippets.

@lessmilk
Last active August 29, 2015 13:56
Show Gist options
  • Save lessmilk/9083321 to your computer and use it in GitHub Desktop.
Save lessmilk/9083321 to your computer and use it in GitHub Desktop.
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'game_div');
var game_state = {};
game_state.main = function() { };
game_state.main.prototype = {
preload: function() {
this.game.stage.backgroundColor = '#71c5cf';
this.game.load.image('bird', 'assets/bird.png');
this.game.load.image('pipe', 'assets/pipe.png');
},
create: function() {
this.bird = this.game.add.sprite(100, this.game.world.height/2, 'bird');
this.bird.body.gravity.y = 1000;
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.flap, this);
this.pipes = game.add.group();
this.pipes.createMultiple(20, 'pipe');
this.timer = this.game.time.events.loop(1500, this.add_row_pipes, this);
this.score = 0;
var style = { font: "30px Arial", fill: "#ffffff"};
this.label_score = this.game.add.text(20, 20, "0", style);
},
update: function() {
if (this.bird.inWorld == false)
this.restart_game();
this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
},
flap: function() {
this.bird.body.velocity.y = -350;
},
add_one_pipe: function(x, y) {
var pipe = this.pipes.getFirstDead();
pipe.outOfBoundsKill = true;
pipe.reset(x, y);
pipe.body.velocity.x = -200;
},
add_row_pipes: function() {
var hole = Math.floor(Math.random()*5)+1;
for (var i = 0; i < 8; i++)
if (i != hole && i != hole +1)
this.add_one_pipe(this.game.world.width, i*60+10);
this.score += 1;
this.label_score.content = this.score;
},
restart_game: function(bird, pipe) {
this.game.time.events.remove(this.timer);
this.game.state.start('main');
}
}
game.state.add('main', game_state.main);
game.state.start('main');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment