bird.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435
  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 = 600, 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;
  12. this.label_score = this.game.add.text(20, 20, '0', {
  13. font: '30px Comic Sans MS',
  14. fill: '#ffffff'
  15. });
  16. },
  17. update: function () {
  18. 0 == this.bird.inWorld && this.restart_game(), this.bird.body.gravity.y = 0.8 * this.bird.body.y + 600, this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
  19. },
  20. jump: function () {
  21. this.bird.body.velocity.y = Math.max(this.bird.body.velocity.y / 3 + 250 - this.bird.body.gravity.y / 1.5, -450);
  22. },
  23. restart_game: function () {
  24. this.game.time.events.remove(this.timer), this.game.state.start('main');
  25. },
  26. add_one_pipe: function (t, i) {
  27. var e = this.pipes.getFirstDead();
  28. e.reset(t, i), e.body.velocity.x = -200, e.outOfBoundsKill = !0;
  29. },
  30. add_row_of_pipes: function () {
  31. for (var t = Math.floor(5 * Math.random()) + 1, i = 0; i < 8; i++)
  32. i != t && i != t + 1 && (i != t - 1 || i == t - 1 && Math.random() < 0.6) && this.add_one_pipe(400, 60 * i + 10);
  33. this.score += 1, this.label_score.content = this.score;
  34. }
  35. }, game.state.add('main', game_state.main), game.state.start('main');