Advertisement
fosterbl

PongProcessing4Tester

Feb 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. Paddle leftPaddle;
  2. Paddle rightPaddle;
  3. Ball pongBall;
  4. Block[] gameElements;
  5. //declare private ints for leftScore and rightScore here
  6.  
  7. void setup() {
  8.   size(640, 480);
  9.  
  10.   //instantiate all objects, use the other constructors
  11.   leftPaddle = new Paddle();
  12.   rightPaddle = new Paddle();
  13.   pongBall = new Ball();
  14.  
  15.   //initialize leftScore and rightScore
  16. }
  17.  
  18. void draw() {
  19.   background(255);
  20.  
  21.   leftPaddle.display();
  22.   rightPaddle.display();
  23.   pongBall.moveAndDisplay();
  24.  
  25.   if ( keyPressed && keyCode == UP) {
  26.     leftPaddle.moveUpAndDisplay();
  27.   }
  28.   if ( keyPressed && keyCode == DOWN) {
  29.     leftPaddle.moveDownAndDisplay();
  30.   }
  31.   //set up other keys for right paddle
  32.  
  33.   //see if ball hits left wall or right wall - a goal
  34.   if (!(pongBall.getX() >= 10 && pongBall.getX() <= width - 10)) {
  35.     pongBall.setXSpeed(0);
  36.     pongBall.setYSpeed(0);
  37.  
  38.     //Add scoring logic here - who scores based on where the ball is?
  39.  
  40.     //The lines below pause the game for about a second if a goal was scored
  41.     try {
  42.       Thread.currentThread().sleep(950);
  43.     }
  44.     catch(Exception e) {
  45.     }  
  46.  
  47.     //Then the ball is moved back close to the middle
  48.     pongBall.display(color(255));
  49.     pongBall.setX((int)(Math.random() * 50) + width / 2);
  50.     pongBall.setY((int)(Math.random() * 50)+ height / 2);
  51.  
  52.     //Then a random direction is chosen for the ball
  53.     int whoot = (int)(Math.random() * 2);
  54.     if (whoot == 0) {
  55.       pongBall.setXSpeed(2);
  56.       pongBall.setYSpeed(1);
  57.     } else {
  58.       pongBall.setXSpeed(-2);
  59.       pongBall.setYSpeed(1);
  60.     }
  61.  
  62.     //Uncomment the below two lines when rightScore and leftScore are ready
  63.     //text("rightScore = "+rightScore, width / 2, 540);
  64.     //text("leftScore = "+leftScore, width / 2, 560);
  65.  
  66.  
  67.     //see if the ball hits the top (y=20) or bottom (y=450)wall and if so, switch its y velocity
  68.  
  69.  
  70.  
  71.     //see if the ball hits the left paddle and bounce appropriately
  72.  
  73.  
  74.  
  75.     //see if the ball hits the right paddle and bounce appropriately
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement