-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.java
1308 lines (1112 loc) · 35.5 KB
/
Board.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Images: 22p x 22p
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import sun.audio.*;
@SuppressWarnings("serial")
public class Board extends JPanel implements KeyListener, ActionListener{
//Timers
private Timer gameTimer = new Timer(225, this); //moving
private Timer animateTimer = new Timer(50, this); //chomp
//Images - Constants (all capitals)
private final ImageIcon WALL = new ImageIcon("./images/StdWall.bmp");
private final ImageIcon FOOD = new ImageIcon("./images/StdFood.bmp");
private final ImageIcon BLANK = new ImageIcon("./images/Black.bmp");
private final ImageIcon DOOR = new ImageIcon("./images/Black.bmp");
private final ImageIcon SKULL = new ImageIcon("./images/Skull.bmp");
private final ImageIcon POINTS = new ImageIcon("./images/Cherry.png");
private final ImageIcon LIVES = new ImageIcon("./images/Banana.png");
private final ImageIcon POWERPELLETS = new ImageIcon("./images/Apple.png");
private final ImageIcon WEAKWARNING = new ImageIcon("./images/WeakGhostWarning.gif");
//2D Arrays - [Rows] [Columns] - "Virtual Graph Paper"
private JLabel[][] cell = new JLabel[25][27]; //images
private char[][] maze = new char[25][27]; //char's from the text file
//Characters: PacMan and Ghosts
private PacMan pacMan;
private Ghost[] ghost = new Ghost[4];
//Variables
public static int food = 0; //amount of food
public static int eaten = 0; //amount of food
public static int cherryCounter = 0; //amount of cherries eaten
public static int bananaCounter = 0; //amount of bananas eaten
public static int appleCounter = 0; //amount of apples eaten
public static int score = 0; //score
public static int highscore = 0; //highscore
public static int lives = 3; //lives
private int pStep; //steps for animating Pacman's chomp
private int gateRow = 0; //row coordinate of the gate
private int gateColumn = 0; //column coordinate of the gate
private int pRow = 0; //pacman's original row
private int pColumn = 0; //pacman's original column
private long startTime; //holds the start time (apple)
private long endTime; //holds the end time (apple)
private long elapsedTime = 10000; //holds the elapsed time (apple)
private int easterEggNum = 0; //number determines easter egg
//Reading the score
Scanner inputScore;
//Powerup Numbers
int cherryNum = 0;
int bananaNum = 0;
int totalBananaNum = 0;
int appleNum = 0;
int totalAppleNum = 0;
//Highscore and Name Array
static String[] scoreNameArray = new String[11];
static int[] highscoresArray = new int[10];
static String[] namesArray = new String[10];
//Scores
static JLabel scoreLabel = new JLabel("Score: " + score);
static JLabel highscoreLabel = new JLabel("Highscore: " + highscore);
static JLabel livesLabel = new JLabel("Lives: " + lives);
//Pause
private boolean isPaused = false;
//Files
File mazeFile = new File("maze.txt");
File highscoresFile = new File("highscores.txt");
/**
* Construct Maze
*/
public Board(){
//1. Set the layout (grid), background (black) and JLabel
setLayout(new GridLayout(25, 27));
setBackground(Color.BLACK);
//2. Create PacMan and the ghosts
pacMan = new PacMan();
ghost[0] = new Ghost(0);
ghost[1] = new Ghost(1);
ghost[2] = new Ghost(2);
ghost[3] = new Ghost(3);
//3. Read the scores
readScore();
}
/**
* Load the maze onto the screen from a text file
*/
public void loadBoard() {
//Variables: rows, scanner
int r = 0;
Scanner inputMaze;
//0. Play intro
if (PacManGUI.isMute == false){
playSound(loadIntroSound());
}
//1. Open the maze text file for input
try {
inputMaze = new Scanner(mazeFile);
//2. Cycle through all the rows in the maze file
while(inputMaze.hasNext()){
//2.1 Read the next line from the maze file
maze[r] = inputMaze.nextLine().toCharArray();
//2.2 For each row, cycle through all the columns
for (int c = 0; c < maze[r].length; c++){
//Create a new picture
cell[r][c] = new JLabel();
//Depending on the symbol in the maze file
//2.2.1 Wall
if (maze[r][c] == 'W'){
cell[r][c].setIcon(WALL);
}
//2.2.2 Food
else if (maze[r][c] == 'F'){
cell[r][c].setIcon(FOOD);
food++; //keep track of the amount of food
PacManGUI.pelletsStatusLabel.setText("0 / " + food);
}
//2.2.3 PacMan
else if (maze[r][c] == 'P'){
cell[r][c].setIcon(pacMan.getIcon());
pRow = r;
pColumn = c;
pacMan.setRow(r);
pacMan.setColumn(c);
pacMan.setDirection(0); //start left
}
//2.2.4 Ghost
else if (maze[r][c] == '0' || maze[r][c] == '1'
|| maze[r][c] == '2' || maze[r][c] == '3'){
int gNum = (Character.getNumericValue(maze[r][c]));
//int gNum = (int)(maze[r][c]) - 48; <-- using ASCII
//2.2.4.1 Easter Eggs
if (PacManGUI.playerName.equalsIgnoreCase("apple")
|| PacManGUI.playerName.equalsIgnoreCase("steve")
|| PacManGUI.playerName.equalsIgnoreCase("jobs")){
cell[r][c].setIcon(Ghost.IMAGE[5]);
ghost[gNum].setIcon(Ghost.IMAGE[5]);
easterEggNum = 5;
}
else if (PacManGUI.playerName.equalsIgnoreCase("bert")
|| PacManGUI.playerName.equalsIgnoreCase("muppet")){
cell[r][c].setIcon(Ghost.IMAGE[6]);
ghost[gNum].setIcon(Ghost.IMAGE[6]);
easterEggNum = 6;
}
else if (PacManGUI.playerName.equalsIgnoreCase("ernie")
|| PacManGUI.playerName.equalsIgnoreCase("muppets")){
cell[r][c].setIcon(Ghost.IMAGE[7]);
ghost[gNum].setIcon(Ghost.IMAGE[7]);
easterEggNum = 7;
}
else if (PacManGUI.playerName.equalsIgnoreCase("hello")
|| PacManGUI.playerName.equalsIgnoreCase("hi")
|| PacManGUI.playerName.equalsIgnoreCase("howdy")
|| PacManGUI.playerName.equalsIgnoreCase("hiya")
|| PacManGUI.playerName.equalsIgnoreCase("yo")
|| PacManGUI.playerName.equalsIgnoreCase("bonjour")
|| PacManGUI.playerName.equalsIgnoreCase("ni hao")){
cell[r][c].setIcon(Ghost.IMAGE[8]);
ghost[gNum].setIcon(Ghost.IMAGE[8]);
easterEggNum = 8;
}
else if (PacManGUI.playerName.equalsIgnoreCase("homer")
|| PacManGUI.playerName.equalsIgnoreCase("simpsons")
|| PacManGUI.playerName.equalsIgnoreCase("simpson")){
cell[r][c].setIcon(Ghost.IMAGE[9]);
ghost[gNum].setIcon(Ghost.IMAGE[9]);
easterEggNum = 9;
}
else if (PacManGUI.playerName.equalsIgnoreCase("luke")
|| PacManGUI.playerName.equalsIgnoreCase("star wars")
|| PacManGUI.playerName.equalsIgnoreCase("r2d2")
|| PacManGUI.playerName.equalsIgnoreCase("vadar")){
cell[r][c].setIcon(Ghost.IMAGE[10]);
ghost[gNum].setIcon(Ghost.IMAGE[10]);
easterEggNum = 10;
}
else if (PacManGUI.playerName.equalsIgnoreCase("rock")
|| PacManGUI.playerName.equalsIgnoreCase("cool")
|| PacManGUI.playerName.equalsIgnoreCase("awesome")){
cell[r][c].setIcon(Ghost.IMAGE[11]);
ghost[gNum].setIcon(Ghost.IMAGE[11]);
easterEggNum = 11;
}
//2.2.4.2 Normal
else{
cell[r][c].setIcon(ghost[gNum].getIcon());
easterEggNum = 0;
}
ghost[gNum].setRow(r);
ghost[gNum].setColumn(c);
}
//2.2.5 Door
else if (maze[r][c] == 'D'){
cell[r][c].setIcon(DOOR);
}
//2.2.6 Gate (Ghosts' home)
else if (maze[r][c] == 'G'){
cell[r][c].setIcon(BLANK);
gateRow = r;
gateColumn = c;
}
//2.2.7 Add the current cell to the board
add(cell[r][c]);
}
//2.3 Increment the row
r++;
}
//3. Close the maze text file
inputMaze.close();
}
//4. Catch the file if there is an error
catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
}
}
/**
* Play sounds
*/
public static void playSound(AudioStream audio){
AudioPlayer.player.start(audio);
}
/**
* Stop sounds
*/
public static void stopSound(AudioStream audio){
AudioPlayer.player.stop(audio);
}
/**
* Load the sounds -
*/
public static AudioStream loadIntroSound(){
try{
InputStream input = new FileInputStream("./sounds/pacmanintro.wav");
AudioStream audio = new AudioStream(input);
return audio;
}
catch (IOException e){
System.out.println("Error");
}
return null;
}
/**
* Load the sounds - eating
*/
public static AudioStream loadEatSound(){
try{
InputStream input = new FileInputStream("./sounds/fruiteat.wav");
AudioStream audio = new AudioStream(input);
return audio;
}
catch (IOException e){
System.out.println("Error");
}
return null;
}
/**
* Load the sounds - dying
*/
public static AudioStream loadDeathSound(){
try{
InputStream input = new FileInputStream("./sounds/killed.wav");
AudioStream audio = new AudioStream(input);
return audio;
}
catch (IOException e){
System.out.println("Error");
}
return null;
}
/**
* Load the sounds - chomping
*/
public static AudioStream loadChompSound(){
try{
InputStream input = new FileInputStream("./sounds/pacchomp.wav");
AudioStream audio = new AudioStream(input);
return audio;
}
catch (IOException e){
System.out.println("Error");
}
return null;
}
/**
* Load the sounds - eating a ghost
*/
public static AudioStream loadEatGhostSound(){
try{
InputStream input = new FileInputStream("./sounds/ghosteaten.wav");
AudioStream audio = new AudioStream(input);
return audio;
}
catch (IOException e){
System.out.println("Error");
}
return null;
}
/**
* Actions
*/
public void actionPerformed(ActionEvent e) {
//1. If action is game timer
if (e.getSource() == gameTimer){
//1.1 Move pacman and ghosts
performMove(pacMan);
moveGhosts();
cherries();
bananas();
apples();
//1.2 When the elapsed time has passed, reset to normal
if (ghost[0].isWeak() == true && ghost[1].isWeak() == true
&& ghost[2].isWeak() == true && ghost[3].isWeak() == true){
for (int i = 0; i < 4; i++){
if (endTime - System.currentTimeMillis() < 3000
&& endTime - System.currentTimeMillis() > 0){
ghost[i].setIcon(WEAKWARNING);
cell[ghost[i].getRow()][ghost[i].getColumn()].setIcon(WEAKWARNING);
}
else if (endTime < System.currentTimeMillis()){
if (easterEggNum != 0){
ghost[i].setIcon(Ghost.IMAGE[easterEggNum]);
cell[ghost[i].getRow()][ghost[i].getColumn()].setIcon(Ghost.IMAGE[easterEggNum]);
}
else if (easterEggNum == 0){
ghost[i].setIcon(Ghost.IMAGE[i]);
cell[ghost[i].getRow()][ghost[i].getColumn()].setIcon(Ghost.IMAGE[i]);
}
ghost[i].setWeak(false);
PacManGUI.ghostStatusLabel.setText("Normal");
}
}
}
//1.3 Check if all food is eaten
if (food == eaten){
stopGame();
}
}
//2. Otherwise, if the action is the animation timer
else if (e.getSource() == animateTimer){
//2.1 Animate PacMan through the current step
animatePacMan(pStep);
//2.2 Increment the step number
pStep++;
//2.3 If the step is the last step then reset the steps
if (pStep == 3){
pStep = 0;
}
}
}
/**
* Animates PacMan's chomp and standing still
* @param pStep2
*/
private void animatePacMan(int pStep) {
//1. If it is step 0 of animation
//1 is open mouth
if (pStep == 0){
//1.1 Open mouth in current cell
cell[pacMan.getRow()][pacMan.getColumn()]
.setIcon(PacMan.IMAGE[pacMan.getDirection()][1]);
//1.2 Delay the animation timer
animateTimer.setDelay(50);
}
//2. Otherwise if it is step 1 of animation
else if (pStep == 1){
//2.1 Blank the current cell
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(BLANK);
}
//3. Otherwise if it is step 2 of animation
else if (pStep == 2){
//3.0 Load the chomp sound
if (PacManGUI.isMute == false){
playSound(loadChompSound());
}
//3.1 Move pacMan
pacMan.move();
//3.2 If there is any food in the new square on the board
if (maze[pacMan.getRow()][pacMan.getColumn()] == 'F'){
//3.2.1 Increment the score with the lives and display it
score += 1 * lives;
scoreLabel.setText("Score: " + score);
//3.2.2 Mark the maze at the new position to 'E' for eaten
maze[pacMan.getRow()][pacMan.getColumn()] = 'E';
//3.3.2 Increment the amount of eaten food
eaten++;
PacManGUI.pelletsStatusLabel.setText(eaten + " / " + food);
}
//3.3 If there is a cherry in the new square on the board
if (maze[pacMan.getRow()][pacMan.getColumn()] == 'C'){
//3.3.0 Load eating the sound
if (PacManGUI.isMute == false){
playSound(loadEatSound());
}
//3.3.1 Increment the score with the lives and display it
score += 10 * lives;
scoreLabel.setText("Score: " + score);
//3.3.2 Mark the maze at the new position to 'E' for eaten
maze[pacMan.getRow()][pacMan.getColumn()] = 'E';
cherryNum--;
cherryCounter++;
PacManGUI.cherryCounterLabel.setText("x " + cherryCounter);
}
//3.4 If there is a banana in the new square on the board
if (maze[pacMan.getRow()][pacMan.getColumn()] == 'B'){
//3.4.0 Load eating the sound
if (PacManGUI.isMute == false){
playSound(loadEatSound());
}
//3.4.1 Increment the lives counter
lives++;
livesLabel.setText("Lives: " + lives);
//3.4.2 Mark the maze at the new position to 'E' for eaten
maze[pacMan.getRow()][pacMan.getColumn()] = 'E';
bananaNum--;
bananaCounter++;
PacManGUI.bananaCounterLabel.setText("x " + bananaCounter);
}
//3.5 If there is an apple in the new square on the board
if (maze[pacMan.getRow()][pacMan.getColumn()] == 'A'){
//3.5.0 Load eating the sound and set the ghost status
if (PacManGUI.isMute == false){
playSound(loadEatSound());
}
PacManGUI.ghostStatusLabel.setText("Weak");
//3.5.1 Get the time when the apple was eaten
startTime = System.currentTimeMillis();
//3.5.2 Start the apple powerup
appleAction();
//3.5.3 Mark the maze at the new position to 'E' for eaten
maze[pacMan.getRow()][pacMan.getColumn()] = 'E';
appleNum--;
appleCounter++;
PacManGUI.appleCounterLabel.setText("x " + appleCounter);
}
//3.6 If there is a weak ghost in the new square
for (Ghost g: ghost){
//3.6.1 If collision occurs but ghosts are weak
if (g.getRow() == pacMan.getRow() && g.getColumn() == pacMan.getColumn()
&& g.isWeak() == true){
//3.6.1.0 Load the sound
if (PacManGUI.isMute == false){
playSound(loadEatGhostSound());
}
//3.6.1.1 Update the score
score += 50 * lives;
scoreLabel.setText("Score: " + score);
//3.6.1.2 Teleport the ghost back to their home and replace image
if (maze[g.getRow()][g.getColumn()] == 'F'){
cell[g.getRow()][g.getColumn()].setIcon(FOOD);
}
else if (maze[g.getRow()][g.getColumn()] == 'C'){
cell[g.getRow()][g.getColumn()].setIcon(POINTS);
}
else if (maze[g.getRow()][g.getColumn()] == 'B'){
cell[g.getRow()][g.getColumn()].setIcon(LIVES);
}
else if (maze[g.getRow()][g.getColumn()] == 'A'){
cell[g.getRow()][g.getColumn()].setIcon(POWERPELLETS);
}
else{
cell[g.getRow()][g.getColumn()].setIcon(BLANK);
}
g.setRow(12);
g.setColumn(13);
g.setIcon(Ghost.IMAGE[4]);
cell[12][13].setIcon(Ghost.IMAGE[4]);
}
}
//3.7 Stop the animation timer
animateTimer.stop();
//3.8 If pacMan is dead then show a skull (runs into a ghost)
if (pacMan.isDead()){
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(SKULL);
}
//3.9 Otherwise, show the appropriate closed pacMan based on direction
//0 is closed mouth
else {
cell[pacMan.getRow()][pacMan.getColumn()]
.setIcon(PacMan.IMAGE[pacMan.getDirection()][0]);
}
}
}
/**
* Apple Powerup Actions (Power Pellet)
*/
private void appleAction(){
//0. Define end time
endTime = startTime + elapsedTime;
//1. Change the ghosts to weak
for (Ghost g: ghost){
cell[g.getRow()][g.getColumn()].setIcon(Ghost.IMAGE[4]);
g.setIcon(Ghost.IMAGE[4]);
g.setWeak(true);
}
}
/**
* What happens when a player presses a key
*/
public void keyPressed(KeyEvent key) {
//1. If the game isn't running and PacMan is alive, then start the timers
if (gameTimer.isRunning() == false && pacMan.isDead() == false){
gameTimer.start();
}
//2. If PacMan is still alive and the game is not over then
if (pacMan.isDead() == false && food != eaten){
//2.1 Track direction based on key pressed
// -37 since ASCII codes for cursor key starts at 37
int direction = key.getKeyCode() - 37;
//2.2 Change direction of PacMan;
//Left: 37 (0), Up: 38 (1), Right: 39 (2), Down: 40 (3)
if (direction == 0 && maze[pacMan.getRow()][pacMan.getColumn() - 1] != 'W'
&& maze[pacMan.getRow()][pacMan.getColumn() - 1] != 'G'){
pacMan.setDirection(0);
}
else if (direction == 1 && maze[pacMan.getRow() - 1][pacMan.getColumn()] != 'W'
&& maze[pacMan.getRow() - 1][pacMan.getColumn()] != 'G'){
pacMan.setDirection(1);
}
else if (direction == 2 && maze[pacMan.getRow()][pacMan.getColumn() + 1] != 'W'
&& maze[pacMan.getRow()][pacMan.getColumn() + 1] != 'G'){
pacMan.setDirection(2);
}
else if (direction == 3 && maze[pacMan.getRow() + 1][pacMan.getColumn()] != 'W'
&& maze[pacMan.getRow() + 1][pacMan.getColumn()] != 'G'){
pacMan.setDirection(3);
}
}
//3. If key pressed is the esc key, PAUSE
if (key.getKeyCode() == 27){
//3.1 If game is already running, then pause
if (isPaused == false){
gameTimer.stop();
animateTimer.stop();
isPaused = true;
PacManGUI.pauseLabel.setText("PAUSED. Press any key to continue.");
}
//3.2 If the game is already paused, then resume
else if (isPaused == true) {
gameTimer.start();
isPaused = false;
PacManGUI.pauseLabel.setText("PLAYING. Press 'Esc' to pause.");
}
}
else if (key.getKeyCode() != 27 && isPaused == true){
PacManGUI.pauseLabel.setText("PLAYING. Press 'Esc' to pause.");
isPaused = false;
}
}
/**
* Determines the moves of either PacMan or the Ghosts
* @param mover
*/
private void performMove(Mover mover){
//1. If a mover is at a door then teleport to other side
if (mover.getColumn() == 1){
mover.setColumn(24);
cell[12][1].setIcon(DOOR);
}
else if (mover.getColumn() == 25){
mover.setColumn(2);
cell[12][25].setIcon(DOOR);
}
//2. If there is no wall or gate where the Mover object wants to go then
if (maze[mover.getNextRow()][mover.getNextColumn()] != 'W'
&& maze[mover.getNextRow()][mover.getNextColumn()] != 'G'){
//2.1 If the Mover object is PacMan then animate a 'chomp'
if (mover == pacMan){
animateTimer.start();
}
//2.2 Otherwise the Mover is a ghost
else {
//2.2.1 If the cell where the Ghost is has food then reset it
if (maze[mover.getRow()][mover.getColumn()] == 'F'){
cell[mover.getRow()][mover.getColumn()].setIcon(FOOD);
}
else if (maze[mover.getRow()][mover.getColumn()] == 'C'){
cell[mover.getRow()][mover.getColumn()].setIcon(POINTS);
}
else if (maze[mover.getRow()][mover.getColumn()] == 'B'){
cell[mover.getRow()][mover.getColumn()].setIcon(LIVES);
}
else if (maze[mover.getRow()][mover.getColumn()] == 'A'){
cell[mover.getRow()][mover.getColumn()].setIcon(POWERPELLETS);
}
//2.2.2 If the ghost moves to 'I', move the ghosts to the gate 'G'
else if (maze[mover.getRow()][mover.getColumn()] == 'I'){
cell[mover.getRow()][mover.getColumn()].setIcon(BLANK);
mover.setRow(gateRow - 1);
mover.setColumn(gateColumn);
}
//2.2.3 Otherwise reset the cell to blank
else {
cell[mover.getRow()][mover.getColumn()].setIcon(BLANK);
}
//2.2.4 Move the ghost's position
mover.move();
//2.2.5 If a collision has occurred then death occurs
if (collided() && lives <= 0){
livesLabel.setText("Lives: 0");
death();
}
//2.2.6 Otherwise update the picture on the screen
else {
cell[mover.getRow()][mover.getColumn()].setIcon(mover.getIcon());
}
}
}
}
/**
* Moves the ghosts in a random pattern (AI)
*/
private void moveGhosts(){
//0.Direction Array
int[] dir = new int[4];
//1. If ghost is normal
if (ghost[0].isWeak() == false && ghost[1].isWeak() == false
&& ghost[2].isWeak() == false && ghost[3].isWeak() == false){
//1. Ghost 0 [Random]
dir[0] = 0;
//1.1 Keep selecting random directions to avoid 'backtracking'
//Left (0), Right (2), Up (1), Down (3)
dir[0] = (int)(Math.random() * 4);
//2. Ghost 1 [Chaser]
dir[1] = 0;
//2.1 Find PacMan's location and compare to ghost's location and chase
//Left (0), Right (2), Up (1), Down (3)
//Chase PacMan (left)
if (pacMan.getColumn() < ghost[1].getColumn()){
dir[1] = 0;
}
//Chase PacMan (right)
else if (pacMan.getColumn() > ghost[1].getColumn()){
dir[1] = 2;
}
//Chase PacMan (up)
else if (pacMan.getRow() < ghost[1].getRow()){
dir[1] = 1;
}
//Chase PacMan (down)
else if (pacMan.getRow() > ghost[1].getRow()){
dir[1] = 3;
}
//3. Ghost 2 [Seeker]
dir[2] = 0;
//3.1 If the seeker is on the same row or column, it will give chase
//Left (0), Right (2), Up (1), Down (3)
//Same Row
if (pacMan.getRow() == ghost[2].getRow()){
//Left
if (pacMan.getColumn() < ghost[2].getColumn()){
dir[2] = 0;
}
//Right
else if (pacMan.getColumn() > ghost[2].getColumn()){
dir[2] = 2;
}
}
//Same Column
else if (pacMan.getColumn() == ghost[2].getColumn()){
//Up
if (pacMan.getRow() < ghost[2].getRow()){
dir[2] = 1;
}
//Down
else if (pacMan.getRow() > ghost[2].getRow()){
dir[2] = 3;
}
}
//4. Ghost 3 [Follower]
dir[3] = 0;
//3.1 Mimics PacMan's moves
//Left (0), Right (2), Up (1), Down (3)
if (pacMan.getDirection() == 0){
dir[3] = 0;
}
else if (pacMan.getDirection() == 1){
dir[3] = 1;
}
else if (pacMan.getDirection() == 2){
dir[3] = 2;
}
else if (pacMan.getDirection() == 3){
dir[3] = 3;
}
}
//2. If ghost is weak
//Left (0), Right (2), Up (1), Down (3)
else if (ghost[0].isWeak() == true && ghost[1].isWeak() == true
&& ghost[2].isWeak() == true && ghost[3].isWeak() == true){
for (int i = 0; i < 4; i++){
//Walls - Up
if (ghost[i].getRow() - 1 == 'W'){
if (ghost[i].getColumn() - 1 != 'W'){
dir[i] = 0;
}
else{
dir[i] = 2;
}
}
//Walls - Down
else if (ghost[i].getRow() + 1 == 'W'){
if (ghost[i].getColumn() - 1 != 'W'){
dir[i] = 0;
}
else{
dir[i] = 2;
}
}
//Walls - Left
else if (ghost[i].getColumn() - 1 == 'W'){
if (ghost[i].getRow() + 1 != 'W'){
dir[i] = 3;
}
else {
dir[i] = 1;
}
}
//Walls - Up
else if (ghost[i].getColumn() + 1 == 'W'){
if (ghost[i].getRow() + 1 != 'W'){
dir[i] = 3;
}
else {
dir[i] = 1;
}
}
//Runaway from PacMan (left)
else if (pacMan.getColumn() > ghost[i].getColumn()){
dir[i] = 0;
}
//Runaway from PacMan (right)
else if (pacMan.getColumn() < ghost[i].getColumn()){
dir[i] = 2;
}
//Runaway from PacMan (up)
else if (pacMan.getRow() > ghost[i].getRow()){
dir[i] = 1;
}
//Runaway from PacMan (down)
else if (pacMan.getRow() < ghost[i].getRow()){
dir[i] = 3;
}
}
}
//3. Setting directions and moving all the ghosts
for (int i = 0; i < 4; i++){
ghost[i].setDirection(dir[i]);
performMove(ghost[i]);
}
}
/**
* Determines if PacMan has collided with a Ghost
* @return
*/
private boolean collided() {
//1. Cycle through all the ghosts to see if anyone has captured PacMan
//Enhanced for loop
for (Ghost g: ghost){
//1.1 If the ghost is in the same location then stop everything and return true
if (g.getRow() == pacMan.getRow() && g.getColumn() == pacMan.getColumn()
&& g.isWeak() == false){
gameTimer.stop();
animateTimer.stop();
lives--;
livesLabel.setText("Lives: " + lives);
if (PacManGUI.isMute == false){
playSound(loadDeathSound());
}
//1.1.1 Reset PacMan's location if still more lives
if (lives > 0){
//1.1.1.1 Replace PacMan's place
if (maze[pacMan.getRow()][pacMan.getColumn()] == 'F'){
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(FOOD);
}
else if (maze[pacMan.getRow()][pacMan.getColumn()] == 'C'){
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(POINTS);
}
else if (maze[pacMan.getRow()][pacMan.getColumn()] == 'B'){
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(LIVES);
}
else if (maze[pacMan.getRow()][pacMan.getColumn()] == 'A'){
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(POWERPELLETS);
}
else{
cell[pacMan.getRow()][pacMan.getColumn()].setIcon(BLANK);
}
//1.1.1.2 Move PacMan
pacMan.setRow(pRow);
pacMan.setColumn(pColumn);
cell[pRow][pColumn].setIcon(PacMan.IMAGE[0][0]);
//1.1.1.3 Resets the ghosts to the four corners of the board
for (Ghost ghost: ghost){
if (maze[ghost.getRow()][ghost.getColumn()] == 'F'){
cell[ghost.getRow()][ghost.getColumn()].setIcon(FOOD);
}
else if (maze[ghost.getRow()][ghost.getColumn()] == 'C'){
cell[ghost.getRow()][ghost.getColumn()].setIcon(POINTS);
}
else if (maze[ghost.getRow()][ghost.getColumn()] == 'B'){
cell[ghost.getRow()][ghost.getColumn()].setIcon(LIVES);
}
else if (maze[ghost.getRow()][ghost.getColumn()] == 'A'){
cell[ghost.getRow()][ghost.getColumn()].setIcon(POWERPELLETS);
}
else{
cell[ghost.getRow()][ghost.getColumn()].setIcon(BLANK);
}
}
//1.1.1.4 Move the ghosts
ghost[0].setRow(2);
ghost[0].setColumn(2);
if (easterEggNum != 0){
cell[2][2].setIcon(Ghost.IMAGE[easterEggNum]);
ghost[0].setIcon(Ghost.IMAGE[easterEggNum]);
}
else if (easterEggNum == 0){
cell[2][2].setIcon(Ghost.IMAGE[0]);
ghost[0].setIcon(Ghost.IMAGE[0]);
}
ghost[1].setRow(22);
ghost[1].setColumn(2);
if (easterEggNum != 0){
cell[22][2].setIcon(Ghost.IMAGE[easterEggNum]);
ghost[1].setIcon(Ghost.IMAGE[easterEggNum]);
}
else if (easterEggNum == 0){
cell[22][2].setIcon(Ghost.IMAGE[1]);
ghost[1].setIcon(Ghost.IMAGE[1]);
}
ghost[2].setRow(2);
ghost[2].setColumn(24);
if (easterEggNum != 0){
cell[2][24].setIcon(Ghost.IMAGE[easterEggNum]);
ghost[2].setIcon(Ghost.IMAGE[easterEggNum]);
}
else if (easterEggNum == 0){
cell[2][24].setIcon(Ghost.IMAGE[2]);
ghost[2].setIcon(Ghost.IMAGE[2]);
}
ghost[3].setRow(22);
ghost[3].setColumn(24);if (easterEggNum != 0){
cell[22][24].setIcon(Ghost.IMAGE[easterEggNum]);
ghost[3].setIcon(Ghost.IMAGE[easterEggNum]);
}