Hardware Reference
In-Depth Information
Now you're ready to animate the
ball. It should move only if it's been
served. This code goes at the end
of the draw() method. The first if()
statement starts the ball in motion
when the serve button is pressed. The
second moves it if it's in service. The
third resets the ball to the center, and
resets the score when the reset button
is pressed.
8
// calculate the ball's position and draw it:
if (ballInMotion == true) {
animateBall();
}
// if the serve button is pressed, start the ball moving:
if (serveButton == 1) {
ballInMotion = true;
}
// if the reset button is pressed, reset the scores
// and start the ball moving:
if (resetButton == 1) {
leftScore = 0;
rightScore = 0;
ballInMotion = true;
}
Modify the animateBall() method
so that when the ball goes off the
screen left or right, the appropriate
score is incremented (added lines are
shown in blue).
8
// if the ball goes off the screen left:
if (xPos < 0) {
rightScore++;
resetBall();
}
// if the ball goes off the screen right:
if (xPos > width) {
leftScore++;
resetBall();
}
To include the scoring display, add
a new global variable before the
setup() method.
8
int fontSize = 36; // point size of the scoring font
Then add two lines before the
end of the setup() method to initial-
ize the font.
8
// create a font with the third font available to the system:
PFont myFont = createFont(PFont.list()[2], fontSize);
textFont(myFont);
Finally, add two lines before the
end of the draw() method to
display the scores.
8
// print the scores:
text(leftScore, fontSize, fontSize);
text(rightScore, width-fontSize, fontSize);
Now you can play Monski Pong! Figure
2-14 shows the game in action. For
added excitement, get a second pink
monkey and put one sensor in each
monkey so you can play with a friend.
 
Search WWH ::




Custom Search