bird.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Initialize Phaser, and creates a 400x490px game
  2. var game = new Phaser.Game(400, 490, Phaser.AUTO, 'game_div');
  3. var game_state = {};
  4. // Creates a new 'main' state that will contain the game
  5. game_state.main = function() { };
  6. game_state.main.prototype = {
  7. // Function called first to load all the assets
  8. preload: function() {
  9. // Change the background color of the game
  10. this.game.stage.backgroundColor = '#71c5cf';
  11. // Load the bird sprite
  12. this.game.load.image('bird', '/../images/bird.png');
  13. // Load the pipe sprite
  14. this.game.load.image('pipe', '/../images/pipe.png');
  15. },
  16. // Fuction called after 'preload' to setup the game
  17. create: function() {
  18. // Display the bird on the screen
  19. this.bird = this.game.add.sprite(100, 245, 'bird');
  20. // Add gravity to the bird to make it fall
  21. this.bird.body.gravity.y = 1000;
  22. // Call the 'jump' function when the spacekey is hit
  23. var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  24. space_key.onDown.add(this.jump, this);
  25. // Create a group of 20 pipes
  26. this.pipes = game.add.group();
  27. this.pipes.createMultiple(20, 'pipe');
  28. // Timer that calls 'add_row_of_pipes' ever 1.5 seconds
  29. this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this);
  30. // Add a score label on the top left of the screen
  31. this.score = 0;
  32. var style = { font: "30px Arial", fill: "#ffffff" };
  33. this.label_score = this.game.add.text(20, 20, "0", style);
  34. },
  35. // This function is called 60 times per second
  36. update: function() {
  37. // If the bird is out of the world (too high or too low), call the 'restart_game' function
  38. if (this.bird.inWorld == false)
  39. this.restart_game();
  40. // If the bird overlap any pipes, call 'restart_game'
  41. this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
  42. },
  43. // Make the bird jump
  44. jump: function() {
  45. // Add a vertical velocity to the bird
  46. this.bird.body.velocity.y = -350;
  47. },
  48. // Restart the game
  49. restart_game: function() {
  50. // Remove the timer
  51. this.game.time.events.remove(this.timer);
  52. // Start the 'main' state, which restarts the game
  53. this.game.state.start('main');
  54. },
  55. // Add a pipe on the screen
  56. add_one_pipe: function(x, y) {
  57. // Get the first dead pipe of our group
  58. var pipe = this.pipes.getFirstDead();
  59. // Set the new position of the pipe
  60. pipe.reset(x, y);
  61. // Add velocity to the pipe to make it move left
  62. pipe.body.velocity.x = -200;
  63. // Kill the pipe when it's no longer visible
  64. pipe.outOfBoundsKill = true;
  65. },
  66. // Add a row of 6 pipes with a hole somewhere in the middle
  67. add_row_of_pipes: function() {
  68. var hole = Math.floor(Math.random()*5)+1;
  69. for (var i = 0; i < 8; i++)
  70. if (i != hole && i != hole +1)
  71. this.add_one_pipe(400, i*60+10);
  72. this.score += 1;
  73. this.label_score.content = this.score;
  74. },
  75. };
  76. // Add and start the 'main' state to start the game
  77. game.state.add('main', game_state.main);
  78. game.state.start('main');