Advertisement
vinci98gl2

PongProv0.4.1

Nov 29th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. float ballx;
  2. float bally;
  3. float dy = -1;  //direction of the ball on y
  4. float dx = random(5); //direction of the ball on x
  5. float ballSize;
  6. float ballSpeed;
  7. boolean inPlay = false;
  8. int score = 0;
  9. int highscore = 0;
  10.  
  11. int paddleWidth;
  12. int paddleHeight;
  13. float paddlex; //paddle position
  14.  
  15. int distWall = 15;
  16.  
  17. int bricksNum = 10;
  18. Brick bricks[] = new Brick[bricksNum];
  19. float minDistance = 200;
  20.  
  21. void setup(){
  22.   size(displayWidth, displayHeight);
  23.   rectMode(RADIUS);
  24.   ellipseMode(RADIUS);
  25.   noStroke();
  26.   smooth();
  27.  
  28.   paddleWidth = displayWidth / 10;
  29.   paddleHeight = displayHeight / 60;
  30.   ballSize = min(displayWidth, displayHeight)/50;
  31.  
  32.   bally = 15;
  33.   ballx = width / 2.0;
  34. }
  35.  
  36. void draw(){
  37.   if(inPlay){
  38.     background(51);
  39.    
  40.     bally += dy * ballSpeed;
  41.     ballx += dx;
  42.     paddlex = constrain(mouseX, paddleWidth, width - paddleWidth);
  43.    
  44.     checkOutOfScreen();
  45.     checkHitsBallPaddle();
  46.     checkHitsBallBackWall();
  47.     checkHitsBallEdges();
  48.     checkHitsBallBrick();
  49.    
  50.     //draw
  51.     fill(255);
  52.     ellipse(ballx,bally,ballSize,ballSize);
  53.    
  54.     fill(153);
  55.     rect(paddlex, height - distWall, paddleWidth, paddleHeight);
  56.    
  57.     fill(180);
  58.     rectMode(CORNER);
  59.     for(int i=0; i<bricksNum; i++){
  60.       try{
  61.         rect(bricks[i].getX(),bricks[i].getY(),bricks[i].getBrickWidth(),bricks[i].getBrickHeight());
  62.       }
  63.       catch(NullPointerException e){
  64.         if(score%10==0){
  65.           newBricks();
  66.           ballSpeed *= 1.5;
  67.         }
  68.       }
  69.     }
  70.     rectMode(RADIUS);
  71.    
  72.     textSize(width/80);
  73.     textAlign(LEFT);
  74.     if(score < highscore) fill(255,0,0);
  75.     else fill(0,255,0);
  76.     text("highscore: "+highscore, distWall, 30);
  77.     text("score: "+score, distWall, 60);
  78.   }
  79.   else{
  80.     background(51);
  81.     fill(0,255,0); //green
  82.     textSize(width/40);
  83.     textAlign(CENTER);
  84.     text("Tap anyware to Play",width/2,height/2);
  85.     textSize(width/80);
  86.     textAlign(LEFT);
  87.     if(score < highscore) fill(255,0,0);
  88.     text("highscore: "+highscore, distWall, 30);
  89.     text("score: "+score, distWall, 60);
  90.   }
  91. }
  92.  
  93. void checkOutOfScreen(){
  94.   if(bally > height + ballSize){ //fuori dallo schermo
  95.     ballx = width / 2 - ballSize;
  96.     bally = 15;
  97.     dx = random(5);
  98.     inPlay = false;
  99.   }
  100. }
  101.  
  102. void checkHitsBallPaddle(){  
  103.   if(bally >= height - distWall - paddleHeight - ballSize
  104.   && ballx > paddlex - paddleWidth - ballSize
  105.   && ballx < paddlex + paddleWidth + ballSize){
  106.    
  107.     dy *= -1;
  108.    
  109.     if(bally >= height - distWall - paddleHeight - ballSize && bally < height){
  110.       //Anti-Bouncing
  111.       dy = -1;
  112.       bally -= paddleHeight;
  113.       bally -= distWall;
  114.     }
  115.    
  116.     if(mouseX != pmouseX){ //abbotta system! XD
  117.       dx = (mouseX - pmouseX) / 2.0;
  118.       dx = constrain(dx, -5, 5);
  119.     }
  120.   }
  121. }
  122.  
  123. void checkHitsBallBackWall(){
  124.   if(bally < ballSize && dy == -1){ //ball hits back wall
  125.     dy *= -1;
  126.   }
  127. }
  128.  
  129. void checkHitsBallEdges(){
  130.   //ball hits top or bottom edges
  131.   if(ballx > width - ballSize){
  132.     dx *= -1;
  133.   }
  134.   if(ballx < ballSize){
  135.     dx *= -1;
  136.   }
  137. }
  138.  
  139. void checkHitsBallBrick(){
  140.   for(int i=0; i<bricksNum;i++){
  141.     try{
  142.       if(ballx >= bricks[i].getX() && ballx <= bricks[i].getX()+bricks[i].getBrickWidth()
  143.       && bally >= bricks[i].getY()-ballSize && bally <= bricks[i].getY()+ballSize){
  144.         //up collision
  145.         dy = -1;
  146.         score++;
  147.         bricks[i]=null;
  148.       }
  149.       if(ballx >= bricks[i].getX() && ballx <= bricks[i].getX()+bricks[i].getBrickWidth()
  150.       && bally >= bricks[i].getY()-ballSize+bricks[i].getBrickHeight()
  151.       && bally <= bricks[i].getY()+ballSize+bricks[i].getBrickHeight()){
  152.         //down collision
  153.         dy = 1;
  154.         score++;
  155.         bricks[i]=null;
  156.       }
  157.       if(bally >= bricks[i].getY() && bally <= bricks[i].getY()+bricks[i].getBrickHeight()
  158.       && ballx >= bricks[i].getX()-ballSize && ballx <= bricks[i].getX()+ballSize){
  159.         //left collision
  160.         dx *= -1;
  161.         score++;
  162.         bricks[i]=null;
  163.       }
  164.       if(bally >= bricks[i].getY() && bally <= bricks[i].getY()+bricks[i].getBrickHeight()
  165.       && ballx >= bricks[i].getX()-ballSize+bricks[i].getBrickWidth()
  166.       && ballx <= bricks[i].getX()+ballSize+bricks[i].getBrickWidth()){
  167.         //right collision
  168.         dx *= 1;
  169.         score++;
  170.         bricks[i]=null;
  171.       }    
  172.     }
  173.     catch(NullPointerException e){
  174.    
  175.     }
  176.   }
  177. }
  178. void newBricks(){
  179.   for(int i=0;i<bricksNum;i++){
  180.     bricks[i] = new Brick(minDistance);
  181.   }
  182. }
  183.  
  184. void mousePressed(){
  185.   if(!inPlay){
  186.       newBricks();
  187.       inPlay = true;
  188.       highscore = score;
  189.       score = 0;
  190.       ballSpeed = height/100;    
  191.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement