geogamerdachi

Breakout

Dec 2nd, 2020 (edited)
1,799
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Constants for bricks */
  2. var NUM_ROWS = 8;
  3. var BRICK_TOP_OFFSET = 10;
  4. var BRICK_SPACING = 2;
  5. var NUM_BRICKS_PER_ROW = 10;
  6. var BRICK_HEIGHT = 10;
  7. var SPACE_FOR_BRICKS = getWidth() - (NUM_BRICKS_PER_ROW + 1) * BRICK_SPACING;
  8. var BRICK_WIDTH = SPACE_FOR_BRICKS / NUM_BRICKS_PER_ROW;
  9.  
  10. /* Constants for ball and paddle */
  11. var PADDLE_WIDTH = 80;
  12. var PADDLE_HEIGHT = 15;
  13. var PADDLE_OFFSET = 10;
  14. var dx=2;
  15. var dy=5;
  16. var ball;
  17. var paddle;
  18. var BALL_RADIUS = 15;
  19. /* constants for bricks vol 2*/
  20. var color;
  21. var x;
  22. var y;
  23. var color;
  24. var l;
  25. var song;
  26. var brick;
  27. var count=0;
  28. var txt;
  29. var counter=80;
  30.  
  31. function start(){
  32.     println("to play game you need to move paddle with mouse,");
  33.     println("if you die 3 times you lose srry :( to try again click run  ");
  34.     sound();
  35.     draw();
  36.     ball=new Circle(BALL_RADIUS);
  37.     ball.setPosition(getWidth()/2- 5,getHeight()/2);
  38.     ball.setColor(Color.black);
  39.     add(ball);
  40.     paddle=new Rectangle(PADDLE_WIDTH,PADDLE_HEIGHT);
  41.     paddle.setColor(Color.black);
  42.     add(paddle);
  43.     setTimer(d2,20);
  44.     mouseMoveMethod(move);
  45.     checkBrick();
  46.    
  47.  
  48. }
  49. function sound(){
  50.   song=new Audio("https://codehs.com/uploads/d176ba6900cc2ff1459dcf83e66aeafa");
  51.     song.play();
  52.     song.loop= true;
  53. }
  54. y=BRICK_TOP_OFFSET;
  55. function d2(){
  56.   checkBrick();
  57.   checkWalls();
  58.   checkpaddle();
  59.   ball.move(dx,dy);
  60.  
  61. }
  62.  
  63. function draw(){
  64.     for(var i = 0; i < NUM_BRICKS_PER_ROW; i++){
  65.         for(var l = 0; l < NUM_ROWS; l++){
  66.  
  67.     brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
  68.     brick.setColor(Color.red);
  69.     brick.setPosition(BRICK_SPACING + BRICK_SPACING * i + BRICK_WIDTH * i,
  70.                         BRICK_TOP_OFFSET + BRICK_SPACING * l + BRICK_HEIGHT * l);
  71.     add(brick);
  72.         if(l == 2 || l == 3){
  73.         brick.setColor(Color.orange)
  74.         }else if(l == 4 || l == 5){
  75.         brick.setColor(Color.green)
  76.         }else if(l == 6 || l == 7){
  77.         brick.setColor(Color.blue)
  78.  
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. /* This function is bouncing ball*/
  85. function checkWalls(){
  86.     // Bounce off right wall
  87.     if(ball.getX() + ball.getRadius() > getWidth()){
  88.         dx = -dx;
  89.     }
  90.    
  91.     // Bounce off left wall
  92.     if(ball.getX() - ball.getRadius() < 0){
  93.         dx = -dx;
  94.     }
  95.     // Bounce off top wall
  96.     if(ball.getY() - ball.getRadius() < 0){
  97.         dy = -dy;
  98.     }
  99.     if(paddle.getX() + PADDLE_WIDTH>getWidth()){
  100.         PADDLE_WIDTH= -PADDLE_WIDTH;
  101.     }
  102.     if(ball.getY()>getHeight()){
  103.         count++
  104.         if(count<3){
  105.             stopTimer(d2);
  106.             song.pause();
  107.            respawn();
  108.  
  109.         }
  110.         dead();
  111.        
  112.     }
  113. }
  114. function dead(){
  115.     if(count==3){
  116.         text("GAME OVER");
  117.         stopTimer(d2);
  118.         song.pause();
  119.     }
  120. }
  121.  
  122. function respawn(){
  123.     if(ball.getX()>0 && ball.getX()<getWidth()){
  124.         if(count<3){
  125.         remove(ball);
  126.         ball=new Circle(BALL_RADIUS);
  127.         ball.setPosition(getWidth()/2- 5,getHeight()/2);
  128.         add(ball);
  129.         song.play();
  130.         add(paddle);
  131.         setTimer(d2,20);
  132.         }
  133.     }
  134. }
  135. function text(t){
  136.     remove(text);
  137.     txt=new Text(t,"40pt arial");
  138.     txt.setPosition(getWidth()/2 - 155,getHeight()/2);
  139.     txt.setColor(Color.red);
  140.     add(txt);
  141. }
  142. /* this function is to move paddle */
  143.  
  144. function move(e){
  145.     paddle.setPosition(e.getX(), getHeight() - PADDLE_OFFSET - PADDLE_HEIGHT);
  146.  
  147. }
  148. function checkBrick(){
  149. var elem = getElementAt(ball.getX(), ball.getY() - ball.getRadius());
  150.  
  151. var ele= getElementAt(ball.getX() - ball.getRadius(), ball.getY());
  152.  
  153. var elem2= getElementAt(ball.getX() + ball.getRadius(), ball.getY());
  154.  
  155.  
  156.  
  157. if(elem != null){
  158.  
  159. if(elem.getColor()!=Color.white){
  160.  
  161. remove(elem);
  162. counter--;
  163.  
  164. if(counter==0){
  165.  
  166. stopTimer(d2);
  167.  
  168. text("YOU WIN !");
  169.  
  170. }
  171.  
  172. }
  173.  
  174. dy = -dy;
  175.  
  176. }
  177.  
  178.  
  179. if(ele != null ){
  180.  
  181. if(ele.getColor()!=Color.white){
  182.  
  183. remove(ele);
  184. counter--;
  185.  
  186.  
  187. if(counter== 0){
  188. println(counter);
  189. stopTimer(d2);
  190.  
  191. text("YOU WIN !");
  192.  
  193. }
  194.  
  195. }
  196.  
  197. dx = -dx;
  198.  
  199. }
  200.  
  201.  
  202.  
  203. if(elem2 != null){
  204.  
  205. if(elem2.getColor()!=Color.white){
  206.  
  207. remove(elem2);
  208.  
  209. if(counter== 0){
  210.  
  211.  
  212.  
  213. stopTimer(d2);
  214.  
  215. text("YOU WIN !");
  216.  
  217. }
  218.  
  219. }
  220.  
  221. dx = -dx;
  222.  
  223. }
  224. }
  225. /* this function is to bounce ball from paddle */
  226. function checkpaddle(){
  227. var el = getElementAt(ball.getX(), ball.getY() + ball.getRadius());
  228.  if(el ==paddle){
  229. dy= -dy;
  230. }
  231. }
RAW Paste Data