Game Development Reference
In-Depth Information
public GolfGame() {
// Create a SimpleProjectile object.
golfball =
new SimpleProjectile(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
// Initialize the distanceToHole field.
distanceToHole = 200.0;
// Create a Timer object that will be used
// to slow the action down and an ActionListener
// that the Timer will call. The timeDelay variable
// is the time delay in milliseconds.
gameUpdater = new GameUpdater();
int timeDelay = 50;
gameTimer = new Timer(timeDelay, gameUpdater);
// Set up some images and determine their dimensions.
golferIcon = new ImageIcon("Golfer.jpg");
golferWidth = golferIcon.getIconWidth();
golferHeight = golferIcon.getIconHeight();
flagIcon = new ImageIcon("Hole_Cartoon.jpg");
flagWidth = flagIcon.getIconWidth();
flagHeight = flagIcon.getIconHeight();
// GUI component initialization not shown.
When the Fire button is pressed for the first time, the golf ball is struck and starts to fly
through the air. Values for the initial golf ball velocity components are obtained from the text
that is inside the text fields. These initial values are used to create a
SimpleProjectile
object
that will represent the golf ball. The
distanceToHole
field value is set, and the display is updated
to show the golf flag in its current location. A
Timer
object is used to control the game flow.
When the
start
method is called on the
Timer
object, the simulation begins.
// The actionPerformed() method is called when
// the Fire button is pressed.
public void actionPerformed(ActionEvent event) {
// Get the initial velocities and distance-to-hole
// from the text fields.
double vx0 = Double.parseDouble(vxTextField.getText());
double vy0 = Double.parseDouble(vyTextField.getText());
double vz0 = Double.parseDouble(vzTextField.getText());
distanceToHole = Double.parseDouble(distanceTextField.getText());