Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Sample Breakout Game</title>
  6. <style type="text/css" media="screen">
  7. *
  8. {
  9. padding: 0;
  10. margin: 0;
  11. }
  12. </style>
  13. <!-- <script src="js/phaser.min.js"></script> -->
  14. <script src="js/phaser.js"></script>
  15. </head>
  16. <body>
  17.  
  18. <script>
  19. var game = new Phaser.Game(480,320,Phaser.AUTO,null,{
  20. preload: preload, create:create,update:update
  21. });
  22. var ball,paddle,bricks,numBricksHit = 0;
  23. var lives = 3, livesText , lifeLostText;
  24. var scoreText,score = 0,playing = false,startButton,paddleNotHit = 0;
  25. var textStyle = { font: '18px Arial', fill: '#0095DD' };
  26. // var brickPattern = [[1,0,1,0,1,1,1],[1,1,1,0,0,1,0],[1,0,1,0,1,1,1]];
  27. var brickPattern;
  28. function preload() {
  29. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  30. game.scale.pageAlignHorizontally = true;
  31. game.scale.pageAlignVertically = true;
  32. game.stage.backgroundColor = '#000';
  33. // game.load.image('ball','img/ball.png');
  34. game.load.image('paddle','img/paddle.png');
  35. game.load.image('brick','img/brick2.png');
  36. game.load.spritesheet('ball','img/wobble.png',20,20);
  37. game.load.spritesheet('button','img/button.png',120,40);
  38. }
  39. function create() {
  40. game.physics.startSystem(Phaser.Physics.ARCADE);
  41. game.physics.arcade.checkCollision.down = false;
  42. ball = game.add.sprite(game.world.width*0.5,game.world.height - 25, 'ball');
  43. ball.animations.add('wobble', [0,1,0,2,0,1,0,2,0], 48);
  44. ball.anchor.set(0.5);
  45. game.physics.enable(ball,Phaser.Physics.ARCADE);
  46. ball.body.collideWorldBounds = true;
  47. ball.body.bounce.set(1);
  48. // ball.body.velocity.set(150,-150);
  49. ball.checkWorldBounds = true;
  50. ball.events.onOutOfBounds.add(ballLeaveScreen,this);
  51. paddle = game.add.sprite(game.world.width*0.5,game.world.height-5,'paddle');
  52. paddle.anchor.set(0.5,1);
  53. game.physics.enable(paddle,Phaser.Physics.ARCADE);
  54. paddle.body.immovable = true;
  55. initBricks();
  56. scoreText = game.add.text(5,5,'Points: 0',textStyle);
  57. livesText = game.add.text(game.world.width-5, 5, 'Lives: '+ lives,textStyle);
  58. livesText.anchor.set(1,0);
  59. lifeLostText = game.add.text(game.world.width*0.5, game.world.height*0.5, 'Life lost, Click to continue!',textStyle);
  60. lifeLostText.anchor.set(0.5);
  61. lifeLostText.visible = false;
  62. startButton = game.add.button(game.world.width*0.5,game.world.height*0.5,'button',startGame,this);
  63. startButton.anchor.set(0.5);
  64. }
  65. function update() {
  66. game.physics.arcade.collide(ball,paddle,ballHitPaddle);
  67. game.physics.arcade.collide(ball,bricks,ballHitBrick);
  68. if(playing)
  69. {
  70. paddle.x = game.input.x || game.world.width*0.5;
  71. }
  72. }
  73.  
  74. function countOnes(arr)
  75. {
  76. var count = 0;
  77. for(var i=0;i<arr.length;i++)
  78. {
  79. for(var j=0;j<arr[i].length;j++)
  80. {
  81. if(arr[i][j] == 1)
  82. count += 1;
  83. }
  84. }
  85. return count;
  86. }
  87. function ballHitBrick(ball,brick)
  88. {
  89. numBricksHit += 1;
  90. paddleNotHit += 1;
  91. var killTween = game.add.tween(brick.scale);
  92. killTween.to({x:0,y:0},200,Phaser.Easing.Linear.None);
  93. killTween.onComplete.addOnce(function() {
  94. brick.kill();
  95. },this);
  96. killTween.start();
  97. score += 10*paddleNotHit;
  98. scoreText.setText('Points: ' + score);
  99. if(numBricksHit === countOnes(brickPattern)) {
  100. alert('You won!');
  101. location.reload();
  102. }
  103. }
  104. function initBricks()
  105. {
  106. brickInfo = {
  107. width: 50,
  108. height: 20,
  109. count: {
  110. row: 7,
  111. col: 3
  112. },
  113. offset: {
  114. top: 50,
  115. left: 60
  116. },
  117. padding: 10
  118. }
  119. bricks = game.add.group();
  120. brickPattern = new Array(brickInfo.count.col);
  121. for(var i = 0;i<brickInfo.count.col;i++)
  122. {
  123. brickPattern[i] = new Array(brickInfo.count.row);
  124. }
  125. var brickColor = Math.random() * 0xdddddd + 0x111111;
  126. for(c=0;c<brickInfo.count.col;c++)
  127. {
  128. for(r=0;r<brickInfo.count.row;r++)
  129. {
  130. brickPattern[c][r] = Math.floor(Math.random() + 0.78);
  131. if(brickPattern[c][r])
  132. {
  133. var brickX = brickInfo.offset.left + (r*(brickInfo.width + brickInfo.padding));
  134. var brickY = brickInfo.offset.top + (c*(brickInfo.height+ brickInfo.padding));
  135. newBrick = game.add.sprite(brickX,brickY, 'brick');
  136. game.physics.enable(newBrick,Phaser.Physics.ARCADE);
  137. newBrick.body.immovable = true;
  138. newBrick.anchor.set(0.5);
  139. newBrick.tint = brickColor;
  140. bricks.add(newBrick);
  141. }
  142. }
  143. }
  144. }
  145. function ballLeaveScreen() {
  146. lives--;
  147. if(lives)
  148. {
  149. livesText.setText('Lives: '+lives);
  150. lifeLostText.visible = true;
  151. ball.reset(game.world.width*0.5,game.world.height - 25);
  152. paddle.reset(game.world.with*0.5,game.world.height - 5);
  153. game.input.onDown.addOnce(function() {
  154. lifeLostText.visible = false;
  155. ball.body.velocity.set(150,-150);
  156. },this);
  157. }
  158. else
  159. {
  160. alert('You Lost!, GAME OVER!!!');
  161. location.reload();
  162. }
  163. }
  164. function ballHitPaddle()
  165. {
  166. paddleNotHit = 0;
  167. ball.animations.play('wobble');
  168. ball.body.velocity.x = -1*5*(paddle.x-ball.x);
  169. }
  170. function startGame()
  171. {
  172. startButton.destroy();
  173. ball.body.velocity.set(150,-150);
  174. playing = true;
  175. }
  176. </script>
  177. </body>
  178. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement