Repository navigation
Expand file tree
/
Copy pathGui.java
More file actions
601 lines (468 loc) · 15.2 KB
/
Copy pathGui.java
File metadata and controls
601 lines (468 loc) · 15.2 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import pt.ipleiria.estg.dei.stackemup.gridpanel.GridPanel;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
/**
*
* Graphical interface class
*
*/
public class Gui extends JFrame implements KeyListener {
private JPanel contentPane;
protected static GridPanel battlefieldGrid;
private BattleField bf;
private int xGun;
private Random ran;
protected int lvl=1;
protected static boolean gameOver = false;
protected static boolean gameStart = false;
protected JLabel lblScore;
protected JLabel lblGameOver;
protected JLabel lblGameStart;
protected JLabel lblLevelFinished;
protected JLabel lblEarthDestroyed;
protected JLabel lblStartLevel;
protected JLabel lblIcon;
protected String info;
protected static Thread novaThread;
protected static int levelNumber = 1;
protected static boolean levelFinished = false;
protected static boolean pause = false;
protected static boolean gameLoad = true;
protected ImageManage im;
protected ImageManageGun imGun;
private static String n;
protected static boolean tooMuchShots ;
protected static int nbShot;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui frame = new Gui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the Graphical interface
*
* @throws IllegalElementException
* @throws IllegalPositionException
*/
public Gui() throws IllegalElementException, IllegalPositionException {
this.addKeyListener(this);
Color col = new Color(000000);
setIconImage(Toolkit.getDefaultToolkit().getImage(Gui.class.getResource("/image/icon.png")));
// System.out.println("test");
setTitle("Space Invaders - Erasmus Project 2013");
bf = new BattleField("es-in.txt");
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
///////////UNCOMMIT THIS BEFORE EXPORT IN .JAR !!!////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
/*
System.out.println(Gui.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String m = Gui.class.getProtectionDomain().getCodeSource().getLocation().getPath();
n = "";
for (int i = 0; i< m.length()-17;i++) {
n += m.charAt(i);
}
String g = "%20";
n = n.replace(g, " ");
n += "es-in.txt";
System.out.println(n);
bf = new BattleField(n);
*/
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
///////////UNCOMMIT THIS BEFORE EXPORT IN .JAR !!!////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
ran = new Random(0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100,694,691);
setBounds(100, 100, 50 * bf.getColumns(), 50 + 50 * bf.getRows());
contentPane = new JPanel();
info = (" Earth life : " + bf.life + " Score : " + bf.score + " Level : " + lvl);
contentPane.setBackground(col);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new BorderLayout(0, 0));
Sound.setCurrentMusic(Sound.music);
setContentPane(contentPane);
battlefieldGrid = new GridPanel();
battlefieldGrid.setShowGridLines(false);
battlefieldGrid.setBackground(col);
battlefieldGrid.setRows(bf.getRows());
battlefieldGrid.setColumns(bf.getColumns());
contentPane.add(battlefieldGrid, BorderLayout.CENTER);
lblScore = new JLabel(info);
lblScore.setFont(new Font("Space Invaders", Font.PLAIN, 16));
lblScore.setHorizontalAlignment(SwingConstants.CENTER);
lblScore.setForeground(Color.WHITE);
contentPane.add(lblScore, BorderLayout.NORTH);
lblIcon = new JLabel();
lblIcon.setFont(new Font("Space Invaders", Font.PLAIN, 16));
lblIcon.setForeground(Color.BLACK);
lblIcon.setHorizontalAlignment(SwingConstants.CENTER);
lblIcon.setIcon(new ImageIcon(Gui.class.getResource("image/startScreen1.png")));
contentPane.add(lblIcon, BorderLayout.CENTER);
lblPause = new JLabel("PAUSE");
lblPause.setFont(new Font("Space Invaders", Font.PLAIN, 18));
lblPause.setHorizontalAlignment(SwingConstants.CENTER);
lblPause.setForeground(Color.BLACK);
contentPane.add(lblPause, BorderLayout.SOUTH);
xGun = 0;
for (int i = 0; i < bf.columns; i++) {
if (bf.battlefield[bf.rows - 2][i].toString().equals("G")) {
xGun = i;
break;
}
}
final Runnable iterator = new Runnable() {
public void run() {
try {
bf.move();
im = new ImageManage(bf,battlefieldGrid);
imGun = new ImageManageGun(bf,battlefieldGrid);
battlefieldGrid.repaint();
} catch (IllegalElementException e) {
e.printStackTrace();
} catch (IllegalPositionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
novaThread = new Thread() {
public void run() {
lblScore.setVisible(false);
lblStartLevel = new JLabel();
lblStartLevel.setHorizontalAlignment(SwingConstants.CENTER);
lblStartLevel.setIcon(new ImageIcon(Gui.class.getResource("/image/start3.gif")));
contentPane.add(lblStartLevel, BorderLayout.CENTER);
repaint();
pause = true;
try {
sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
gameLoad = false;
lblStartLevel.setIcon(new ImageIcon(Gui.class
.getResource("/image/startBar.gif")));
repaint();
while (gameStart == false) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pause = false;
lblStartLevel.setVisible(false);
battlefieldGrid.setVisible(true);
//battlefieldGrid.setGridBackground(Color.BLACK);
battlefieldGrid.setGridBackground("/back.jpg");
lblScore.setVisible(true);
contentPane.add(battlefieldGrid, BorderLayout.CENTER);
int sc = 0;
int li = 3;
while (gameOver == false) {
if (pause) {
lblPause.setForeground(Color.WHITE);
repaint();
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
info = (" Earth life : " + BattleField.life + " Score : " + BattleField.score + " Level : " + levelNumber);
int newli = BattleField.life;
if (newli != li) {
if (BattleField.life <= 0) {
//gameOver = true;
// ////////////////
lblEarthDestroyed = new JLabel();
lblEarthDestroyed.setFont(new Font(
"Space Invaders", Font.PLAIN, 16));
lblEarthDestroyed.setForeground(Color.BLACK);
lblEarthDestroyed
.setHorizontalAlignment(SwingConstants.CENTER);
lblEarthDestroyed.setIcon(new ImageIcon(Gui.class.getResource("image/earthDestroyed.png")));
contentPane.add(lblEarthDestroyed,
BorderLayout.CENTER);
// //////////////
lblGameOver = new JLabel("Score : " + BattleField.score);
lblGameOver.setFont(new Font("Space Invaders",Font.PLAIN, 25));
lblGameOver.setHorizontalAlignment(SwingConstants.CENTER);
lblGameOver.setForeground(Color.WHITE);
contentPane.add(lblGameOver, BorderLayout.SOUTH);
battlefieldGrid.setVisible(false);
lblPause.setVisible(false);
lblScore.setVisible(false);
pause = true;
}
lblScore.setText(info);
li = newli;
}
if (levelFinished == true) {
if(levelNumber<5){
try {
sleep(1500);
levelNumber++;
info = (" Earth life : " + bf.life + " Score : " + bf.score + " Level : " + levelNumber);
bf.newLevel(levelNumber);
levelFinished=false;
} catch (IllegalElementException | IllegalPositionException | InterruptedException e) {
e.printStackTrace();
}
}
else{
lblLevelFinished = new JLabel("You saved the earth ! - Score : "+BattleField.score);
lblLevelFinished.setFont(new Font("Space Invaders", Font.PLAIN, 25));
lblLevelFinished.setHorizontalAlignment(SwingConstants.CENTER);
lblLevelFinished.setForeground(Color.WHITE);
contentPane.add(lblLevelFinished, BorderLayout.CENTER);
battlefieldGrid.setVisible(false);
lblScore.setVisible(false);
pause = true;
}
}
int newsc = BattleField.score;
if (newsc != sc) {
sc = newsc;
lblScore.setText(info);
}
for (int i = 0; i < bf.columns; i++) {
if (bf.battlefield[bf.rows - 2][i].toString().equals(
"G")) {
xGun = i;
break;
}
}
try {
SwingUtilities.invokeAndWait(iterator);
} catch (InvocationTargetException | InterruptedException e) {
System.out.println("Invocation/Interruption exception");
}
if (BattleField.dead) {
try {
BattleField.score-=250;
if(BattleField.score<0)
BattleField.score=0;
} catch (Exception e) {
e.printStackTrace();
}
}
int numRand = ran.nextInt(100) + 1;
if (numRand < 3) {
try {
bf.setBattleFieldElement(0, bf.columns - 1,new RedSpacecraft(0, bf.columns - 1));
} catch (Exception e) {
System.out.println("RedSPaceCraft problem in Gui.java");
}
}
try {
im = new ImageManage(bf, battlefieldGrid);
imGun = new ImageManageGun(bf, battlefieldGrid);
sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
novaThread.start();
}
static boolean left = false;
static boolean right = false;
static boolean shot = false;
private JLabel lblPause;
/**
* Key Detection
*/
public void keyPressed(KeyEvent e) {
if (gameLoad == true) {
} else {
while (gameStart == false) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_SPACE:
gameStart = true;
break;
default:
gameStart = true;
break;
}
}
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT :
if (left == false && xGun > 0 && gameOver == false && BattleField.dead == false && pause==false && !levelFinished) {
//TODO
System.out.println("reload");
left = true;
try {
if (bf.battlefield[bf.rows - 2][xGun - 1].toString().equals("S")) {
BattleField.score-=250;
if(BattleField.score<0){
BattleField.score=0;
}
bf.gunCounter--;
Sound.explosion.play();
BattleField.dead = true;
bf.setBattleFieldElement(bf.rows - 2, xGun, new Empty(bf.rows - 2, xGun));
bf.setBattleFieldElement(bf.rows - 2, xGun - 1,new GunExplosion(bf.rows - 2, xGun - 1));
bf.setBattleFieldElement(bf.rows - 2, bf.columns / 2,new Gun(bf.rows - 2, bf.columns / 2));
xGun = 0;
} else {
bf.battlefield[bf.rows - 2][xGun].move(bf.rows - 2, xGun - 1);
bf.battlefield[bf.rows - 2][xGun - 1] = bf.battlefield[bf.rows - 2][xGun];
bf.setBattleFieldElement(bf.rows - 2, xGun, new Empty(bf.rows - 2, xGun));
}
xGun--;
imGun = new ImageManageGun(bf,battlefieldGrid);
battlefieldGrid.repaint();
} catch (IllegalElementException | IllegalPositionException | ArrayIndexOutOfBoundsException e1) {
System.out.println("ArrayIndexOutOfBoundsException exception in Gui.java");
}
}
break;
case KeyEvent.VK_RIGHT :
if ( right == false && xGun < bf.columns - 1 && gameOver == false && BattleField.dead == false && pause==false && !levelFinished) {
//TODO
System.out.println("reload");
right = true;
try {
if (bf.battlefield[bf.rows - 2][xGun + 1].toString().equals("S")) {
BattleField.score-=250;
if(BattleField.score<0){
BattleField.score=0;
}
bf.gunCounter--;
// bf.life--;
Sound.explosion.play();
BattleField.dead = true;
bf.setBattleFieldElement(bf.rows - 2, xGun, new Empty(bf.rows - 2, xGun));
bf.setBattleFieldElement(bf.rows - 2, xGun + 1,new GunExplosion(bf.rows - 2, xGun + 1));
bf.setBattleFieldElement(bf.rows - 2, bf.columns / 2,new Gun(bf.rows - 2, bf.columns / 2));
xGun = 0;
} else {
bf.battlefield[bf.rows - 2][xGun].move(bf.rows - 2,xGun + 1);
bf.battlefield[bf.rows - 2][xGun + 1] = bf.battlefield[bf.rows - 2][xGun];
bf.setBattleFieldElement(bf.rows - 2, xGun, new Empty(bf.rows - 2, xGun));
}
xGun++;
imGun = new ImageManageGun(bf,battlefieldGrid);
battlefieldGrid.repaint();
} catch (IllegalElementException | IllegalPositionException | ArrayIndexOutOfBoundsException e1) {
System.out.println(xGun + " = " + e1);
}
}
break;
case KeyEvent.VK_SPACE :
if (tooMuchShots == false && shot == false && gameOver == false && BattleField.dead == false && pause==false) {
shot = true;
try {
bf.setBattleFieldElement(bf.rows - 3, xGun, new GunShot(bf.rows - 3, xGun));
im = new ImageManage(bf, battlefieldGrid);
battlefieldGrid.repaint();
} catch (IllegalElementException | IllegalPositionException e1) {
System.out.println("Probel shot inside the Gui.java");
}
}
break;
case KeyEvent.VK_L :
//TODO
System.out.println("You are a cheater ! But take one life, it could be useful");
BattleField.life++;
break;
case KeyEvent.VK_P :
if (pause == false) {
pause = true;
} else {
pause = false;
}
break;
case KeyEvent.VK_ENTER :
reloadGame();
break;
default:
break;
}
}
}
/**
* Key Released
*/
public void keyReleased(KeyEvent e) {
if (gameLoad == true) {
} else {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_SPACE:
shot = false;
break;
default:
break;
}
}
}
/**
* Key Typed (Not used in this case)
*/
public void keyTyped(KeyEvent e) {
if (gameLoad == true) {
}
}
public void reloadGame() {
levelFinished=false;
gameOver = false;
BattleField.dead = false;
BattleField.life = 20;
levelNumber=2;
info = (" Earth life : " + bf.life + " Score : " + bf.score + " Level : " + levelNumber);
try {
bf.newLevel(levelNumber);
} catch (IllegalElementException | IllegalPositionException e1) {
e1.printStackTrace();
}
lblPause.setForeground(Color.BLACK);
battlefieldGrid.setVisible(true);
lblPause.setVisible(false);
lblScore.setVisible(true);
System.out.println(gameOver+" "+shot + " "+tooMuchShots+" "+BattleField.dead);
repaint();
}
}