bird.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. var game = new Phaser.Game(400, 490, Phaser.AUTO, 'game_div'), game_state = {
  3. main: function () {
  4. }
  5. };
  6. game_state.main.prototype = {
  7. preload: function () {
  8. this.game.stage.backgroundColor = '#71c5cf', this.game.load.image('bird', '/../images/bird.png'), this.game.load.image('pipe', '/../images/pipe.png');
  9. },
  10. create: function () {
  11. this.bird = this.game.add.sprite(100, 245, 'bird'), this.bird.body.gravity.y = 1000, this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(this.jump, this), this.pipes = game.add.group(), this.pipes.createMultiple(20, 'pipe'), this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this), this.score = 0, this.label_score = this.game.add.text(20, 20, '0', {
  12. font: '30px Arial',
  13. fill: '#ffffff'
  14. });
  15. },
  16. update: function () {
  17. 0 == this.bird.inWorld && this.restart_game(), this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
  18. },
  19. jump: function () {
  20. this.bird.body.velocity.y = -350;
  21. },
  22. restart_game: function () {
  23. this.game.time.events.remove(this.timer), this.game.state.start('main');
  24. },
  25. add_one_pipe: function (t, e) {
  26. var i = this.pipes.getFirstDead();
  27. i.reset(t, e), i.body.velocity.x = -200, i.outOfBoundsKill = !0;
  28. },
  29. add_row_of_pipes: function () {
  30. for (var t = Math.floor(5 * Math.random()) + 1, e = 0; e < 8; e++)
  31. e != t && e != t + 1 && this.add_one_pipe(400, 60 * e + 10);
  32. this.score += 1, this.label_score.content = this.score;
  33. }
  34. }, game.state.add('main', game_state.main), game.state.start('main');