Game Development Reference
In-Depth Information
Figure 4-2. A sample Beanbag Game screen shot
In the vertical direction, the beanbag will be subject to gravitational acceleration. The
vertical location of the beanbag at any point in time is a function of the gravitational accelera-
tion, the initial vertical location, and the initial z-velocity component.
1
2
2
zz vt
=+ −
t
(4.18)
0
z
0
The Beanbag Game is implemented in a class named BeanBag . The development of the
class is very similar to the games that were written in the previous chapter, so we won't go over
the entire code listing in detail, but we will focus on the elements that are used to compute the
motion of the beanbag. In addition to GUI component declarations, the BeanBag class also declares
fields that represent the initial and current horizontal and vertical locations and velocities of
the beanbag.
public class BeanBag extends JFrame implements ActionListener
{
private JTextField vxTextField;
private JTextField vzTextField;
private JLabel vxLabel;
private JLabel vzLabel;
private JButton fireButton;
private JButton resetButton;
private JPanel drawingPanel;
private GridBagConstraints gbc;
private double z; // altitude of beanbag
private double z0; // initial altitude of beanbag
private double vz0; // initial vertical velocity
private double x; // horizontal location
private double x0; // initial horizontal location
private double vx0; // initial horizontal velocity
private double time;
As with the previous games, the BeanBag class makes use of a Timer object to control the
execution of the game. The Timer object is set up to call the actionPerformed method every
0.05 seconds to compute the current position of the beanbag and to update the display.
Search WWH ::




Custom Search