Create a Whack A Mole game application that emulates its popular arcade counterpart. Allow players to start a new game by clicking a button. Then, a mole should appear randomly within a single cell of an outlined grid. Clicking on the mole before it moves will add 50 points to the score. Playing the game should result in output similar to Fig. 27.36.



a) Copying the template to your working directory. Copy the C:Examples Tutorial27ExercisesWhackAMole directory to your C:SimplyJava directory.

b) Opening the template file. Open the Mole.java file in your text editor.

c) Declaring local variables in the drawMole method. At line 23, add a comment indicating that the cell dimensions will be calculated. At line 24, declare and initialize a local variable of type int named x. Set x equal to moleColumn * 50. Next, declare and initialize another local variable of type int named y. Set y equal to moleRow * 50. Variables x and y represent the x- and y-coordinates in pixels of each cell. These variables will be used in later calculations.

d) Drawing the mole’s head in the drawMole method. At line 27, add a comment indi- cating that the mole’s head color will be set. Now, notice that the parameter list of the drawMole method indicates that it will be passed an instance of Graphics named g. On line 28, call the setColor method on g. Pass a new Colo

```
1 // WhackAMole.java
2 // This game challenges a player to try to whack the mole.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class WhackAMole extends JFrame
8 {
9 // JButton to start a new game
10 private JButton newGameJButton;
11
12 // JLabel and JTextField for points
13 private JLabel pointsJLabel;
14 private JTextField pointsJTextField;
15
16 // JLabel and JTextField for time remaining
17 private JLabel timeJLabel;
18 private JTextField timeJTextField;
19
20 // counter for moles whacked
21 private int points = 0;
22
23 // variable to count down the seconds left
24 private int countDown = 30;
25
26 // Timer for the moles
27 private Timer moleTimer;
28
29 // Timer for game time
30 private Timer gameTimer;
31
32 private Mole gameMole;
33
34 // no-argument constructor
35 public WhackAMole()
36 {
37 gameMole = new Mole(); // declare new Mole object
38
39 createUserInterface();
40 }
41
42 // create and position GUI components; register event handlers
43 private void createUserInterface()
44 {
45 // get content pane for attaching GUI components
46 Container contentPane = getContentPane();
47
48 // enable explicit positioning of GUI components
49 contentPane.setLayout( null );
50
51 // set up newGameJButton
52 newGameJButton = new JButton();
53 newGameJButton.setBounds( 100, 8, 125, 23 );
54 newGameJButton.setText( "New Game" );
55 contentPane.add ( newGameJButton );
56 newGameJButton.addActionListener(
57
58 new ActionListener() // anonymous inner class
59 {
60 // event handler called when newGameJButton is pressed
61 public void actionPerformed( ActionEvent event )
62 {
63 newGameJButtonActionPerformed( event );
64 }
65
66 } // end anonymous inner class
67
68 ); // end call to addActionListener
69
70 // set up pointsJLabel
71 pointsJLabel = new JLabel();
72 pointsJLabel.setBounds( 16, 315, 50, 23 );
73 pointsJLabel.setText( "Points:" );
74 contentPane.add( pointsJLabel );
75
76 // set up pointsJTextField
77 pointsJTextField = new JTextField();
78 pointsJTextField.setBounds( 60, 315, 40, 23 );
79 pointsJTextField.setText( "0" );
80 pointsJTextField.setHorizontalAlignment( JTextField.RIGHT );
81 pointsJTextField.setEditable( false );
82 contentPane.add( pointsJTextField );
83
84 // set up timeJLabel
85 timeJLabel = new JLabel();
86 timeJLabel.setBounds( 130, 315, 100, 23 );
87 timeJLabel.setText( "Time Remaining:" );
88 contentPane.add( timeJLabel );
89
90 // set up timeJTextField
91 timeJTextField = new JTextField();
92 timeJTextField.setBounds( 243, 315, 40, 23 );
93 timeJTextField.setText( "30" );
94 timeJTextField.setHorizontalAlignment( JTextField.RIGHT );
95 timeJTextField.setEditable( false );
96 contentPane.add( timeJTextField );
97
98 // set up moleTimer
99 moleTimer = new Timer( 550,
100
101 new ActionListener() // anonymous inner class
102 {
103 // timer has incriminated
104 public void actionPerformed( ActionEvent event )
105 {
106 moleTimerActionPerformed();
107 }
108 } // end anonymous inner class
109
110 ); // end new Timer
111
112 // set up gameTimer
113 gameTimer = new Timer( 1000,
114
115 new ActionListener() // anonymous inner class
116 {
117 // timer has incriminated
118 public void actionPerformed( ActionEvent event )
119 {
120 gameTimerActionPerformed();
121 }
122 } // end anonymous inner class
123
124 ); // end new Timer
125
126 addMouseListener(
127
128 new MouseAdapter() // anonymous inner class
129 {
130 // event handler called when mouse button is pressed
131 public void mousePressed( MouseEvent event )
132 {
133 whackAMoleMousePressed( event );
134 }
135
136 } // end anonymous inner class
137
138 ); // end call to addMouseListener
139
140 // set properties of application's window
141 setTitle( "Whack A Mole" ); // set title bar string
142 setSize( 320, 380 ); // set window size
143 setVisible( true ); // display window
144
145 } // end method createUserInterface
146
147 // paint the chart
148 public void paint( Graphics g )
149 {
150 super.paint( g );
151
152 // draw vertical chart lines
153 for ( int i = 0; i <= 250; i += 50 )
154 {
155 g.drawLine( 35 + i, 68, 35 + i, 318 );
156 }
157
158 // draw horizontal chart lines
159 for ( int i = 0; i <= 250; i += 50 )
160 {
161 g.drawLine( 35, 68 + i, 285, 68 + i );
162 }
163
164 gameMole.drawMole( g );
165
166 } // end method paint
167
168 // change the position of the mole
169 private void moleTimerActionPerformed()
170 {
171 gameMole.moveMole(); // move the mole to a new hole
172
173 repaint(); // repaint JFrame
174
175 } // end event handler moleTimerActionPerformed
176
177 // ends the Whack A Mole game if time reaches zero
178 private void gameTimerActionPerformed()
179 {
180 countDown--; // decrement counter
181
182 // display time remaining
183 timeJTextField.setText( String.valueOf( countDown ) );
184
185 // if less than ten seconds are remaining
186 if ( countDown < 10 )
187 {
188 // change the color of the text in timeJTextField to red
189 timeJTextField.setForeground( Color.RED );
190 }
191
192 // when time runs out
193 if ( countDown == 0 )
194 {
195 // stop timers
196 gameTimer.stop();
197 moleTimer.stop();
198
199 newGameJButton.setEnabled( true ); // enable newGameJButton
200
201 // reset and hide the mole
202 gameMole.reset();
203 repaint();
204
205 // display score in a JOptionPane
206 JOptionPane.showMessageDialog( this, "Your score is " +
207 points + " points", "Game Over",
208 JOptionPane.INFORMATION_MESSAGE );
209
210 } // end if
211
212 } // end method gameTimerActionPerformed
213
214 // generate a new Whack A Mole game
215 private void newGameJButtonActionPerformed( ActionEvent event )
216 {
217 // reset counters
218 points = 0;
219 countDown = 30;
220
221 // reset JTextFields
222 pointsJTextField.setText( String.valueOf( points ) );
223 timeJTextField.setForeground( Color.BLACK );
224 timeJTextField.setText( String.valueOf( countDown ) );
225
226 newGameJButton.setEnabled( false ); // disable newGameJButton
227
228 // start the moleTimer and gameTimer
229 moleTimer.start();
230 gameTimer.start();
231
232 // display first mole
233 gameMole.moveMole();
234 repaint();
235
236 } // end method newGameJButtonActionPerformed
237
238 // user clicked the mouse button
239 private void whackAMoleMousePressed( MouseEvent event )
240 {
241 // if mole is hit
242 if ( gameMole.isHit( event.getX(), event.getY() ) )
243 {
244 points += 50; // add 50 points to the score
245
246 // display new score
247 pointsJTextField.setText( String.valueOf( points ) );
248
249 moleTimer.restart(); // restart the moleTimer
250
251 // display next mole
252 gameMole.moveMole();
253 repaint();
254
255 } // end if
256
257 } // end method whackAMoleMousePressed
258
259 // main method
260 public static void main( String[] args )
261 {
262 WhackAMole application = new WhackAMole();
263 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
264
265 } // end method main
266
267 } // end class WhackAMole
```

```
1 // Mole.java
2 // A class that represents the mole in the Whack A Mole application.
3 import java.awt.*;
4 import java.util.*;
5
6 public class Mole
7 {
8 // int for storing the row in which the mole is in
9 private int moleRow = -1;
10
11 // int for storing the column in which the mole is in
12 private int moleColumn = -1;
13
14 // Random object to generate new mole position
15 private Random randomGenerator = new Random();
16
17 // draw the mole in a new cell
18 public void drawMole( Graphics g )
19 {
20 // if mole data is not -1
21 if ( !( moleRow == -1 || moleColumn == -1 ) )
22 {
23 // calculate cell dimensions
24 int x = moleColumn * 50;
25 int y = moleRow * 50;
26
27 // set the mole's hea

Computer Science & Information Technology

You might also like to view...

Which of the following is not true about action queries?

A) They can be run more than once, but with full awareness of the action(s) they perform. B) They have special symbol icons to distinguish them from other queries. C) They should never be run more than once. D) You should backup your data before running them.

Computer Science & Information Technology

A field, or a combination of fields, that has a unique value is a:

A) field value/ B) primary key/ C) table/ D) foreign key/

Computer Science & Information Technology