Hardware Reference
In-Depth Information
If the game is over, the program
stops the serving and displays the
winner for four seconds.
Continued from previous page.
8
// if the game is over, show the winner:
if (gameOver) {
textSize(24);
gameOver = true;
text("Game Over", width/2, height/2 - 30);
if (topScore > bottomScore) {
text("Top Team Wins!", width/2, height/2);
}
else {
text("Bottom Team Wins!", width/2, height/2);
}
}
// pause after each game:
if (gameOver && (millis() > delayCounter + gameOverDelay)) {
gameOver = false;
newGame();
}
After each point is scored, the
program takes a two-second
pause. If there aren't at least two
players after that pause, it doesn't
serve another ball. This is to keep the
game from running when there's no
one playing.
8
// pause after each point:
if (!gameOver && !ballInMotion && (millis() >
delayCounter + pointDelay)) {
// make sure there are at least two players:
if (playerList.size() >=2) {
ballInMotion = true;
}
else {
ballInMotion = false;
textSize(24);
text("Waiting for two players", width/2, height/2 - 30);
// reset the score:
newGame();
}
}
}
That closes out the drawGame()
method itself. It calls a few other
methods: moveBall(), which calculates
the ball's trajectory; showScore() ,
which shows the score; and
newGame() , which resets the game.
Those are shown next.
First, moveBall() checks whether
the position of the ball intersects
any of the Players' paddles. To do this,
it has to iterate over playerList , pull out
each Player, and check to see whether
the ball position is contained within the
rectangle of the paddle. If the ball does
intersect a paddle, its vertical direction
is reversed.
8
void moveBall() {
// Check to see if the ball contacts any paddles:
for (int p = 0; p < playerList.size(); p++) {
// get the player to check:
Player thisPlayer = (Player)playerList.get(p);
// calculate the horizontal edges of the paddle:
float paddleRight = thisPlayer.paddleH + paddleWidth/2;
float paddleLeft = thisPlayer.paddleH - paddleWidth/2;
// check to see if the ball is in the horizontal range of the paddle:
if ((ballPosH >= paddleLeft) && (ballPosH <= paddleRight)) {
ยป
 
Search WWH ::




Custom Search