Game Development Reference
In-Depth Information
// Create a SimpleProjectile object.
golfball = new SimpleProjectile(0.0, 0.0, 0.0,
vx0, vy0, vz0, 0.0);
// Update the display.
updateDisplay();
// Start the box sliding using a Timer object
// to slow down the action.
gameTimer.start();
}
The GolfGame class declares an inner class named GameUpdater that declares an
actionPerformed method. The Timer object calls this method to update the velocities and loca-
tion of the golf ball by calling the updateLocationAndVelocity method of the SimpleProjectile
class. Recall from the previous section of this chapter that the updateLocationAndVelocity
method solves the gravity-only equations of motion.
// This ActionListener is called by the Timer.
class GameUpdater implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Update the time and compute the new position
// of the golfball.
double timeIncrement = 0.07;
golfball.updateLocationAndVelocity(timeIncrement);
// Update the display.
updateDisplay();
If the z-location of the golf ball is zero, it means that the ball has hit the ground. The simu-
lation is stopped. If the ball lands within 10 m of the hole, it is considered to be on the green,
and the triumphant “You're on the green” message is displayed on the screen. Otherwise, the
disappointing “You missed” message is displayed.
// Access the Graphics object of the drawing panel.
Graphics g = drawingPanel.getGraphics();
// When the golfball hits the ground, stop the simulation
// and see where ball has landed.
if ( golfball.getZ() <= 0.0 ) {
// Stop the simulation.
gameTimer.stop();
Search WWH ::




Custom Search