Advertisement
Guest User

game.js

a guest
Jan 5th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. var Game = {
  2.  
  3. preload : function() {
  4.  
  5. game.load.spritesheet('bee1', 'assets/images/sprites/bee-1-sprite.png', 58, 38, 2);
  6. game.load.spritesheet('bee2', 'assets/images/sprites/bee-2-sprite.png', 58, 48, 2);
  7. game.load.spritesheet('bee3', 'assets/images/sprites/bee-3-sprite.png', 58, 52, 2);
  8. game.load.spritesheet('bee4', 'assets/images/sprites/bee-4-sprite.png', 58, 36, 2);
  9. game.load.spritesheet('bee5', 'assets/images/sprites/bee-5-sprite.png', 58, 37, 2);
  10.  
  11. game.load.image('pipe', 'assets/images/pipe.png');
  12. game.load.image('image1', 'assets/images/backgrounds/background1.png');
  13. game.load.image('image2', 'assets/images/backgrounds/background2.png');
  14. game.load.image('image3', 'assets/images/backgrounds/background3.png');
  15. game.load.image('image4', 'assets/images/backgrounds/background4.png');
  16. game.load.image('image5', 'assets/images/backgrounds/background5.png');
  17.  
  18. // Load the jump sound
  19. game.load.audio('jump', 'assets/images/jump.wav');
  20. game.load.audio('buzzing', 'assets/images/buzzing.wav');
  21. },
  22.  
  23. create : function() {
  24.  
  25. // Background Test
  26. backgroundimage = game.add.image(0, 0, 'image' + game.rnd.integerInRange(1,5));
  27.  
  28. game.physics.startSystem(Phaser.Physics.ARCADE);
  29.  
  30. this.pipes = game.add.group();
  31. this.pipes.enableBody = true;
  32. this.pipes.createMultiple(20, 'pipe');
  33. this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this);
  34.  
  35. // Add Spritesheet, Animate Spritesheet, Add Physics, Bee Sprite Right
  36. this.bee = this.game.add.sprite(100, 245, 'bee'+ game.rnd.integerInRange(1,5));
  37. this.bee.animations.add('fly',[0,1]);
  38. this.bee.animations.play('fly', 20, true);
  39. game.physics.arcade.enable(this.bee);
  40. this.bee.body.gravity.y = 1000;
  41. this.game.add.image(400, 400, 'bee'+ game.rnd.integerInRange(1,5));
  42.  
  43. // New anchor position
  44. this.bee.anchor.setTo(-0.2, 0.5);
  45.  
  46. var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  47. spaceKey.onDown.add(this.jump, this);
  48.  
  49. // P to Pause
  50. window.onkeydown = function() {
  51. if (game.input.keyboard.event.keyCode == 80){
  52. game.paused = !game.paused;
  53. }
  54. }
  55.  
  56. // Add the score GUI
  57. this.score = 0;
  58. this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
  59.  
  60. // Add the jump sound
  61. this.jumpSound = this.game.add.audio('jump');
  62.  
  63. // Add the buzzin sound
  64. music = game.add.audio('buzzing');
  65.  
  66. music.play();
  67.  
  68. },
  69.  
  70.  
  71.  
  72. update: function() {
  73. if (this.bee.inWorld == false)
  74. this.restartGame();
  75.  
  76. game.physics.arcade.overlap(this.bee, this.pipes, this.hitPipe, null, this);
  77.  
  78. // Slowly rotate the bird downward, up to a certain point.
  79. if (this.bee.angle < 20)
  80. this.bee.angle += 1;
  81. },
  82.  
  83. jump: function() {
  84. // If the bird is dead, he can't jump
  85. if (this.bee.alive == false)
  86. return;
  87.  
  88. this.bee.body.velocity.y = -350;
  89.  
  90. // Jump animation
  91. game.add.tween(this.bee).to({angle: -20}, 100).start();
  92.  
  93. // Play sound
  94. this.jumpSound.play();
  95. },
  96.  
  97. hitPipe: function() {
  98. // If the bird has already hit a pipe, we have nothing to do
  99. if (this.bee.alive == false)
  100. return;
  101.  
  102. // Set the alive property of the bird to false
  103. this.bee.alive = false;
  104.  
  105. // Prevent new pipes from appearing
  106. this.game.time.events.remove(this.timer);
  107.  
  108. // Go through all the pipes, and stop their movement
  109. this.pipes.forEachAlive(function(p){
  110. p.body.velocity.x = 0;
  111. }, this);
  112. },
  113.  
  114. restartGame: function() {
  115. game.state.start('Game_Over');
  116. },
  117.  
  118. addOnePipe: function(x, y) {
  119. var pipe = this.pipes.getFirstDead();
  120.  
  121. pipe.reset(x, y);
  122. pipe.body.velocity.x = -200;
  123. pipe.checkWorldBounds = true;
  124. pipe.outOfBoundsKill = true;
  125. },
  126.  
  127. addRowOfPipes: function() {
  128. var hole = Math.floor(Math.random()*5)+1;
  129.  
  130. for (var i = 0; i < 8; i++)
  131. if (i != hole && i != hole +1)
  132. this.addOnePipe(400, i*60+10);
  133.  
  134. this.score += 1;
  135. this.labelScore.text = this.score;
  136. },
  137.  
  138. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement