Hardware Reference
In-Depth Information
Continued from previous page.
// calculate the vertical edges of the paddle:
float paddleTop = thisPlayer.paddleV - paddleHeight/2;
float paddleBottom = thisPlayer.paddleV + paddleHeight/2;
// check to see if the ball is in the
// horizontal range of the paddle:
if ((ballPosV >= paddleTop) && (ballPosV <= paddleBottom)) {
// reverse the ball's vertical direction:
ballDirectionV = -ballDirectionV;
}
}
}
If the ball goes above the top of the
screen or below the bottom, then one
team has scored. If any team goes over
five points, the game is over.
8
// 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;
}
8
Finally, moveBall() checks to see
whether the ball hits one of the sides
of the screen. If so, the horizontal
direction is reversed.
// 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;
}
The newGame() method just
restarts the game play and resets
the scores.
void newGame() {
gameOver = false;
topScore = 0;
bottomScore = 0;
}
8
 
Search WWH ::




Custom Search