
392 MAKING THINGS TALK
// check to see if the ball is in the
// horizontal range of the paddle:
if ((ballPosV >= paddleTop) && (ballPosV <= paddleBottom)) {
// reverse the ball vertical direction:
ballDirectionV = -ballDirectionV;
}
}
}
// if the ball goes off the screen top:
if (ballPosV < 0) {
bottomScore++;
ballDirectionV = int(random(2) + 1) * -1;
resetBall();
}
// if the ball goes off the screen bottom:
if (ballPosV > height) {
topScore++;
ballDirectionV = int(random(2) + 1);
resetBall();
}
// if any team goes over 5 points, the other team loses:
if ((topScore > 5) || (bottomScore > 5)) {
delayCounter = millis();
gameOver = true;
}
// stop the ball going off the left or right of the screen:
if ((ballPosH - ballSize/2 <= 0) || (ballPosH +ballSize/2 >=width)) {
// reverse the y direction of the ball:
ballDirectionH = -ballDirectionH;
}
// update the ball position:
ballPosV = ballPosV + ballDirectionV;
ballPosH = ballPosH + ballDirectionH;
}
void newGame() {
gameOver = false;
topScore = 0;
bottomScore = 0;
}
public void showScore() {
textSize(24);
text(topScore, 20, 40);
text(bottomScore, 20, height - 20);
}
void resetBall() {
// put the ball back in the center
ballPosV = height/2;
ballPosH = width/2;
ballInMotion = false;
delayCounter = millis();
}
public class Player {
// declar