Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. // Create the state that will contain the whole game for Breakout
  2. var mainState = {
  3.  
  4. preload: function() {
  5.  
  6. game.load.image('paddle', 'assets/paddle.png');
  7.  
  8. game.load.image('brick', 'assets/brick.png');
  9.  
  10. game.load.image('ball', 'assets/ball.png');
  11.  
  12. },
  13.  
  14. create: function() {
  15. // Set the background color to blue
  16. game.stage.backgroundColor = '#3598db';
  17.  
  18. // Start the Arcade physics system (for movements and collisions)
  19. game.physics.startSystem(Phaser.Physics.ARCADE);
  20.  
  21. // Add the physics engine to all the game objetcs
  22. game.world.enableBody = true;
  23.  
  24. // Create the left/right arrow keys
  25. this.left = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
  26. this.right = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
  27.  
  28. // Add the paddle at the bottom of the screen
  29. this.paddle = game.add.sprite(200, 400, 'paddle');
  30.  
  31. // Make sure the paddle won't move when it hits the ball
  32. this.paddle.body.immovable = true;
  33.  
  34. // Create a group that will contain all the bricks
  35. this.bricks = game.add.group();
  36.  
  37. // Add 25 bricks to the group (5 columns and 5 lines)
  38. for (var i = 0; i < 5; i++) {
  39. for (var j = 0; j < 5; j++) {
  40. // Create the brick at the correct position
  41. var brick = game.add.sprite(55+i*60, 55+j*35, 'brick');
  42.  
  43. // Make sure the brick won't move when the ball hits it
  44. brick.body.immovable = true;
  45.  
  46. // Add the brick to the group
  47. this.bricks.add(brick);
  48. }
  49. }
  50.  
  51. // Add the ball
  52. this.ball = game.add.sprite(200, 300, 'ball');
  53.  
  54. // Give the ball some initial speed
  55. this.ball.body.velocity.x = 200;
  56. this.ball.body.velocity.y = 100;
  57.  
  58. // Make sure the ball will bounce when hitting something
  59. this.ball.body.bounce.setTo(1);
  60. this.ball.body.collideWorldBounds = true;
  61.  
  62. },
  63.  
  64. update: function() {
  65. // Move the paddle left/right when an arrow key is pressed
  66. if (this.left.isDown) this.paddle.body.velocity.x = -300;
  67. else if (this.right.isDown) this.paddle.body.velocity.x = 300;
  68.  
  69. // Stop the paddle when no key is pressed
  70. else this.paddle.body.velocity.x = 0;
  71.  
  72. // Add collisions between the paddle and the ball
  73. game.physics.arcade.collide(this.paddle, this.ball);
  74.  
  75. // Call the 'hit' function when the ball hits a brick
  76. game.physics.arcade.collide(this.ball, this.bricks, this.hit, null, this);
  77.  
  78. // Restart the game if the ball is below the paddle
  79. if (this.ball.y > this.paddle.y)
  80. game.state.start('main');
  81. },
  82.  
  83. // New function that removes a brick from the game
  84. hit: function(ball, brick) {
  85. brick.kill();
  86. },
  87. };
  88.  
  89. // Initialize the game and start our state
  90. var game = new Phaser.Game(400, 450);
  91. game.state.add('main', mainState);
  92. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement