Game Development Reference
In-Depth Information
components that are used to input the initial velocity components of the golf ball. These values
can be changed to adjust the trajectory of the ball. Another text field adjusts the distance from
the tee to the flag, and a combo box lets the user select either a side or top-down view.
Figure 5-4. A sample Golf Game screen shot
A Fire button fires the golf ball, and a Reset button is used to stop the simulation and
return the golf ball to the tee. Below the buttons and text fields is a white drawing area that
displays the ball as it flies through the air. If you get the ball to within 10 m of the flag, it is
considered to be “on the green” and you win.
The Golf Game makes use of the SimpleProjectile class that was developed in the
previous section to model the flight of the golf ball. The GUI elements of the game are
contained in a class called GolfGame . As with the previous games in this topic, the code that sets
up the GUI components will not be shown in detail, but instead the focus will be on the projec-
tile modeling elements of the code.
Along with fields representing the GUI components of the game, the GolfGame class declares
a SimpleProjectile object that will represent the golf ball. Additional fields store the value of
the distance from the tee to the hole and encapsulate some images that are used in the game.
A Timer object is created that will be used to control how quickly the simulation runs.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import javax.swing.Timer;
Search WWH ::




Custom Search