Advertisement
Guest User

Untitled

a guest
May 24th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1.  
  2. var game = new Phaser.Game(288, 505, Phaser.AUTO, 'flappyDiv');
  3. var mainState = {
  4.  
  5. preload: function() {
  6. game.load.image('background', 'assets/background.png');
  7. game.load.image('ground', 'assets/ground.png');
  8. game.load.spritesheet('bird', 'assets/bird.png', 34, 24, 3);
  9. game.load.image('pipe', 'assets/pipe.png');
  10. game.load.audio('jump', 'assets/flap.wav');
  11. },
  12.  
  13. create: function() {
  14.  
  15. this.background = this.game.add.sprite(0, 0, 'background');
  16.  
  17. this.ground = this.game.add.tileSprite(0, 400, 288, 112, 'ground');
  18. this.ground.autoScroll(-200, 0);
  19.  
  20. this.bird = this.game.add.sprite(100, 245, 'bird');
  21. this.bird.animations.add('bird', [0, 1, 2], 12, true);
  22. this.bird.animations.play('bird');
  23.  
  24. game.physics.startSystem(Phaser.Physics.ARCADE);
  25. game.physics.arcade.enableBody(this.bird);
  26. game.physics.arcade.enableBody(this.ground);
  27. this.ground.body.immovable = true;
  28. this.bird.body.gravity.y = 1000;
  29.  
  30. var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  31. spaceKey.onDown.add(this.jump, this);
  32.  
  33. this.pipes = game.add.group();
  34. this.pipes.enableBody = true;
  35. this.pipes.createMultiple(20, 'pipe');
  36.  
  37. this.timer = game.time.events.loop(1600, this.addLongPipes, this);
  38.  
  39. this.score = 0;
  40. this.labelScore = game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
  41.  
  42. this.bird.anchor.setTo(0.5, 0.5);
  43.  
  44. this.jumpSound = game.add.audio('jump');
  45.  
  46. },
  47.  
  48. update: function() {
  49.  
  50. game.physics.arcade.overlap(this.bird, this.pipes, this.overlapPipe, null, this);
  51. game.physics.arcade.collide(this.bird, this.ground);
  52.  
  53. if (this.bird.inWorld == false){
  54. this.restartGame();
  55. }
  56.  
  57. if (this.bird.angle < 20) {
  58. this.bird.angle += 1;
  59. }
  60.  
  61. },
  62.  
  63.  
  64. overlapPipe: function() {
  65.  
  66. game.time.events.remove(this.timer);
  67.  
  68. this.pipes.forEachAlive(function(p){
  69. p.body.velocity.x = 0;
  70. }, this);
  71.  
  72. this.bird.alive = false;
  73. },
  74.  
  75. jump: function() {
  76.  
  77. if (this.bird.alive == false) {
  78. return;
  79. }
  80.  
  81. this.bird.body.velocity.y = -350;
  82.  
  83. var animation = game.add.tween(this.bird);
  84. animation.to({angle: -20}, 100);
  85. animation.start();
  86.  
  87. this.jumpSound.play();
  88. },
  89.  
  90. restartGame: function() {
  91. game.state.start('main');
  92. },
  93.  
  94. addShotPipe: function(x, y) {
  95.  
  96. var pipe = this.pipes.getFirstDead();
  97. pipe.reset(x, y);
  98. pipe.body.velocity.x = -200;
  99.  
  100. pipe.checkWorldBounds = true;
  101. pipe.outOfBoundsKill = true;
  102.  
  103. },
  104.  
  105. addLongPipes: function() {
  106.  
  107. var blank = Math.floor(Math.random() * 5) + 1;
  108. for (var i = 0; i < 8; i++){
  109. if (i != blank && i != blank + 1){
  110. this.addShotPipe(400, (i * 60) + 10);
  111. }
  112. }
  113.  
  114. this.score += 1;
  115. this.labelScore.text = this.score;
  116.  
  117. },
  118.  
  119. };
  120.  
  121. game.state.add('main', mainState);
  122. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement