Game Development Reference
In-Depth Information
shown in Figure 3-5. When the Start button is pressed, a black disk slides across the surface of
the board with an initial velocity that is determined by the user. A friction force between the
disk and the board resists the motion of the disk. When the disk comes to rest, the user is
awarded the score according to the section of the board that the disk occupies. The user can
specify the mass of the disk and the coefficient of friction between the disk and the surface of
the board. The Reset button is used to reset the display.
Figure 3-5. Shuffleboard Game screen shot
The GUI setup of the Shuffleboard Game is very similar to the Gravity Game, so we won't
spend much time going over the GUI aspects of the code. As with the Gravity Game, the
Shuffleboard class makes use of a Timer object to slow the action of the game down. When the
Start button is pressed, the actionPerformed method that is declared in the Shuffleboard class
is called. The actionPerformed method extracts the values of the friction coefficient, disk mass,
and initial disk velocity from the values inside the text fields. The time and disk location values
are reset to zero and the start method is called on the Timer object to start the disk moving.
public void actionPerformed(ActionEvent event) {
// Extract initial data from the text fields.
mu = Double.parseDouble(muTextField.getText());
mass = Double.parseDouble(massTextField.getText());
initialVelocity = Double.parseDouble(velocityTextField.getText());
// Set the time and the initial x-location of the disk
time = 0.0;
xLocation = 0.0;
// Start the disk sliding using a Timer object
// to slow down the action.
gameTimer.start();
}
Search WWH ::




Custom Search