Advertisement
Guest User

game.js

a guest
Jan 7th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 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. // Random Background
  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.  
  37. var randomBee = 'bee'+ this.game.rnd.integerInRange(1,5);
  38. // Bee Name
  39. var beeName = getName(randomBeeKey);
  40. //Add Sprite
  41. this.bee = this.game.add.sprite(100, 245, randomBee);
  42. // Add Animation
  43. this.bee.animations.add('fly',[0,1]);
  44. // Play Animation
  45. this.bee.animations.play('fly', 20, true);
  46. // Enable Bee Physics
  47. game.physics.arcade.enable(this.bee);
  48. // Add gravity to Bee
  49. this.bee.body.gravity.y = 1000;
  50.  
  51. function getName(beeKey) {
  52. if(beeKey === "bee1") return "Boobee";
  53. if(beeKey === "bee2") return "Digbee";
  54. if(beeKey === "bee3") return "Feebee";
  55. if(beeKey === "bee4") return "Abbee";
  56. if(beeKey === "bee5") return "Newbee";
  57. }
  58.  
  59. // Display Bee top right
  60. this.game.add.image(320, 20, randomBee);
  61.  
  62. // this.bee.body.acceleration.x = 1;
  63.  
  64. // New anchor position
  65. this.bee.anchor.setTo(-0.2, 0.5);
  66.  
  67. // Spacebar
  68. var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  69. spaceKey.onDown.add(this.jump, this);
  70. game.input.onTap.add(this.jump, this);
  71.  
  72. // P to Pause
  73. window.onkeydown = function() {
  74. if (game.input.keyboard.event.keyCode == 80){
  75. game.paused = !game.paused;
  76. }
  77. }
  78.  
  79. // Add the score GUI
  80. this.score = 0;
  81. this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
  82.  
  83. // Add the jump sound
  84. this.jumpSound = this.game.add.audio('jump');
  85.  
  86. // Add the buzzin sound
  87. music = game.add.audio('buzzing');
  88.  
  89. music.play();
  90.  
  91. },
  92.  
  93. update: function() {
  94.  
  95.  
  96. // Touch to Jump
  97. if (game.input.activePointer.isDown && !this.touched) {
  98. this.touched = true;
  99. this.jump();
  100. }
  101.  
  102. if (game.input.activePointer.isUp) {
  103. this.touched = false;
  104. }
  105.  
  106. if (this.bee.inWorld == false)
  107. this.restartGame();
  108.  
  109. game.physics.arcade.overlap(this.bee, this.pipes, this.hitPipe, null, this);
  110.  
  111. // Slowly rotate the bird downward, up to a certain point.
  112. if (this.bee.angle < 20)
  113. this.bee.angle += 1;
  114. },
  115.  
  116. jump: function() {
  117. // If the bird is dead, he can't jump
  118. if (this.bee.alive == false)
  119. return;
  120.  
  121. this.bee.body.velocity.y = -350;
  122.  
  123. // Jump animation
  124. game.add.tween(this.bee).to({angle: -20}, 100).start();
  125.  
  126. // Play sound
  127. this.jumpSound.play();
  128. },
  129.  
  130. hitPipe: function() {
  131. // If the bird has already hit a pipe, we have nothing to do
  132. if (this.bee.alive == false)
  133. return;
  134.  
  135. // Set the alive property of the bird to false
  136. this.bee.alive = false;
  137.  
  138. // Prevent new pipes from appearing
  139. this.game.time.events.remove(this.timer);
  140.  
  141. // Go through all the pipes, and stop their movement
  142. this.pipes.forEachAlive(function(p){
  143. p.body.velocity.x = 0;
  144. }, this);
  145. },
  146.  
  147. restartGame: function() {
  148. game.state.start('Game_Over');
  149. },
  150.  
  151. addOnePipe: function(x, y) {
  152. var pipe = this.pipes.getFirstDead();
  153.  
  154. pipe.reset(x, y);
  155. pipe.body.velocity.x = -200;
  156. pipe.checkWorldBounds = true;
  157. pipe.outOfBoundsKill = true;
  158. },
  159.  
  160. addRowOfPipes: function() {
  161. var hole = Math.floor(Math.random()*5)+1;
  162.  
  163. for (var i = 0; i < 8; i++)
  164. if (i != hole && i != hole +1)
  165. this.addOnePipe(400, i*60+10);
  166.  
  167. this.score += 1;
  168. this.labelScore.text = this.score;
  169.  
  170. },
  171.  
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement