-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.java
1467 lines (1298 loc) · 42 KB
/
Game.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
//DEPS org.openjfx:javafx-controls:RELEASE:${os.detected.jfxname}
//SOURCES Ghost.java
//FILES styles.css
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.event.* ;
import javafx.scene.control.* ;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.* ;
import javafx.geometry.*;
import javafx.animation.* ;
import javafx.util.* ;
import java.util.* ;
import java.io.* ;
// javac --module-path "%PATH_TO_FX%" --add-modules javafx.controls Game.java
// java --module-path "%PATH_TO_FX%" --add-modules javafx.controls Game
public class Game extends Application
{
public static void main(String[] args)
{
Application.launch(args) ;
}
enum Dir
{
UP, DOWN, LEFT, RIGHT ;
}
private final int mapWidth = 600 ;
private final int mapHeight = 400 ;
private final int size = 8 ; // refers to the radius of the circle
private final int speed = 10 ; // both, pacman and the ghosts move with the same speed
private final int ghostSize = size*2 ; // the width & height of the ghosts are equal to pacman's diameter
private final int wallSize = 20 ;
private boolean bonusEaten ;
private double pacmanX = 280 ;
private double pacmanY = 30 ;
private double pacmanTurnedAt_x, pacmanTurnedAt_y ; // holds the coordinates of the point where pacman made a turn
private int numOfWalls ; // keeps track of the number of walls created
private int score, highScore ;
private Stage window ;
private Scene scene ;
private Pane pane ;
private Circle pacman ;
private Ghost redGhost, pinkGhost, orangeGhost, cyanGhost ;
private KeyFrame pacman_keyFrame ;
private Timeline timeline ;
private Label scoreLabel, highLabel ;
private ArrayList<Rectangle> wallList = new ArrayList<Rectangle>() ;
private ArrayList<Circle> pelletList = new ArrayList<Circle>() ;
private ArrayList<Circle> bonusList = new ArrayList<Circle>() ;
private Dir red_movingAt, pink_movingAt, orange_movingAt, cyan_movingAt ;
private Dir movingAt, newDir ;
@Override
public void start(Stage primaryStage)
{
window = primaryStage ;
window.setTitle("P A C M A N") ;
window.setResizable(false) ;
pane = new Pane() ;
pane.setStyle("-fx-background-color : black") ;
initialize() ;
scoreLabel = new Label() ;
scoreLabel.setPrefWidth(130) ;
highLabel = new Label("High Score : " + Integer.toString(highScore)) ;
highLabel.setPrefWidth(150) ;
HBox h_box = new HBox(250) ;
h_box.setPadding(new Insets(0,5,0,5)) ;
h_box.getChildren().addAll(scoreLabel, highLabel) ;
VBox vbox = new VBox(h_box, pane) ;
scene = new Scene(vbox, mapWidth, mapHeight+20) ;
scene.getStylesheets().add("styles.css") ;
scene.setOnKeyPressed(event ->
{
switch(event.getCode())
{
case UP : newDir = Dir.UP ;
break ;
case DOWN : newDir = Dir.DOWN ;
break ;
case LEFT : newDir = Dir.LEFT ;
break ;
case RIGHT : newDir = Dir.RIGHT ;
break ;
}
}) ;
play() ;
window.setScene(scene) ;
window.show() ;
}
// pacman and the ghosts are born in this method. The walls are created here, too
private void initialize()
{
drawWalls() ;
drawBonusFood() ;
drawPellets() ;
pacman = new Circle() ; // create pacman
pacman.setRadius(size) ;
pacman.setFill(Color.YELLOW) ;
pacman.setCenterX(pacmanX) ;
pacman.setCenterY(pacmanY) ;
pane.getChildren().add(pacman) ;
redGhost = new Ghost(pane, 120, 110, ghostSize, ghostSize) ; // create the red ghost
redGhost.setColor(Color.RED) ;
setTimerForTransparency(redGhost,5) ;
pinkGhost = new Ghost(pane, 120, 150, ghostSize, ghostSize) ; // create the pink ghost
pinkGhost.setColor(Color.PINK) ;
setTimerForTransparency(pinkGhost, 15) ;
orangeGhost = new Ghost(pane, 100, 120, ghostSize, ghostSize) ; // create the orange ghost
orangeGhost.setColor(Color.ORANGE) ;
setTimerForTransparency(orangeGhost, 30) ;
cyanGhost = new Ghost(pane, 80, 100, ghostSize, ghostSize) ; // create the cyan ghost
cyanGhost.setColor(Color.CYAN) ;
setTimerForTransparency(cyanGhost, 40) ;
highScore = Integer.parseInt(getHighScore()) ;
}
private void play()
{
pacman_keyFrame = new KeyFrame(Duration.millis(110), e->
{
blinkBonus() ;
if(bonusEaten)
ateGhost() ;
else // check whether any of the ghosts have caught pacman
if(redGhost.caughtPacman(pacman, speed) || pinkGhost.caughtPacman(pacman, speed) || orangeGhost.caughtPacman(pacman, speed) || cyanGhost.caughtPacman(pacman, speed))
endGame() ;
// only when pacman cannot continue in its current direction, its direction is updated
if(checkForWalls(newDir, pacmanX, pacmanY) == false)
{
if(movingAt != newDir) // when pacman makes a turn, record the coordinates of the position he turned at
{
pacmanTurnedAt_x = pacman.getCenterX() ;
pacmanTurnedAt_y = pacman.getCenterY() ;
}
movingAt = newDir ;
}
if(movingAt == Dir.UP)
{
if(checkForWalls(movingAt, pacmanX, pacmanY) == false)
pacman.setCenterY(pacmanY - speed) ;
}
else if(movingAt == Dir.DOWN)
{
if(checkForWalls(movingAt, pacmanX, pacmanY) == false)
pacman.setCenterY(pacmanY + speed) ;
}
else if(movingAt == Dir.LEFT)
{
if(checkForWalls(movingAt, pacmanX, pacmanY) == false)
pacman.setCenterX(pacmanX - speed) ;
}
else if(movingAt == Dir.RIGHT)
{
if(checkForWalls(movingAt, pacmanX, pacmanY) == false)
pacman.setCenterX(pacmanX + speed) ;
}
ateFood(pacmanX, pacmanY) ;
ateBonus(pacmanX, pacmanY) ;
// move the ghosts
moveRed() ;
movePink() ;
moveOrange() ;
moveCyan() ;
// check if all the pellets & bonus food have been eaten and then end the game
if(pelletList.isEmpty() == true && bonusList.isEmpty()) endGame() ;
scoreLabel.setText("Score : " + Integer.toString(score)) ;
// update pacman's coordinates
pacmanX = pacman.getCenterX() ;
pacmanY = pacman.getCenterY() ;
}) ;
timeline = new Timeline(pacman_keyFrame) ;
timeline.setCycleCount(Timeline.INDEFINITE) ;
timeline.play() ;
}
// method to move the red ghost
private void moveRed()
{
Dir dontGo = null ;
if(bonusEaten && redGhost.getColor() == Color.BLUE)
dontGo = pacmanAt(redGhost) ; // the ghost must not go to this direction
if(redGhost.isTransparent() == true) // move the ghost out of the cell
{
if(redGhost.getX() < 210)
red_movingAt = Dir.RIGHT ;
else
redGhost.setTransparent(false) ; // only when the ghost has passed the right border of the cell it loses its transparency
}
else if(checkForWalls(red_movingAt, redGhost.getX(), redGhost.getY()) == true && numOfTurns(red_movingAt, redGhost.getX(), redGhost.getY()) == 1)
{ // if the ghost runs to a dead end it goes in the direction opposite to its current direction
red_movingAt = oppositeDir(red_movingAt) ;
}
else
{
for(;;)
{
if(redGhost.getColor() != Color.BLUE)
{
if(tailPacman(redGhost, 2) != null)
{
red_movingAt = tailPacman(redGhost, 2) ; // check if pacman is in red's radar. If pacman is 2 walls away from red, red will follow him
break ;
}
}
// move in a random direction
Dir direction = getRandomDir() ;
if(checkForWalls(direction, redGhost.getX(), redGhost.getY()) == false)
{
if(dontGo != null && direction != dontGo)
{
red_movingAt = direction ;
break ;
}
else if(direction != oppositeDir(red_movingAt))
{
red_movingAt = direction ;
break ;
}
}
}
}
if(red_movingAt == Dir.UP)
{
if(checkForWalls(red_movingAt, redGhost.getX(), redGhost.getY()) == false || redGhost.isTransparent() == true)
redGhost.setY(redGhost.getY() - speed) ;
}
else if(red_movingAt == Dir.DOWN)
{
if(checkForWalls(red_movingAt, redGhost.getX(), redGhost.getY()) == false || redGhost.isTransparent() == true)
redGhost.setY(redGhost.getY() + speed) ;
}
else if(red_movingAt == Dir.LEFT)
{
if(checkForWalls(red_movingAt, redGhost.getX(), redGhost.getY()) == false || redGhost.isTransparent() == true)
redGhost.setX(redGhost.getX() - speed) ;
}
else if(red_movingAt == Dir.RIGHT)
{
if(checkForWalls(red_movingAt, redGhost.getX(), redGhost.getY()) == false || redGhost.isTransparent() == true)
redGhost.setX(redGhost.getX() + speed) ;
}
}
// method to move the pink ghost
private void movePink()
{
Dir dontGo = null ;
if(bonusEaten && pinkGhost.getColor() == Color.BLUE)
dontGo = pacmanAt(pinkGhost) ; // the ghost must not go to this direction
if(pinkGhost.isTransparent() == true) // move the ghost out of the cell
{
if(pinkGhost.getY() < 200)
pink_movingAt = Dir.DOWN ;
else
pinkGhost.setTransparent(false) ; // only when the ghost has passed the bottom border of the cell it loses its transparency
}
else if(checkForWalls(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == true && numOfTurns(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == 1)
{ // if the ghost runs to a dead end it goes in the direction opposite to its current direction
pink_movingAt = oppositeDir(pink_movingAt) ;
}
else
{
for(;;)
{
if(pinkGhost.getColor() != Color.BLUE)
{
if(tailPacman(pinkGhost, 4) != null)
{
pink_movingAt = tailPacman(pinkGhost, 4) ; // check if pacman is in pink's radar. If pacman is 4 walls away from pink, pink will follow him
break ;
}
}
// move in a random direction
Dir direction = getRandomDir() ;
if(checkForWalls(direction, pinkGhost.getX(), pinkGhost.getY()) == false)
{
if(dontGo != null && direction != dontGo)
{
pink_movingAt = direction ;
break ;
}
else if(direction != oppositeDir(pink_movingAt))
{
pink_movingAt = direction ;
break ;
}
}
}
}
if(pink_movingAt == Dir.UP)
{
if(checkForWalls(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == false || pinkGhost.isTransparent() == true)
pinkGhost.setY(pinkGhost.getY() - speed) ;
}
else if(pink_movingAt == Dir.DOWN)
{
if(checkForWalls(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == false || pinkGhost.isTransparent() == true)
pinkGhost.setY(pinkGhost.getY() + speed) ;
}
else if(pink_movingAt == Dir.LEFT)
{
if(checkForWalls(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == false || pinkGhost.isTransparent() == true)
pinkGhost.setX(pinkGhost.getX() - speed) ;
}
else if(pink_movingAt == Dir.RIGHT)
{
if(checkForWalls(pink_movingAt, pinkGhost.getX(), pinkGhost.getY()) == false || pinkGhost.isTransparent() == true)
pinkGhost.setX(pinkGhost.getX() + speed) ;
}
}
// method to move the orange ghost
private void moveOrange()
{
Dir dontGo = null ;
if(bonusEaten && orangeGhost.getColor() == Color.BLUE)
dontGo = pacmanAt(orangeGhost) ; // the ghost must not go to this direction
if(orangeGhost.isTransparent() == true) // move the ghost out of the cell
{ if(orangeGhost.getY() > 70)
orange_movingAt = Dir.UP ;
else
orangeGhost.setTransparent(false) ; // only when the ghost has passed the top border of the cell it loses its transparency
}
else if(checkForWalls(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == true && numOfTurns(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == 1)
{ // if the ghost runs to a dead end it goes in the direction opposite to its current direction
orange_movingAt = oppositeDir(orange_movingAt) ;
}
else
{
for(;;)
{
if(orangeGhost.getColor() != Color.BLUE)
{
if(tailPacman(orangeGhost, 7) != null)
{
orange_movingAt = tailPacman(orangeGhost, 7) ; // check if pacman is in orange's radar. If pacman is 7 walls away from orange, orange will follow him
break ;
}
}
// move in a random direction
Dir direction = getRandomDir() ;
if(checkForWalls(direction, orangeGhost.getX(), orangeGhost.getY()) == false)
{
if(dontGo != null && direction != dontGo)
{
orange_movingAt = direction ;
break ;
}
else if(direction != oppositeDir(orange_movingAt))
{
orange_movingAt = direction ;
break ;
}
}
}
}
if(orange_movingAt == Dir.UP)
{
if(checkForWalls(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == false || orangeGhost.isTransparent() == true)
orangeGhost.setY(orangeGhost.getY() - speed) ;
}
else if(orange_movingAt == Dir.DOWN)
{
if(checkForWalls(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == false || orangeGhost.isTransparent() == true)
orangeGhost.setY(orangeGhost.getY() + speed) ;
}
else if(orange_movingAt == Dir.LEFT)
{
if(checkForWalls(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == false || orangeGhost.isTransparent() == true)
orangeGhost.setX(orangeGhost.getX() - speed) ;
}
else if(orange_movingAt == Dir.RIGHT)
{
if(checkForWalls(orange_movingAt, orangeGhost.getX(), orangeGhost.getY()) == false || orangeGhost.isTransparent() == true)
orangeGhost.setX(orangeGhost.getX() + speed) ;
}
}
private void moveCyan()
{
Dir dontGo = null ;
if(bonusEaten && cyanGhost.getColor() == Color.BLUE)
dontGo = pacmanAt(cyanGhost) ; // the ghost must not go to this direction
if(cyanGhost.isTransparent() == true) // move the ghost out of the cell
{
if(cyanGhost.getX() > 50)
cyan_movingAt = Dir.LEFT ;
else
cyanGhost.setTransparent(false) ; // only when the ghost has passed the left border of the cell it loses its transparency
}
else if(checkForWalls(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == true && numOfTurns(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == 1)
{ // if the ghost runs to a dead end it goes in the direction opposite to its current direction
cyan_movingAt = oppositeDir(cyan_movingAt) ;
}
else
{
for(;;)
{
if(cyanGhost.getColor() != Color.BLUE)
{
if(tailPacman(cyanGhost, 5) != null) // check if pacman is in cyan's radar. If pacman is 5 walls away from cyan, cyan will follow him
{
cyan_movingAt = tailPacman(cyanGhost, 5) ;
break ;
}
}
Dir direction = getRandomDir() ;
if(checkForWalls(direction, cyanGhost.getX(), cyanGhost.getY()) == false)
{
if(dontGo != null && direction != dontGo)
{
cyan_movingAt = direction ;
break ;
}
else if(direction != oppositeDir(cyan_movingAt))
{
cyan_movingAt = direction ;
break ;
}
}
}
}
if(cyan_movingAt == Dir.UP)
{
if(checkForWalls(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == false || cyanGhost.isTransparent() == true)
cyanGhost.setY(cyanGhost.getY() - speed) ;
}
else if(cyan_movingAt == Dir.DOWN)
{
if(checkForWalls(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == false || cyanGhost.isTransparent() == true)
cyanGhost.setY(cyanGhost.getY() + speed) ;
}
else if(cyan_movingAt == Dir.LEFT)
{
if(checkForWalls(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == false || cyanGhost.isTransparent() == true)
cyanGhost.setX(cyanGhost.getX() - speed) ;
}
else if(cyan_movingAt == Dir.RIGHT)
{
if(checkForWalls(cyan_movingAt, cyanGhost.getX(), cyanGhost.getY()) == false || cyanGhost.isTransparent() == true)
cyanGhost.setX(cyanGhost.getX() + speed) ;
}
}
// check if pacman is in the ghost's radar. If pacman is "wallCount" walls away from the ghost, this method will return the direction that leads to pacman
private Dir tailPacman(Ghost ghost, int wallCount)
{
Dir direction = null ;
double ghostX = ghost.getX() ;
double ghostY = ghost.getY() ;
Dir pacmanDir = pacmanAt(ghost) ; // from the ghost's current position find out in which direction pacman is
if(pacmanDir == Dir.DOWN && pacmanY-ghostY <= (wallSize*wallCount))
{
if(checkForWallsBetween(ghostX, ghostY, pacmanX, pacmanY, Dir.DOWN) == false) direction = Dir.DOWN ;
}
else if(pacmanDir == Dir.UP && ghostY-pacmanY <= (wallSize*wallCount))
{
if(checkForWallsBetween(ghostX, ghostY, pacmanX, pacmanY, Dir.UP) == false) direction = Dir.UP ;
}
else if(pacmanDir == Dir.LEFT && ghostX-pacmanX <= (wallSize*wallCount))
{
if(checkForWallsBetween(ghostX, ghostY, pacmanX, pacmanY, Dir.LEFT) == false) direction = Dir.LEFT ;
}
else if(pacmanDir == Dir.RIGHT && pacmanX-ghostX <= (wallSize*wallCount))
{
if(checkForWallsBetween(ghostX, ghostY, pacmanX, pacmanY, Dir.RIGHT) == false) direction = Dir.RIGHT ;
}
return direction ;
}
// method that'll return the direction that points towards pacman from a ghost's current position
private Dir pacmanAt(Ghost ghost)
{
double x = ghost.getX() ;
double y = ghost.getY() ;
if(y == pacmanY && (pacmanX-x) > 0 && (pacmanX-x) < 100 && checkForWallsBetween(x, y, pacmanX, pacmanY, Dir.RIGHT) == false)
return Dir.RIGHT ;
else if(y == pacmanY && (x-pacmanX) > 0 && (x-pacmanX) < 100 && checkForWallsBetween(x, y, pacmanX, pacmanY, Dir.LEFT) == false)
return Dir.LEFT ;
else if(x == pacmanX && (pacmanY-y) > 0 && (pacmanY-y) < 100 && checkForWallsBetween(x, y, pacmanX, pacmanY, Dir.DOWN) == false)
return Dir.DOWN ;
else if(x == pacmanX && (y-pacmanY) > 0 && (y-pacmanY) < 100 && checkForWallsBetween(x, y, pacmanX, pacmanY, Dir.UP) == false)
return Dir.UP ;
return null ;
}
private void setTimerForTransparency(Ghost ghost, long delay)
{
TimerTask task = new TimerTask()
{
@Override
public void run()
{
ghost.setTransparent(true) ;
}
} ;
Timer timer = new Timer() ;
timer.schedule(task, (1000*delay)) ;
}
// this method sets timer for how long the ghosts stay blue. Once the time is reached, the ghosts return to their original colours
private void setTimerForGhosts()
{
TimerTask tsk = new TimerTask()
{
@Override
public void run()
{
redGhost.setColor(Color.RED) ;
pinkGhost.setColor(Color.PINK) ;
orangeGhost.setColor(Color.ORANGE) ;
cyanGhost.setColor(Color.CYAN) ;
bonusEaten = false ;
}
} ;
Timer tmr = new Timer() ;
tmr.schedule(tsk, (1000*10)) ; // schedule it for 10 seconds from the time it's called
}
// method that will return the number(1 to 3) of possible turns a ghost can make excluding the direction its already moving
private Integer numOfTurns(Dir currentDir, double x, double y)
{
int numOfTurns = 0 ;
if(currentDir != Dir.UP && checkForWalls(Dir.UP, x, y) == false) numOfTurns++ ;
if(currentDir != Dir.DOWN && checkForWalls(Dir.DOWN, x, y) == false) numOfTurns++ ;
if(currentDir != Dir.LEFT && checkForWalls(Dir.LEFT, x, y) == false) numOfTurns++ ;
if(currentDir != Dir.RIGHT && checkForWalls(Dir.RIGHT, x, y) == false) numOfTurns++ ;
return numOfTurns ;
}
// method to check for an incoming wall specific to the direction
private Boolean checkForWalls(Dir theDir, double x_coord, double y_coord)
{
x_coord = x_coord - speed ;
y_coord = y_coord - speed ;
for(int counter = 0 ; counter < wallList.size() ; counter++)
{
double checkX = wallList.get(counter).getX() ;
double checkY = wallList.get(counter).getY() ;
if(theDir == Dir.UP && checkY < y_coord)
{
if((x_coord == checkX || (x_coord < checkX+wallSize && x_coord > checkX) || x_coord+speed == checkX) && y_coord-wallSize <= checkY)
return true ;
}
else if(theDir == Dir.DOWN && checkY > y_coord)
{
if((x_coord == checkX || (x_coord < checkX+wallSize && x_coord > checkX) || x_coord+speed == checkX) && y_coord+wallSize >= checkY)
return true ;
}
else if(theDir == Dir.LEFT && checkX < x_coord)
{
if(x_coord-wallSize <= checkX && (y_coord == checkY || (y_coord < checkY+wallSize && y_coord > checkY) || y_coord+speed == checkY))
return true ;
}
else if(theDir == Dir.RIGHT && checkX > x_coord)
{
if(x_coord+wallSize >= checkX && (y_coord == checkY || (y_coord < checkY+wallSize && y_coord > checkY) || y_coord+speed == checkY))
return true ;
}
}
return false ;
}
// method that'll check for walls between 2 positions in a specific direction
private Boolean checkForWallsBetween(double from_x, double from_y, double to_x, double to_y, Dir direction)
{
boolean wall_present = false ;
if(direction == Dir.UP)
{
while(from_y > to_y && wall_present == false)
{
wall_present = checkForWalls(direction, from_x, from_y) ;
from_y-=wallSize ;
}
}
else if(direction == Dir.DOWN)
{
while(from_y < to_y && wall_present == false)
{
wall_present = checkForWalls(direction, from_x, from_y) ;
from_y+=wallSize ;
}
}
else if(direction == Dir.LEFT)
{
while(from_x > to_x && wall_present == false)
{
wall_present = checkForWalls(direction, from_x, from_y) ;
from_x-=wallSize ;
}
}
else if(direction == Dir.RIGHT)
{
while(from_x < to_x && wall_present == false)
{
wall_present = checkForWalls(direction, from_x, from_y) ;
from_x+=wallSize ;
}
}
return wall_present ;
}
// returns the opposite direction of the specified dirrection
private Dir oppositeDir(Dir d)
{
Dir dir = null ;
if(d == Dir.UP)
dir = Dir.DOWN ;
else if(d == Dir.DOWN)
dir = Dir.UP ;
else if(d == Dir.LEFT)
dir = Dir.RIGHT ;
else if(d == Dir.RIGHT)
dir = Dir.LEFT ;
return dir ;
}
// method that'll return a random direction
private Dir getRandomDir()
{
Dir dirc = null ;
int num = (int)(Math.random() * 4) ;
switch(num)
{
case 0 : dirc = Dir.UP ;
break ;
case 1 : dirc = Dir.DOWN ;
break ;
case 2 : dirc = Dir.LEFT ;
break ;
case 3 : dirc = Dir.RIGHT ;
break ;
}
return dirc ;
}
// method to check if pacman ate a pellet
private Boolean ateFood(double x, double y)
{
for(int n = 0 ; n < pelletList.size() ; n++)
{
if(pelletList.get(n).getCenterX() == x && pelletList.get(n).getCenterY() == y)
{
pane.getChildren().remove(pelletList.get(n)) ;
pelletList.remove(n) ;
score+=10 ; // increment the player's score by 10
return true ;
}
}
return false ;
}
// method to check if pacman ate a bonus food
private Boolean ateBonus(double x, double y)
{
for(int n = 0 ; n < bonusList.size() ; n++)
{
if(bonusList.get(n).getCenterX() == x && bonusList.get(n).getCenterY() == y)
{
pane.getChildren().remove(bonusList.get(n)) ;
bonusList.remove(n) ;
score+=20 ; // increment the player's score by 20
bonusEaten = true ;
// set the colors of the ghosts to BLUE
redGhost.setColor(Color.BLUE) ;
pinkGhost.setColor(Color.BLUE) ;
orangeGhost.setColor(Color.BLUE) ;
cyanGhost.setColor(Color.BLUE) ;
sleep(0.1) ;
setTimerForGhosts() ;
return true ;
}
}
return false ;
}
// method that'll check if pacman ate a ghost
private void ateGhost()
{
if(pacmanX == redGhost.getX() && (pacmanY+speed == redGhost.getY() || pacmanY-speed == redGhost.getY()))
{
redGhost.setX(120) ;
redGhost.setY(170) ;
redGhost.setColor(Color.RED) ;
setTimerForTransparency(redGhost, 5) ;
score+=50 ;
}
else if((pacmanX-speed == redGhost.getX() || pacmanX+speed == redGhost.getX()) && pacmanY == redGhost.getY())
{
redGhost.setX(120) ;
redGhost.setY(170) ;
redGhost.setColor(Color.RED) ;
setTimerForTransparency(redGhost, 5) ;
score+=50 ;
}
else if(pacmanX == redGhost.getX() && pacmanY == redGhost.getY())
{
redGhost.setX(120) ;
redGhost.setY(170) ;
redGhost.setColor(Color.RED) ;
setTimerForTransparency(redGhost, 5) ;
score+=50 ;
}
if(pacmanX == pinkGhost.getX() && (pacmanY+speed == pinkGhost.getY() || pacmanY-speed == pinkGhost.getY()))
{
pinkGhost.setX(120) ;
pinkGhost.setY(150) ;
pinkGhost.setColor(Color.PINK) ;
setTimerForTransparency(pinkGhost, 10) ;
score+=50 ;
}
else if((pacmanX-speed == pinkGhost.getX() || pacmanX+speed == pinkGhost.getX()) && pacmanY == pinkGhost.getY())
{
pinkGhost.setX(120) ;
pinkGhost.setY(150) ;
pinkGhost.setColor(Color.PINK) ;
setTimerForTransparency(pinkGhost, 10) ;
score+=50 ;
}
else if(pacmanX == pinkGhost.getX() && pacmanY == pinkGhost.getY())
{
pinkGhost.setX(120) ;
pinkGhost.setY(150) ;
pinkGhost.setColor(Color.PINK) ;
setTimerForTransparency(pinkGhost, 10) ;
score+=50 ;
}
if(pacmanX == orangeGhost.getX() && (pacmanY+speed == orangeGhost.getY() || pacmanY-speed == orangeGhost.getY()))
{
orangeGhost.setX(120) ;
orangeGhost.setY(110) ;
orangeGhost.setColor(Color.ORANGE) ;
setTimerForTransparency(orangeGhost, 20) ;
score+=50 ;
}
else if((pacmanX-speed == orangeGhost.getX() || pacmanX+speed == orangeGhost.getX()) && pacmanY == orangeGhost.getY())
{
orangeGhost.setX(120) ;
orangeGhost.setY(110) ;
orangeGhost.setColor(Color.ORANGE) ;
setTimerForTransparency(orangeGhost, 20) ;
score+=50 ;
}
else if(pacmanX == orangeGhost.getX() && pacmanY == orangeGhost.getY())
{
orangeGhost.setX(120) ;
orangeGhost.setY(110) ;
orangeGhost.setColor(Color.ORANGE) ;
setTimerForTransparency(orangeGhost, 20) ;
score+=50 ;
}
if(pacmanX == cyanGhost.getX() && (pacmanY+speed == cyanGhost.getY() || pacmanY-speed == cyanGhost.getY()))
{
cyanGhost.setX(80) ;
cyanGhost.setY(130) ;
cyanGhost.setColor(Color.CYAN) ;
setTimerForTransparency(cyanGhost, 25) ;
score+=50 ;
}
else if((pacmanX-speed == cyanGhost.getX() || pacmanX+speed == cyanGhost.getX()) && pacmanY == cyanGhost.getY())
{
cyanGhost.setX(80) ;
cyanGhost.setY(130) ;
cyanGhost.setColor(Color.CYAN) ;
setTimerForTransparency(cyanGhost, 25) ;
score+=50 ;
}
else if(pacmanX == cyanGhost.getX() && pacmanY == cyanGhost.getY())
{
cyanGhost.setX(80) ;
cyanGhost.setY(130) ;
cyanGhost.setColor(Color.CYAN) ;
setTimerForTransparency(cyanGhost, 25) ;
score+=50 ;
}
}
private void blinkBonus()
{
for(int index = 0 ; index < bonusList.size() ; index++)
{
Circle food = bonusList.get(index) ;
if(food.getFill() == Color.WHITE)
food.setFill(Color.BLACK) ;
else
food.setFill(Color.WHITE) ;
}
}
private void endGame()
{
sleep(3) ;
if(score > highScore) setHighScore(Integer.toString(score)) ;
Runtime.getRuntime().exit(0) ; // terminate the program
}
private void sleep(double num) // a method to put the system to sleep
{
try
{
Thread.sleep((long)(1000*num)) ; // putting the system to sleep for 'num' seconds
}
catch(InterruptedException e)
{
e.printStackTrace() ;
}
}
private String getHighScore()
{
String s = "0" ;
String filePath = new File("").getAbsolutePath() ;
try
{
File file = new File(filePath.concat("\\high_score.txt")) ;
if(file.exists())
{
Scanner scan = new Scanner(file) ;
s = scan.nextLine() ;
}
else
{
// create a the high_score.txt file and insert a 0
setHighScore("0") ;
}
}
catch(Exception e)
{
System.out.println("Failed to retrieve the high score") ;
}
return s ;
}
private Boolean setHighScore(String newScore)
{
boolean highScoreUpdated = false ;
try
{
FileWriter writer = new FileWriter("high_score.txt") ;
writer.write(newScore) ;
writer.close() ;
highScoreUpdated = true ;
}
catch(Exception e)
{
System.out.println("Failed to set the new high score") ;
}
return highScoreUpdated ;
}
// method to create pellets, store them in an ArrayList and put them on the pane
private void drawPellets()
{
int x, y, num = 0 ;
for(x = 30 ; x < mapWidth ; x+=wallSize)
{
for(y = 30 ; y < mapHeight ; y+=wallSize)
{
if(isAWall(x-(wallSize/2), y-(wallSize/2)) == false && !isABonusFood(x, y) && (x < 50 || x > 190 || y < 90 || y > 200))
{
Circle pellet = new Circle() ;
pellet.setRadius(1.5) ;
pellet.setCenterX(x) ;
pellet.setCenterY(y) ;
pellet.setFill(Color.WHITE) ;
pane.getChildren().add(pellet) ;
pelletList.add(pellet) ;
num++ ;
}
}
}
}
// method to put bonus food in the game. The cooridinates are held in an ArrayList
private void drawBonusFood()
{
int x, y ;
for(y = 30 ; y < mapHeight ; y+=(mapHeight-(wallSize*3)))
{
for(x = 30 ; x < mapWidth ; x+=(mapWidth-(wallSize*3)))
{
Circle c = new Circle() ;
c.setRadius(4) ;
c.setCenterX(x) ;
c.setCenterY(y) ;
c.setFill(Color.WHITE) ;
pane.getChildren().add(c) ;
bonusList.add(c) ;
}
}
y = 250 ;
for(x = 410 ; x < 510 ; x+=80)
{
Circle circ = new Circle() ;
circ.setRadius(4) ;
circ.setCenterX(x) ;
circ.setCenterY(y) ;
circ.setFill(Color.WHITE) ;
pane.getChildren().add(circ) ;
bonusList.add(circ) ;
}