SHARE
TWEET

Untitled

a guest Dec 6th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var PADDLE_WIDTH = 80;
  2. var PADDLE_HEIGHT = 15;
  3. var PADDLE_OFFSET = 10;
  4.  
  5. var BALL_RADIUS = 15;
  6.  
  7. var NUM_ROWS = 8;
  8. var BRICK_TOP_OFFSET = 10;
  9. var BRICK_SPACING = 2;
  10. var NUM_BRICKS_PER_ROW = 10;
  11. var BRICK_HEIGHT = 10;
  12. var SPACE_FOR_BRICKS = getWidth() - (NUM_BRICKS_PER_ROW + 1) * BRICK_SPACING;
  13. var BRICK_WIDTH = SPACE_FOR_BRICKS / NUM_BRICKS_PER_ROW;
  14.  
  15. var paddle, ball;
  16. var vx;
  17. var vy = 8;
  18. var gameOver = false;
  19. var NUM_TURNS = 3;
  20. var turnsLeft = NUM_TURNS;
  21. var bricksLeft = NUM_ROWS * NUM_BRICKS_PER_ROW;
  22.  
  23. function setupBall(){
  24.     ball = new Circle(BALL_RADIUS);
  25.     ball.setPosition(getWidth()/2, getHeight()/2);
  26.     add(ball);
  27. }
  28.  
  29. function setupPaddle(){
  30.     paddle = new Rectangle(PADDLE_WIDTH, PADDLE_HEIGHT);
  31.     paddle.setPosition(getWidth()/2 - paddle.getWidth()/2, getHeight() - paddle.getHeight() - PADDLE_OFFSET);
  32.     add(paddle);
  33. }
  34.  
  35. function getColorForRow(rowNum){
  36.     rowNum = rowNum % 8;
  37.     if(rowNum <= 1){
  38.         return Color.red;
  39.     }else if(rowNum > 1 && rowNum <= 3){
  40.         return Color.orange;
  41.     }else if(rowNum > 3 && rowNum <= 5){
  42.         return Color.green;
  43.     }else{
  44.         return Color.blue;
  45.     }
  46. }
  47.  
  48. function drawBrick(x, y, color){
  49.     var brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
  50.     brick.setPosition(x, y);
  51.     brick.setColor(color);
  52.     add(brick);
  53. }
  54.  
  55. function drawRow(rowNum, yPos){
  56.     var xPos = BRICK_SPACING;
  57.     for(var i = 0; i < NUM_BRICKS_PER_ROW; i++){
  58.         drawBrick(xPos, yPos, getColorForRow(rowNum));
  59.         xPos += BRICK_WIDTH + BRICK_SPACING;
  60.     }
  61. }
  62.  
  63. function drawBricks(){
  64.     var yPos = BRICK_TOP_OFFSET;
  65.     for(var i = 0; i < NUM_ROWS; i++){
  66.         drawRow(i, yPos);
  67.         yPos += BRICK_HEIGHT + BRICK_SPACING;
  68.     }
  69. }
  70.  
  71. function setSpeeds(){
  72.     vx = Randomizer.nextInt(2, 7);
  73.     if(Randomizer.nextBoolean()){
  74.         vx = -vx;
  75.     }
  76. }
  77.  
  78. function setup(){
  79.     drawBricks();
  80.     setupPaddle();
  81.     setupBall();
  82.     setSpeeds();
  83. }
  84.  
  85. function checkWalls(){
  86.     if(ball.getX() - ball.getRadius() < 0 || ball.getX() + ball.getRadius() > getWidth()){
  87.         vx = -vx;
  88.     }
  89.    
  90.     if(ball.getY() - ball.getRadius() < 0){
  91.         vy = -vy;
  92.     }
  93.    
  94.     if(ball.getY() + ball.getRadius() > getHeight()){
  95.         gameOver = true;
  96.     }
  97. }
  98.  
  99. function getCollidingObject(){
  100.     var left = ball.getX() + ball.getRadius();
  101.     var right = ball.getX() + ball.getRadius();
  102.    
  103.     var top = ball.getY() - ball.getRadius();
  104.     var bottom = ball.getY() + ball.getRadius();
  105.    
  106.     var topLeft = getElementAt(left, top);
  107.     if(topLeft){
  108.         return topLeft;
  109.     }
  110.    
  111.     var topRight = getElementAt(right, top);
  112.     if(topRight){
  113.         return topRight;
  114.     }
  115.    
  116.     var bottomLeft = getElementAt(left, bottom);
  117.     if(bottomLeft){
  118.         return bottomLeft;
  119.     }
  120.    
  121.     var bottomRight = getElementAt(left, bottom);
  122.     if(bottomRight){
  123.         return bottomRight;
  124.     }
  125. }
  126.  
  127. function checkObjects(){
  128.     var elem = getCollidingObject();
  129.     if(elem != null){
  130.         if(elem != paddle){
  131.             remove(elem);
  132.             vy = -vy;
  133.             bricksLeft--;
  134.         }else{
  135.             vy = -Math.abs(vy);
  136.         }
  137.     }
  138. }
  139.  
  140. function drawGameOver(){
  141.     var text = new Text("Game over", "25pt Arial");
  142.     text.setPosition(getWidth()/2 - text.getWidth()/2, getHeight()/2);
  143.     add(text);
  144. }
  145.  
  146. function drawGameWon(){
  147.     var text = new Text("You Win!", "25pt Arial");
  148.     text.setPosition(getWidth()/2 - text.getWidth()/2, getHeight()/2);
  149.     add(text);
  150. }
  151.  
  152. function checkWin(){
  153.     if(bricksLeft== 0){
  154.         stopTimer(draw);
  155.         drawGameWon();
  156.     }
  157. }
  158.  
  159. function checkLose(){
  160.     if(gameOver){
  161.         turnsLeft--;
  162.         remove(ball);
  163.         if(turnsLeft == 0){
  164.             stopTimer(draw);
  165.             drawGameOver();
  166.         }
  167.         else{
  168.             stopTimer(draw);
  169.             waitForClick();
  170.             setTimer(draw, 40);
  171.             setupBall();
  172.             setSpeeds();
  173.             gameOver = false;
  174.         }
  175.     }
  176. }
  177.  
  178. function draw(){
  179.     checkWalls();
  180.     checkObjects();
  181.     ball.move(vx, vy);
  182.     checkWin();
  183.     checkLose();
  184. }
  185.  
  186. function myMove(event){
  187.     var x = event.getX() - paddle.getWidth()/2;
  188.     if(x < 0){
  189.         x = 0;
  190.     }
  191.     if(x + paddle.getWidth() > getWidth()){
  192.         x = getWidth() - paddle.getWidth();
  193.     }
  194.     paddle.setPosition(x, paddle.getY());
  195. }
  196.  
  197. function start(){
  198.     setup();
  199.     //waitForClick();
  200.     setTimer(draw, 40);
  201.     mouseMoveMethod(myMove);
  202. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top