Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Paddle leftPaddle;
- Paddle rightPaddle;
- Ball pongBall;
- Block[] gameElements;
- //declare private ints for leftScore and rightScore here
- void setup() {
- size(640, 480);
- //instantiate all objects, use the other constructors
- leftPaddle = new Paddle();
- rightPaddle = new Paddle();
- pongBall = new Ball();
- //initialize leftScore and rightScore
- }
- void draw() {
- background(255);
- leftPaddle.display();
- rightPaddle.display();
- pongBall.moveAndDisplay();
- if ( keyPressed && keyCode == UP) {
- leftPaddle.moveUpAndDisplay();
- }
- if ( keyPressed && keyCode == DOWN) {
- leftPaddle.moveDownAndDisplay();
- }
- //set up other keys for right paddle
- //see if ball hits left wall or right wall - a goal
- if (!(pongBall.getX() >= 10 && pongBall.getX() <= width - 10)) {
- pongBall.setXSpeed(0);
- pongBall.setYSpeed(0);
- //Add scoring logic here - who scores based on where the ball is?
- //The lines below pause the game for about a second if a goal was scored
- try {
- Thread.currentThread().sleep(950);
- }
- catch(Exception e) {
- }
- //Then the ball is moved back close to the middle
- pongBall.display(color(255));
- pongBall.setX((int)(Math.random() * 50) + width / 2);
- pongBall.setY((int)(Math.random() * 50)+ height / 2);
- //Then a random direction is chosen for the ball
- int whoot = (int)(Math.random() * 2);
- if (whoot == 0) {
- pongBall.setXSpeed(2);
- pongBall.setYSpeed(1);
- } else {
- pongBall.setXSpeed(-2);
- pongBall.setYSpeed(1);
- }
- //Uncomment the below two lines when rightScore and leftScore are ready
- //text("rightScore = "+rightScore, width / 2, 540);
- //text("leftScore = "+leftScore, width / 2, 560);
- //see if the ball hits the top (y=20) or bottom (y=450)wall and if so, switch its y velocity
- //see if the ball hits the left paddle and bounce appropriately
- //see if the ball hits the right paddle and bounce appropriately
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment