Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var game = new Phaser.Game(288, 505, Phaser.AUTO, 'flappyDiv');
- var mainState = {
- preload: function() {
- game.load.image('background', 'assets/background.png');
- game.load.image('ground', 'assets/ground.png');
- game.load.spritesheet('bird', 'assets/bird.png', 34, 24, 3);
- game.load.image('pipe', 'assets/pipe.png');
- game.load.audio('jump', 'assets/flap.wav');
- },
- create: function() {
- this.background = this.game.add.sprite(0, 0, 'background');
- this.ground = this.game.add.tileSprite(0, 400, 288, 112, 'ground');
- this.ground.autoScroll(-200, 0);
- this.bird = this.game.add.sprite(100, 245, 'bird');
- this.bird.animations.add('bird', [0, 1, 2], 12, true);
- this.bird.animations.play('bird');
- game.physics.startSystem(Phaser.Physics.ARCADE);
- game.physics.arcade.enableBody(this.bird);
- game.physics.arcade.enableBody(this.ground);
- this.ground.body.immovable = true;
- 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" });
- this.bird.anchor.setTo(0.5, 0.5);
- this.jumpSound = game.add.audio('jump');
- },
- update: function() {
- game.physics.arcade.overlap(this.bird, this.pipes, this.overlapPipe, null, this);
- game.physics.arcade.collide(this.bird, this.ground);
- if (this.bird.inWorld == false){
- this.restartGame();
- }
- if (this.bird.angle < 20) {
- this.bird.angle += 1;
- }
- },
- overlapPipe: function() {
- game.time.events.remove(this.timer);
- this.pipes.forEachAlive(function(p){
- p.body.velocity.x = 0;
- }, this);
- this.bird.alive = false;
- },
- jump: function() {
- if (this.bird.alive == false) {
- return;
- }
- this.bird.body.velocity.y = -350;
- var animation = game.add.tween(this.bird);
- animation.to({angle: -20}, 100);
- animation.start();
- this.jumpSound.play();
- },
- 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');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement