Advertisement
Guest User

Untitled

a guest
May 10th, 2016
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. // Create our 'main' state that will contain the game
  2. var mainState = {
  3. preload: function() {
  4. // This function will be executed at the beginning
  5. // That's where we load the images and sounds
  6. game.load.image('bird', 'assets/bird.png');
  7. game.load.image('pipe', 'assets/pipe.png');
  8. },
  9.  
  10. create: function() {
  11. // This function is called after the preload function
  12. // Here we set up the game, display sprites, etc.
  13. // If this is not a desktop (so it's a mobile device)
  14.  
  15. if (game.device.desktop == false) {
  16. // Set the scaling mode to SHOW_ALL to show all the game
  17. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  18.  
  19. // Set a minimum and maximum size for the game
  20. // Here the minimum is half the game size
  21. // And the maximum is the original game size
  22. game.scale.setMinMax(game.width/2, game.height/2, game.width, game.height);
  23. }
  24. // Center the game horizontally and vertically
  25.  
  26.  
  27. game.stage.backgroundColor = '#71c5cf';
  28. game.physics.startSystem(Phaser.Physics.ARCADE);
  29. this.bird = game.add.sprite(100, 245, 'bird');
  30. this.pipes = game.add.group();
  31. game.physics.arcade.enable(this.bird);
  32. this.bird.body.gravity.y = 1000;
  33. var spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  34. spaceKey.onDown.add(this.jump, this);
  35. game.input.onDown.add(this.jump, this);
  36. this.timer = game.time.events.loop(1500, this.addRowOfPipes, this);
  37. this.score = 0;
  38. this.labelScore = game.add.text(20, 20, "0",
  39. { font: "30px Arial", fill: "#ffffff" });
  40. setTimeout(test, 100);
  41. },
  42.  
  43. update: function() {
  44. // This function is called 60 times per second
  45. // It contains the game's logic
  46. if (this.bird.y < 0 || this.bird.y > 490)
  47. this.restartGame();
  48. game.physics.arcade.overlap(this.bird, this.pipes, this.restartGame, null, this);
  49. },
  50.  
  51. jump: function() {
  52. // Add a vertical velocity to the bird
  53. this.bird.body.velocity.y = -350;
  54. },
  55. addOnePipe: function(x, y) {
  56. // Create a pipe at the position x and y
  57. var pipe = game.add.sprite(x, y, 'pipe');
  58.  
  59. // Add the pipe to our previously created group
  60. this.pipes.add(pipe);
  61.  
  62. // Enable physics on the pipe
  63. game.physics.arcade.enable(pipe);
  64.  
  65. // Add velocity to the pipe to make it move left
  66. pipe.body.velocity.x = -200;
  67.  
  68. // Automatically kill the pipe when it's no longer visible
  69. pipe.checkWorldBounds = true;
  70. pipe.outOfBoundsKill = true;
  71. },
  72.  
  73. addRowOfPipes: function() {
  74. // Randomly pick a number between 1 and 5
  75. // This will be the hole position
  76. var hole = Math.floor(Math.random() * 5) + 1;
  77.  
  78. // Add the 6 pipes
  79. // With one big hole at position 'hole' and 'hole + 1'
  80. for (var i = 0; i < 8; i++)
  81. if (i != hole && i != hole + 1)
  82. this.addOnePipe(400, i * 60 + 10);
  83. this.score += 1;
  84. this.labelScore.text = this.score;
  85. },
  86.  
  87. // Restart the game
  88. restartGame: function() {
  89. // Start the 'main' state, which restarts the game
  90. var buildProgress = 1;
  91. var houseid = 1;
  92. if(this.score > 0){
  93. $.ajax({
  94. type: "POST",
  95. url: 'updatehighscore.php',
  96. data: {test: buildProgress, test2: this.score },
  97. });
  98. }
  99. game.state.start('main');
  100.  
  101. },
  102. };
  103.  
  104. // Initialize Phaser, and create a 400px by 490px game
  105. var game = new Phaser.Game(400, 490);
  106.  
  107. // Add and start the 'main' state to start the game
  108. game.state.add('main', mainState, true);
  109.  
  110. function test(){
  111. window.dispatchEvent(new Event('resize'));
  112. }
  113.  
  114.  
  115. function resizeGame() {var height = $(window).height();var width = $(window).width(); game.width = width;game.height = height; }
  116.  
  117. $( document ).ready(function() {
  118. $(window).resize(function() { window.resizeGame(); } );
  119. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement