08-wall-blaster-2.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. This activity uses loops and switches statements to place brick objects. Conditionals are used to program collision code. The player must clear all the bricks to win.
  3. */
  4. //*****************Scene - Initialize When Scene Starts Event************************//
  5. // set up the scene
  6. if ($this.scene.state() == "PLAY") {
  7. // keep track of how many bricks there are
  8. $this.brickCount = 0;
  9. // set up helper variables for the loops
  10. var blockSpacingX = 100;
  11. var blockSpacingY = 35;
  12. // loop through 4 rows and 8 columns and place the bricks
  13. for (var row = 0; row < 4; row++) {
  14. for(var col = 0; col < 8; col++) {
  15. // clone the brick objects
  16. var brickClone = brick.clone();
  17. // place the cloned brick using the helper variables
  18. brickClone.x(col * blockSpacingX);
  19. brickClone.y(row * blockSpacingY);
  20. // give the bricks health based on the row
  21. brickClone.health = 4 - row;
  22. brickClone.updateColor();
  23. //increment the brickCount
  24. $this.brickCount++;
  25. }
  26. }
  27. // remove the original brick from the scene
  28. brick.remove();
  29. }
  30. //*************************paddle - Update Every Frame Event************************//
  31. // move the paddle to the cursor
  32. $this.x(getMouseX() - 125/2);
  33. //************************ball - Update Every Frame Event**************************//
  34. // move the ball on every frame
  35. moveX($this);
  36. moveY($this);
  37. // check for collisions on every frame
  38. $this.checkPaddleCollision();
  39. $this.checkWallCollision();
  40. //***********************ball - Initialize When Scene Starts Event*****************//
  41. // check to see if the ball is colliding with the paddle
  42. $this.checkPaddleCollision = function() {
  43. if ($this.isTouching(paddle)) {
  44. // calculate x speed based on where the ball hit the paddle
  45. var ballCenter = $this.x() + 25/2;
  46. var paddleCenter = paddle.x() + 125/2;
  47. var launchSpeed = 3 * (ballCenter - paddleCenter);
  48. // adjust the speedX based on the launch speed
  49. $this.speedX(launchSpeed);
  50. // keep the speedY at 200 but make sure the ball goes up
  51. $this.speedY(-200);
  52. }
  53. };
  54. // check to see if the ball is colliding with the walls
  55. $this.checkWallCollision = function() {
  56. // bounce the ball off the left or right side
  57. if ($this.x() <= 0 || $this.x() >= 800) {
  58. // change the ball's x direction
  59. $this.speedX(-$this.speedX());
  60. }
  61. // bounce the ball off the top side
  62. if ($this.y() <= 0) {
  63. // change the ball's y direction
  64. $this.speedY(-$this.speedY());
  65. }
  66. // reset the ball if it goes off the bottom side
  67. if ($this.y() >= 600) {
  68. // place the ball in the center of the scne
  69. $this.x(400);
  70. $this.y(300);
  71. // give the ball a random speedX
  72. $this.speedX(random(-200, 200));
  73. }
  74. };
  75. //************************brick - Update Every Frame Event**************************//
  76. // if this brik touches the ball
  77. if ($this.isTouching(ball)) {
  78. // decrease the brick's health by one and update its color
  79. $this.health--;
  80. $this.updateColor();
  81. // bounce the ball in the opposite direction
  82. ball.speedY(-ball.speedY());
  83. // if the brick's health is 0
  84. if ($this.health <= 0) {
  85. // decrement the brick count
  86. $this.scene.brickCount--;
  87. // remove the brick
  88. $this.remove();
  89. // if there are no more bricks, stop the game
  90. if ($this.scene.brickCount <= 0) {
  91. $this.scene.stopCode();
  92. }
  93. }
  94. }
  95. //*********************brick - Initialize When Scene Starts Event********************//
  96. // change the brick color based on its health
  97. $this.updateColor = function() {
  98. switch ($this.health) {
  99. case 1:
  100. $this.fill("#8CE0D3"); // light blue
  101. break;
  102. case 2:
  103. $this.fill("#28ADA9"); // dark blue
  104. break;
  105. case 3:
  106. $this.fill("#8A2F62"); // light purple
  107. break;
  108. case 4:
  109. $this.fill("#500F34"); // dark purple
  110. break;
  111. default:
  112. $this.fill("#000000"); //black
  113. break;
  114. }
  115. };