Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to create Flappy Bird (Part 5) – Scoring and Overlapping
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'flappyDiv');
var mainState = {
preload: function() {
game.stage.backgroundColor = '#71c5cf';
game.load.image('bird', 'assets/bird.png');
game.load.image('pipe', 'assets/pipe.png');
},
create: function() {
this.bird = this.game.add.sprite(100, 245, 'bird');
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.enable(this.bird);
this.bird.body.gravity.y = 1000;
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
spaceKey.onDown.add(this.jump, this);
this.pipes = game.add.group();
this.pipes.enableBody = true;
this.pipes.createMultiple(20, 'pipe');
this.timer = game.time.events.loop(1600, this.addLongPipes, this);
this.score = 0;
this.labelScore = game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
},
update: function() {
game.physics.arcade.overlap(this.bird, this.pipes, this.restartGame, null, this);
if (this.bird.inWorld == false){
this.restartGame();
}
},
jump: function() {
this.bird.body.velocity.y = -350;
},
restartGame: function() {
game.state.start('main');
},
addShotPipe: function(x, y) {
var pipe = this.pipes.getFirstDead();
pipe.reset(x, y);
pipe.body.velocity.x = -200;
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
},
addLongPipes: function() {
var blank = Math.floor(Math.random() * 5) + 1;
for (var i = 0; i < 8; i++){
if (i != blank && i != blank + 1){
this.addShotPipe(400, (i * 60) + 10);
}
}
this.score += 1;
this.labelScore.text = this.score;
},
};
game.state.add('main', mainState);
game.state.start('main');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.