Advertisement
Arham-4

Flappy Bird - Phaser.JS

Dec 23rd, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const PIPES = 8;
  2. const mainState = {
  3.     preload: function () {
  4.         game.load.image('bird', 'assets/bird.png');
  5.         game.load.image('pipe', 'assets/pipe.png');
  6.         game.load.audio('jump', 'assets/jump.wav');
  7.     },
  8.     create: function () {
  9.         this.jumpSound = game.add.audio('jump');
  10.         this.score = 0;
  11.         this.pipes = game.add.group();
  12.  
  13.         game.stage.backgroundColor = '#71c5cf';
  14.         this.bird = game.add.sprite(100, 245, 'bird');
  15.         this.bird.anchor.setTo(-0.2, 0.5);
  16.  
  17.         game.physics.startSystem(game.physics.arcade);
  18.         game.physics.arcade.enable(this.bird);
  19.         this.bird.body.gravity.y = 1000;
  20.  
  21.         const spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  22.         spaceKey.onDown.add(this.jump, this);
  23.  
  24.         this.timer = game.time.events.loop(1500, this.addPipes, this);
  25.         this.labelScore = game.add.text(20, 20, "0", {
  26.             font: "30px Arial",
  27.             fill: "#ffffff"
  28.         });
  29.     },
  30.     update: function () {
  31.         if (this.bird.y < 0 || this.bird.y > 490) {
  32.             this.restartGame();
  33.         }
  34.         game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe, null, this);
  35.         if (this.bird.angle < 20) {
  36.             this.bird.angle += 1;
  37.         }
  38.     },
  39.     jump: function () {
  40.         if (!this.bird.alive) {
  41.             return;
  42.         }
  43.         this.bird.body.velocity.y = -350;
  44.         const animation = game.add.tween(this.bird);
  45.         animation.to({angle: -20}, 100);
  46.         animation.start();
  47.         this.jumpSound.play();
  48.     },
  49.     restartGame: function () {
  50.         game.state.start('main');
  51.     },
  52.     addPipe: function (x, y) {
  53.         const pipe = game.add.sprite(x, y, 'pipe');
  54.  
  55.         this.pipes.add(pipe);
  56.         game.physics.arcade.enable(pipe);
  57.         pipe.body.velocity.x = -200;
  58.  
  59.         pipe.checkWorldBounds = true;
  60.         pipe.outOfBoundsKill = true;
  61.     },
  62.     addPipes: function () {
  63.         const holeIndex = Math.floor(Math.random() * 5) + 1;
  64.         for (let i = 0; i < PIPES; i++) {
  65.             if (i !== holeIndex && i !== holeIndex + 1) {
  66.                 this.addPipe(400, i * 60 + 10);
  67.             }
  68.         }
  69.         this.score += 1;
  70.         this.labelScore.text = this.score;
  71.     },
  72.     hitPipe: function () {
  73.         if (!this.bird.alive) {
  74.             return;
  75.         }
  76.         this.bird.alive = false;
  77.         game.time.events.remove(this.timer);
  78.         this.pipes.forEach(function(pipe) {
  79.             pipe.body.velocity.x = 0;
  80.         }, this);
  81.     }
  82. };
  83. const game = new Phaser.Game(400, 490);
  84. game.state.add('main', mainState);
  85. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement