-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGame.java
More file actions
326 lines (292 loc) · 8.92 KB
/
Game.java
File metadata and controls
326 lines (292 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Timer;
public abstract class Game extends JFrame {
private boolean _isSetup = false;
private boolean _initialized = false;
private ArrayList _ObjectList = new ArrayList();
private Timer _t;
private boolean shootLeftL = false;
private boolean shootRightL = false;
private boolean shootLeftR = false;
private boolean shootRightR = false;
private boolean ballUp = false;
private boolean ballDown = false;
private boolean ballLeft = false;
private boolean ballRight = false;
private boolean defUp = false;
private boolean defDown = false;
private boolean defLeft = false;
private boolean defRight = false;
private boolean changeColor = false;
public boolean UpKeyPressed(){
return changeColor;
}
public boolean IKeyPressed() {
return defUp;
}
public boolean QKeyPressed(){
return shootLeftL;
}
public boolean EKeyPressed(){
return shootRightL;
}
public boolean UKeyPressed(){
return shootLeftR;
}
public boolean OKeyPressed(){
return shootRightR;
}
public void bulletHit(){
shootLeftL=false;
shootRightL=false;
shootLeftR=false;
shootRightR=false;
}
public boolean KKeyPressed() {
return defDown;
}
public boolean JKeyPressed() {
return defLeft;
}
public boolean LKeyPressed() {
return defRight;
}
public boolean WKeyPressed() {
return ballUp;
}
public boolean SKeyPressed() {
return ballDown;
}
public boolean AKeyPressed() {
return ballLeft;
}
public boolean DKeyPressed() {
return ballRight;
}
/**
* When implemented, this will allow the programmer to initialize the game
* before it begins running
*
* Adding objects to the game and setting their initial positions should be
* done here.
*
* @see GameObject
*/
public abstract void setup();
/**
* When the game begins, this method will automatically be executed every
* millisecond
*
* This may be used as a control method for checking user input and
* collision between any game objects
*/
public abstract void act();
/**
* Sets up the game and any objects.
*
* This method should never be called by anything other than a <code>main</code>
* method after the frame becomes visible.
*/
public void initComponents() {
getContentPane().setBackground(Color.black);
setup();
for (int i = 0; i < _ObjectList.size(); i++) {
GameObject o = (GameObject)_ObjectList.get(i);
o.repaint();
}
_t.start();
}
/**
* Adds a game object to the screen
*
* Any added objects will have their <code>act</code> method called every
* millisecond
*
* @param o the <code>GameObject</code> to add.
* @see GameObject#act()
*/
public void add(GameObject o) {
_ObjectList.add(o);
getContentPane().add(o);
}
/**
* Removes a game object from the screen
*
* @param o the <code>GameObject</code> to remove
* @see GameObject
*/
public void remove(GameObject o) {
_ObjectList.remove(o);
getContentPane().remove(o);
}
/**
* Sets the millisecond delay between calls to <code>act</code> methods.
*
* Increasing the delay will make the game run "slower." The default delay
* is 1 millisecond.
*
* @param delay the number of milliseconds between calls to <code>act</code>
* @see Game#act()
* @see GameObject#act()
*/
public void setDelay(int delay) {
_t.setDelay(delay);
}
public void setBackground(Color c) {
getContentPane().setBackground(c);
}
public Game() {
setSize(600, 600);
getContentPane().setBackground(Color.black);
getContentPane().setLayout(null);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuFileExit = new JMenuItem("Exit");
menuBar.add(menuFile);
menuFile.add(menuFileExit);
setJMenuBar(menuBar);
setTitle("Pong");
// Add window listener.
addWindowListener (
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
menuFileExit.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
_t = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
act();
for (int i = 0; i < _ObjectList.size(); i++) {
GameObject o = (GameObject)_ObjectList.get(i);
o.act();
}
}
});
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
char pressed = Character.toUpperCase(e.getKeyChar());
switch (pressed) {
case 'W' : ballUp = true; break;
case 'S' : ballDown = true; break;
case 'A' : ballLeft = true; break;
case 'D' : ballRight = true; break;
case 'I' : defUp = true; break;
case 'K' : defDown = true; break;
case 'J' : defLeft = true; break;
case 'L' : defRight = true; break;
case 'Q' : shootLeftL = true; break;
case 'E' : shootRightL = true; break;
case 'U' : shootLeftR = true; break;
case 'O' : shootRightR = true; break;
case KeyEvent.VK_UP : changeColor = true; break;
}
}
public void keyReleased(KeyEvent e) {
char released = Character.toUpperCase(e.getKeyChar());
switch (released) {
case 'W' : ballUp = false; break;
case 'S' : ballDown = false; break;
case 'A' : ballLeft = false; break;
case 'D' : ballRight = false; break;
case 'I' : defUp = false; break;
case 'K' : defDown = false; break;
case 'J' : defLeft = false; break;
case 'L' : defRight = false; break;
case KeyEvent.VK_UP : changeColor = false; break;
}
}
});
}
/**
* Starts updates to the game
*
* The game should automatically start.
*
* @see Game#stopGame()
*/
public void startGame() {
_t.start();
}
/**
* Stops updates to the game
*
* This can act like a "pause" method
*
* @see Game#startGame()
*/
public void stopGame() {
_t.stop();
}
/**
* Displays a dialog that says "Player 1 Wins!"
*
*/
public void p1Wins() {
_WinDialog d = new _WinDialog(this, "Darn, You got caught!");
d.setVisible(true);
}
/**
* Displays a dialog that says "Player 2 Wins!"
*
*/
public void p2Wins() {
_WinDialog d = new _WinDialog(this, "You died :(");
d.setSize(300,100);
d.setVisible(true);
}
/**
* Gets the pixel width of the visible playing field
*
* @return a width in pixels
*/
public int getFieldWidth() {
return getContentPane().getBounds().width;
}
/**
* Gets the pixel height of the visible playing field
*
* @return a height in pixels
*/
public int getFieldHeight() {
return getContentPane().getBounds().height;
}
class _WinDialog extends JDialog {
JButton ok = new JButton("OK");
_WinDialog(JFrame owner, String title) {
super(owner, title);
Rectangle r = owner.getBounds();
setSize(200, 100);
setLocation(r.x + r.width / 2 - 100, r.y + r.height / 2 - 50);
getContentPane().add(ok);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_WinDialog.this.setVisible(false);
}
});
}
}
}