-
Notifications
You must be signed in to change notification settings - Fork 1
/
Planewar.java
481 lines (407 loc) · 17.1 KB
/
Planewar.java
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.Clip;
public class Planewar extends JFrame {
// GameState store enum for gaming flow control
public enum GameState {
INITIAL,
GAMING,
PAUSE,
GAMEOVER,
VICTORY
}
// Gaming screen size
public static int width = 1000;
public static int height = 1000;
public static String windowSize = "1000x1000";
// Gaming stage control
public static Planewar.GameState currentState = GameState.INITIAL;
// Player score
public static int score = 0;
// gameLevel determine how many boss player defeat
public static int gameLevel = 0;
// Init background music is normal background music
public static String backGroundMusic = GameUtil.backGroundMusic;
// Control background music volume
public static float volume = 0.8f;
/* Because bossObj is non static object
* Use static bossAlive to record boss is alive or not
*/
public static boolean bossAlive = false;
// Setting before gaming
public static String Dfficulty = "Medium";
//Init window title
public static String title = "Airplane Battle";
public static int bossSpeed = 5;
public static int bulletSpeed = 5;
public static int enemySpeed = 5;
public static int shellSpeed = 5;
// The smaller the number is, the higher the bullet productivity is
public static int bulletProductivity = 15;
public static int enemyProductivity = 15;
public static int shellProductivity = 15;
//argument used to set window size
static Planewar mainFrame;
/*
* Time counter
* When time pass 25ms
* Player's plane shoot one bullet and add one enemy to the screen
*/
int count = 1;
int enemyCount = 0;
Image offScreenImage = null;
BgObj backGround = new BgObj(GameUtil.bgImag, 0, -435, 2);
// Init player's plane object and bossObj
public PlaneObj planeObj = new PlaneObj(GameUtil.planeImag, width/2+10, height/3+80, 20, 30, 0, this);
public BossObj bossObj = null;
// Button before gaming
JButton startButton;
JButton settingButton;
// Button when game over
static JButton retryButton;
static JButton homeButton;
// Clip store background music information
boolean bossMusicPlaying = true;
static Clip backgroundClip = null;
static Clip lose = null;
static Clip win = null;
static Clip open = null; //openning music
static Clip explode = null;
/* This function init Game screen parameter
* Provide setting option before starting the game
*/
public void launch() {
open = SoundUtil.playSoundWithVolume(GameUtil.rickMusic, true, volume);
this.setVisible(true);
this.setSize(width, height);
this.setLocationRelativeTo(null);
this.setTitle(title);
this.setDefaultCloseOperation(3);
this.setLayout(null);
startButton = new JButton(new ImageIcon(GameUtil.startButton));
startButton.setBounds(width/2-90, height/3+20, 200, 100);
startButton.setContentAreaFilled(false);
this.add(startButton);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SoundUtil.stopSound(open); //stop music when the game start
currentState = GameState.GAMING;
startButton.setVisible(false);
settingButton.setVisible(false);
startButton = null;
settingButton = null;
repaint();
}
});
settingButton = new JButton(new ImageIcon(GameUtil.settingButton));
settingButton.setBounds(width/2-90, height/3+130, 200, 100);
settingButton.setUI(new BasicButtonUI());
this.add(settingButton);
settingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SoundUtil.stopSound(open); //stop music when set variables
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameSettings().setVisible(true);
}
});
}
});
// Add basic objects to game screen
GameUtil.gameObjList.add(backGround);
GameUtil.gameObjList.add(planeObj);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
/*
* use getKeyCode() to detect keyboard input
* For Windows, the spacebar keyCode is 32
* For MacOS, the spacebar keyCode is 49
* For Linux distro, the spacebar keyCode is 62
*/
if (e.getKeyCode() == 32 || e.getKeyCode() == 49 || e.getKeyCode() == 62) {
switch (currentState) {
case GAMING:
backgroundClip.stop();
currentState = GameState.PAUSE;
break;
case PAUSE:
backgroundClip.start();
currentState = GameState.GAMING;
break;
default:
}
}
}
});
// Set focus on KeyListener after using JButton
this.setFocusable(true);
this.requestFocusInWindow();
while (true) {
if (currentState == GameState.GAMING) {
createObj();
repaint();
if(!bossAlive) bossObj = null;
if(bossObj != null && !bossMusicPlaying) {
/*
* When boss appear
* Stop normal background music
* Play background music for specific boss
*/
SoundUtil.stopSound(backgroundClip);
backgroundClip = SoundUtil.playSoundWithVolume(backGroundMusic, true, volume);
bossMusicPlaying = true;
} else if(bossObj == null && bossMusicPlaying) {
/*
* When there is no boss
* Stop previouse boss background music
* Play normal background music
*/
SoundUtil.stopSound(backgroundClip);
backgroundClip = SoundUtil.playSoundWithVolume(GameUtil.backGroundMusic, true, volume);
bossMusicPlaying = false;
}
} else {
// If GameOver stop background music
SoundUtil.stopSound(backgroundClip);
}
// Time sleep for counting
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
// Create gaming screen border
if (offScreenImage == null) {
offScreenImage = createImage(width, height);
}
Graphics gImage = offScreenImage.getGraphics();
gImage.fillRect(0, 0, width, height);
if (currentState == GameState.INITIAL) {
gImage.drawImage(GameUtil.bgImag, 0, 0, width, height, this);
gImage.drawImage(GameUtil.bossRickImag, width/10, height/6, this);
gImage.drawImage(GameUtil.bossTrumpImag, (width*9)/13, height/9, this);
gImage.drawImage(GameUtil.explodeImag, width/2-70, height/6, this);
}
if (currentState == GameState.GAMING) {
for (int i = 0; i < GameUtil.gameObjList.size(); i++) {
GameUtil.gameObjList.get(i).paintSelf(gImage);
}
GameUtil.gameObjList.removeAll(GameUtil.removeList);
}
if (currentState == GameState.GAMEOVER || currentState == GameState.VICTORY) {
SoundUtil.stopSound(backgroundClip);
gImage.drawImage(GameUtil.explodeImag, planeObj.getX() - 60, planeObj.getY() - 90, null);
lose = SoundUtil.playSoundWithVolume(GameUtil.loseSound, false, volume);
GameUtil.drawWord(gImage, "GAME OVER", Color.RED, 50, width/2-150, height/3);
if (retryButton == null) {
retryButton = new JButton(new ImageIcon(GameUtil.retryButton));
retryButton.setBounds( width/2-90, height/3+20, 174, 68);
retryButton.setUI(new BasicButtonUI());
retryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
restartGameAfterGameOver();
}
});
this.add(retryButton);
}
retryButton.setVisible(true);
if (homeButton == null) {
homeButton = new JButton(new ImageIcon(GameUtil.homeButoon));
homeButton.setBounds( width/2-90, height/3+100, 174, 68);
homeButton.setUI(new BasicButtonUI());
homeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetGameToInitialState();
}
});
this.add(homeButton);
}
homeButton.setVisible(true);
}
if (currentState == GameState.VICTORY) {
win = SoundUtil.playSoundWithVolume(GameUtil.score100Sound, false, volume);
GameUtil.drawWord(gImage, "YOU WON", Color.GREEN, 50, width/2-125, height/3);
}
if (currentState == GameState.GAMING || currentState == GameState.VICTORY || currentState == GameState.GAMEOVER) {
GameUtil.drawWord(gImage, "SCORE : " + score, Color.GREEN, 40, 30, 100);
}
g.drawImage(offScreenImage, 0, 0, null);
count++;
}
void createObj() {
// By count, when pass 25 * somethingProductivity, add object to the gaming
if (count % shellProductivity == 0) {
GameUtil.shellObjList.add(new ShellObj(GameUtil.shellImag, planeObj.getX() + 3, planeObj.getY() - 16, 14, 29, shellSpeed, this));
GameUtil.gameObjList.add(GameUtil.shellObjList.get(GameUtil.shellObjList.size() - 1));
SoundUtil.playSoundWithVolume(GameUtil.planeShootSound, false, volume*0.95f);
}
if (count % enemyProductivity == 0) {
GameUtil.enemyObjList.add(new EnemyObj(GameUtil.enemyImag, (int) (Math.random() * (width/50)) * 50, 0, 49, 36, enemySpeed, this));
GameUtil.gameObjList.add(GameUtil.enemyObjList.get(GameUtil.enemyObjList.size() - 1));
enemyCount++;
}
if (count % bulletProductivity == 0 && bossObj != null) {
GameUtil.bulletObjList.add(new BulletObj(GameUtil.bulletImag, bossObj.getX() + 40, bossObj.getY() + 85, 15, 25, bulletSpeed, this));
GameUtil.gameObjList.add(GameUtil.bulletObjList.get(GameUtil.bulletObjList.size() - 1));
}
// Control Game Level and Boss Type
if(gameLevel == 0 && score > 10 && bossObj == null) {
gameLevel++;
bossAlive = true;
bossObj = new BossObj(GameUtil.bossRickImag, width/2, 20, 176, 155, bossSpeed, gameLevel, this);
backGroundMusic = GameUtil.rickMusic;
GameUtil.gameObjList.add(bossObj);
}
if(gameLevel == 1 && score > 30 && bossObj == null) {
gameLevel++;
bossAlive = true;
bossObj = new BossObj(GameUtil.bossTrumpImag, width/2, 20, 206, 217, bossSpeed, gameLevel, this);
backGroundMusic = GameUtil.shootingStarSound;
GameUtil.gameObjList.add(bossObj);
}
}
private void restartGameAfterGameOver() {
SoundUtil.stopSound(lose);
SoundUtil.stopSound(explode);
GameUtil.gameObjList.clear();
GameUtil.bulletObjList.clear();
GameUtil.enemyObjList.clear();
GameUtil.shellObjList.clear();
GameUtil.removeList.clear();
backGround = new BgObj(GameUtil.bgImag, 0, -435, 2);
planeObj = new PlaneObj(GameUtil.planeImag, width/2+10, height/3+80, 20, 30, 0, this);
bossObj = null;
bossAlive = false;
startButton = null;
settingButton = null;
GameUtil.gameObjList.add(backGround);
GameUtil.gameObjList.add(planeObj);
currentState = GameState.GAMING;
score = 0;
count = 1;
enemyCount = 0;
gameLevel=0;
retryButton.setVisible(false);
retryButton = null;
homeButton.setVisible(false);
homeButton = null;
backgroundClip = SoundUtil.playSoundWithVolume(GameUtil.backGroundMusic, true, volume);
}
private void resetGameToInitialState() {
SoundUtil.stopSound(win);
SoundUtil.stopSound(lose);
SoundUtil.stopSound(explode);
currentState = GameState.INITIAL;
score = 0;
gameLevel = 0;
bossAlive = false;
bossMusicPlaying = false;
GameUtil.gameObjList.clear();
GameUtil.bulletObjList.clear();
GameUtil.enemyObjList.clear();
GameUtil.shellObjList.clear();
GameUtil.removeList.clear();
backGround = new BgObj(GameUtil.bgImag, 0, -435, 2);
planeObj = new PlaneObj(GameUtil.planeImag, width/2+10, height/3+80, 20, 30, 0, this);
bossObj = null;
startButton = null;
settingButton = null;
GameUtil.gameObjList.add(backGround);
GameUtil.gameObjList.add(planeObj);
repaint();
if (retryButton != null) {
retryButton.setVisible(false);
retryButton = null;
}
if (homeButton != null) {
homeButton.setVisible(false);
homeButton = null;
}
initializeButtons();
open = SoundUtil.playSoundWithVolume(GameUtil.rickMusic, true, volume);
}
private void initializeButtons() {
startButton = new JButton(new ImageIcon(GameUtil.startButton));
startButton.setBounds(width/2-90, height/3+20, 200, 100);
startButton.setContentAreaFilled(false);
this.add(startButton);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SoundUtil.stopSound(open);
currentState = GameState.GAMING;
startButton.setVisible(false);
settingButton.setVisible(false);
startButton = null;
settingButton = null;
backgroundClip = SoundUtil.playSoundWithVolume(GameUtil.backGroundMusic, true, volume);
repaint();
}
});
settingButton = new JButton(new ImageIcon(GameUtil.settingButton));
settingButton.setBounds(width/2-90, height/3+130, 200, 100);
settingButton.setUI(new BasicButtonUI());
this.add(settingButton);
settingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SoundUtil.stopSound(open);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameSettings().setVisible(true);
}
});
}
});
}
//setting window size change
public static void reinitializeComponents(Planewar mainFrame2) {
GameUtil.gameObjList.clear();
GameUtil.bulletObjList.clear();
GameUtil.enemyObjList.clear();
GameUtil.shellObjList.clear();
GameUtil.removeList.clear();
mainFrame.setSize(width, height);
mainFrame.setLocationRelativeTo(null);
BgObj backGround = new BgObj(GameUtil.bgImag, 0, -435, 2);
PlaneObj planeObj = new PlaneObj(GameUtil.planeImag, width/2+10, height/3+80, 20, 30, 0, mainFrame2);
GameUtil.gameObjList.add(backGround);
GameUtil.gameObjList.add(planeObj);
if (mainFrame2.startButton != null) {
mainFrame2.startButton.setBounds(width / 2 - 90, height / 3 + 20, 200, 100);
mainFrame2.add(mainFrame2.startButton);
}
if (mainFrame2.settingButton != null) {
mainFrame2.settingButton.setBounds(width / 2 - 90, height / 3 + 130, 200, 100);
mainFrame2.add(mainFrame2.settingButton);
}
mainFrame.repaint();
}
public static void main(String args[]) {
Planewar gameWin = new Planewar();
mainFrame = gameWin;
reinitializeComponents(gameWin);
gameWin.launch();
}
}