-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBoard.java
1219 lines (1097 loc) · 32.6 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
/* Drew Schuster */
import java.awt.*;
import javax.imageio.*;
import javax.swing.JPanel;
import java.lang.Math;
import java.util.*;
import java.io.*;
/* Both Player and Ghost inherit Mover. Has generic functions relevant to both*/
class Mover
{
/* Framecount is used to count animation frames*/
int frameCount=0;
/* State contains the game map */
boolean[][] state;
/* gridSize is the size of one square in the game.
max is the height/width of the game.
increment is the speed at which the object moves,
1 increment per move() call */
int gridSize;
int max;
int increment;
/* Generic constructor */
public Mover()
{
gridSize=20;
increment = 4;
max = 400;
state = new boolean[20][20];
for(int i =0;i<20;i++)
{
for(int j=0;j<20;j++)
{
state[i][j] = false;
}
}
}
/* Updates the state information */
public void updateState(boolean[][] state)
{
for(int i =0;i<20;i++)
{
for(int j=0;j<20;j++)
{
this.state[i][j] = state[i][j];
}
}
}
/* Determines if a set of coordinates is a valid destination.*/
public boolean isValidDest(int x, int y)
{
/* The first statements check that the x and y are inbounds. The last statement checks the map to
see if it's a valid location */
if ((((x)%20==0) || ((y)%20)==0) && 20<=x && x<400 && 20<= y && y<400 && state[x/20-1][y/20-1] )
{
return true;
}
return false;
}
}
/* This is the pacman object */
class Player extends Mover
{
/* Direction is used in demoMode, currDirection and desiredDirection are used in non demoMode*/
char direction;
char currDirection;
char desiredDirection;
/* Keeps track of pellets eaten to determine end of game */
int pelletsEaten;
/* Last location */
int lastX;
int lastY;
/* Current location */
int x;
int y;
/* Which pellet the pacman is on top of */
int pelletX;
int pelletY;
/* teleport is true when travelling through the teleport tunnels*/
boolean teleport;
/* Stopped is set when the pacman is not moving or has been killed */
boolean stopped = false;
/* Constructor places pacman in initial location and orientation */
public Player(int x, int y)
{
teleport=false;
pelletsEaten=0;
pelletX = x/gridSize-1;
pelletY = y/gridSize-1;
this.lastX=x;
this.lastY=y;
this.x = x;
this.y = y;
currDirection='L';
desiredDirection='L';
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
public char newDirection()
{
int random;
char backwards='U';
int newX=x,newY=y;
int lookX=x,lookY=y;
Set<Character> set = new HashSet<Character>();
switch(direction)
{
case 'L':
backwards='R';
break;
case 'R':
backwards='L';
break;
case 'U':
backwards='D';
break;
case 'D':
backwards='U';
break;
}
char newDirection = backwards;
while (newDirection == backwards || !isValidDest(lookX,lookY))
{
if (set.size()==3)
{
newDirection=backwards;
break;
}
newX=x;
newY=y;
lookX=x;
lookY=y;
random = (int)(Math.random()*4) + 1;
if (random == 1)
{
newDirection = 'L';
newX-=increment;
lookX-= increment;
}
else if (random == 2)
{
newDirection = 'R';
newX+=increment;
lookX+= gridSize;
}
else if (random == 3)
{
newDirection = 'U';
newY-=increment;
lookY-=increment;
}
else if (random == 4)
{
newDirection = 'D';
newY+=increment;
lookY+=gridSize;
}
if (newDirection != backwards)
{
set.add(new Character(newDirection));
}
}
return newDirection;
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
public boolean isChoiceDest()
{
if ( x%gridSize==0&& y%gridSize==0 )
{
return true;
}
return false;
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
public void demoMove()
{
lastX=x;
lastY=y;
if (isChoiceDest())
{
direction = newDirection();
}
switch(direction)
{
case 'L':
if ( isValidDest(x-increment,y))
{
x -= increment;
}
else if (y == 9*gridSize && x < 2 * gridSize)
{
x = max - gridSize*1;
teleport = true;
}
break;
case 'R':
if ( isValidDest(x+gridSize,y))
{
x+= increment;
}
else if (y == 9*gridSize && x > max - gridSize*2)
{
x = 1*gridSize;
teleport=true;
}
break;
case 'U':
if ( isValidDest(x,y-increment))
y-= increment;
break;
case 'D':
if ( isValidDest(x,y+gridSize))
y+= increment;
break;
}
currDirection = direction;
frameCount ++;
}
/* The move function moves the pacman for one frame in non demo mode */
public void move()
{
int gridSize=20;
lastX=x;
lastY=y;
/* Try to turn in the direction input by the user */
/*Can only turn if we're in center of a grid*/
if (x %20==0 && y%20==0 ||
/* Or if we're reversing*/
(desiredDirection=='L' && currDirection=='R') ||
(desiredDirection=='R' && currDirection=='L') ||
(desiredDirection=='U' && currDirection=='D') ||
(desiredDirection=='D' && currDirection=='U')
)
{
switch(desiredDirection)
{
case 'L':
if ( isValidDest(x-increment,y))
x -= increment;
break;
case 'R':
if ( isValidDest(x+gridSize,y))
x+= increment;
break;
case 'U':
if ( isValidDest(x,y-increment))
y-= increment;
break;
case 'D':
if ( isValidDest(x,y+gridSize))
y+= increment;
break;
}
}
/* If we haven't moved, then move in the direction the pacman was headed anyway */
if (lastX==x && lastY==y)
{
switch(currDirection)
{
case 'L':
if ( isValidDest(x-increment,y))
x -= increment;
else if (y == 9*gridSize && x < 2 * gridSize)
{
x = max - gridSize*1;
teleport = true;
}
break;
case 'R':
if ( isValidDest(x+gridSize,y))
x+= increment;
else if (y == 9*gridSize && x > max - gridSize*2)
{
x = 1*gridSize;
teleport=true;
}
break;
case 'U':
if ( isValidDest(x,y-increment))
y-= increment;
break;
case 'D':
if ( isValidDest(x,y+gridSize))
y+= increment;
break;
}
}
/* If we did change direction, update currDirection to reflect that */
else
{
currDirection=desiredDirection;
}
/* If we didn't move at all, set the stopped flag */
if (lastX == x && lastY==y)
stopped=true;
/* Otherwise, clear the stopped flag and increment the frameCount for animation purposes*/
else
{
stopped=false;
frameCount ++;
}
}
/* Update what pellet the pacman is on top of */
public void updatePellet()
{
if (x%gridSize ==0 && y%gridSize == 0)
{
pelletX = x/gridSize-1;
pelletY = y/gridSize-1;
}
}
}
/* Ghost class controls the ghost. */
class Ghost extends Mover
{
/* Direction ghost is heading */
char direction;
/* Last ghost location*/
int lastX;
int lastY;
/* Current ghost location */
int x;
int y;
/* The pellet the ghost is on top of */
int pelletX,pelletY;
/* The pellet the ghost was last on top of */
int lastPelletX,lastPelletY;
/*Constructor places ghost and updates states*/
public Ghost(int x, int y)
{
direction='L';
pelletX=x/gridSize-1;
pelletY=x/gridSize-1;
lastPelletX=pelletX;
lastPelletY=pelletY;
this.lastX = x;
this.lastY = y;
this.x = x;
this.y = y;
}
/* update pellet status */
public void updatePellet()
{
int tempX,tempY;
tempX = x/gridSize-1;
tempY = y/gridSize-1;
if (tempX != pelletX || tempY != pelletY)
{
lastPelletX = pelletX;
lastPelletY = pelletY;
pelletX=tempX;
pelletY = tempY;
}
}
/* Determines if the location is one where the ghost has to make a decision*/
public boolean isChoiceDest()
{
if ( x%gridSize==0&& y%gridSize==0 )
{
return true;
}
return false;
}
/* Chooses a new direction randomly for the ghost to move */
public char newDirection()
{
int random;
char backwards='U';
int newX=x,newY=y;
int lookX=x,lookY=y;
Set<Character> set = new HashSet<Character>();
switch(direction)
{
case 'L':
backwards='R';
break;
case 'R':
backwards='L';
break;
case 'U':
backwards='D';
break;
case 'D':
backwards='U';
break;
}
char newDirection = backwards;
/* While we still haven't found a valid direction */
while (newDirection == backwards || !isValidDest(lookX,lookY))
{
/* If we've tried every location, turn around and break the loop */
if (set.size()==3)
{
newDirection=backwards;
break;
}
newX=x;
newY=y;
lookX=x;
lookY=y;
/* Randomly choose a direction */
random = (int)(Math.random()*4) + 1;
if (random == 1)
{
newDirection = 'L';
newX-=increment;
lookX-= increment;
}
else if (random == 2)
{
newDirection = 'R';
newX+=increment;
lookX+= gridSize;
}
else if (random == 3)
{
newDirection = 'U';
newY-=increment;
lookY-=increment;
}
else if (random == 4)
{
newDirection = 'D';
newY+=increment;
lookY+=gridSize;
}
if (newDirection != backwards)
{
set.add(new Character(newDirection));
}
}
return newDirection;
}
/* Random move function for ghost */
public void move()
{
lastX=x;
lastY=y;
/* If we can make a decision, pick a new direction randomly */
if (isChoiceDest())
{
direction = newDirection();
}
/* If that direction is valid, move that way */
switch(direction)
{
case 'L':
if ( isValidDest(x-increment,y))
x -= increment;
break;
case 'R':
if ( isValidDest(x+gridSize,y))
x+= increment;
break;
case 'U':
if ( isValidDest(x,y-increment))
y-= increment;
break;
case 'D':
if ( isValidDest(x,y+gridSize))
y+= increment;
break;
}
}
}
/*This board class contains the player, ghosts, pellets, and most of the game logic.*/
public class Board extends JPanel
{
/* Initialize the images*/
/* For JAR File*/
/*
Image pacmanImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/pacman.jpg"));
Image pacmanUpImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/pacmanup.jpg"));
Image pacmanDownImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/pacmandown.jpg"));
Image pacmanLeftImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/pacmanleft.jpg"));
Image pacmanRightImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/pacmanright.jpg"));
Image ghost10 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost10.jpg"));
Image ghost20 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost20.jpg"));
Image ghost30 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost30.jpg"));
Image ghost40 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost40.jpg"));
Image ghost11 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost11.jpg"));
Image ghost21 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost21.jpg"));
Image ghost31 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost31.jpg"));
Image ghost41 = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/ghost41.jpg"));
Image titleScreenImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/titleScreen.jpg"));
Image gameOverImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/gameOver.jpg"));
Image winScreenImage = Toolkit.getDefaultToolkit().getImage(Pacman.class.getResource("img/winScreen.jpg"));
*/
/* For NOT JAR file*/
Image pacmanImage = Toolkit.getDefaultToolkit().getImage("img/pacman.jpg");
Image pacmanUpImage = Toolkit.getDefaultToolkit().getImage("img/pacmanup.jpg");
Image pacmanDownImage = Toolkit.getDefaultToolkit().getImage("img/pacmandown.jpg");
Image pacmanLeftImage = Toolkit.getDefaultToolkit().getImage("img/pacmanleft.jpg");
Image pacmanRightImage = Toolkit.getDefaultToolkit().getImage("img/pacmanright.jpg");
Image ghost10 = Toolkit.getDefaultToolkit().getImage("img/ghost10.jpg");
Image ghost20 = Toolkit.getDefaultToolkit().getImage("img/ghost20.jpg");
Image ghost30 = Toolkit.getDefaultToolkit().getImage("img/ghost30.jpg");
Image ghost40 = Toolkit.getDefaultToolkit().getImage("img/ghost40.jpg");
Image ghost11 = Toolkit.getDefaultToolkit().getImage("img/ghost11.jpg");
Image ghost21 = Toolkit.getDefaultToolkit().getImage("img/ghost21.jpg");
Image ghost31 = Toolkit.getDefaultToolkit().getImage("img/ghost31.jpg");
Image ghost41 = Toolkit.getDefaultToolkit().getImage("img/ghost41.jpg");
Image titleScreenImage = Toolkit.getDefaultToolkit().getImage("img/titleScreen.jpg");
Image gameOverImage = Toolkit.getDefaultToolkit().getImage("img/gameOver.jpg");
Image winScreenImage = Toolkit.getDefaultToolkit().getImage("img/winScreen.jpg");
/* Initialize the player and ghosts */
Player player = new Player(200,300);
Ghost ghost1 = new Ghost(180,180);
Ghost ghost2 = new Ghost(200,180);
Ghost ghost3 = new Ghost(220,180);
Ghost ghost4 = new Ghost(220,180);
/* Timer is used for playing sound effects and animations */
long timer = System.currentTimeMillis();
/* Dying is used to count frames in the dying animation. If it's non-zero,
pacman is in the process of dying */
int dying=0;
/* Score information */
int currScore;
int highScore;
/* if the high scores have been cleared, we have to update the top of the screen to reflect that */
boolean clearHighScores= false;
int numLives=2;
/*Contains the game map, passed to player and ghosts */
boolean[][] state;
/* Contains the state of all pellets*/
boolean[][] pellets;
/* Game dimensions */
int gridSize;
int max;
/* State flags*/
boolean stopped;
boolean titleScreen;
boolean winScreen = false;
boolean overScreen = false;
boolean demo = false;
int New;
/* Used to call sound effects */
GameSounds sounds;
int lastPelletEatenX = 0;
int lastPelletEatenY=0;
/* This is the font used for the menus */
Font font = new Font("Monospaced",Font.BOLD, 12);
/* Constructor initializes state flags etc.*/
public Board()
{
initHighScores();
sounds = new GameSounds();
currScore=0;
stopped=false;
max=400;
gridSize=20;
New=0;
titleScreen = true;
}
/* Reads the high scores file and saves it */
public void initHighScores()
{
File file = new File("highScores.txt");
Scanner sc;
try
{
sc = new Scanner(file);
highScore = sc.nextInt();
sc.close();
}
catch(Exception e)
{
}
}
/* Writes the new high score to a file and sets flag to update it on screen */
public void updateScore(int score)
{
PrintWriter out;
try
{
out = new PrintWriter("highScores.txt");
out.println(score);
out.close();
}
catch(Exception e)
{
}
highScore=score;
clearHighScores=true;
}
/* Wipes the high scores file and sets flag to update it on screen */
public void clearHighScores()
{
PrintWriter out;
try
{
out = new PrintWriter("highScores.txt");
out.println("0");
out.close();
}
catch(Exception e)
{
}
highScore=0;
clearHighScores=true;
}
/* Reset occurs on a new game*/
public void reset()
{
numLives=2;
state = new boolean[20][20];
pellets = new boolean[20][20];
/* Clear state and pellets arrays */
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
state[i][j]=true;
pellets[i][j]=true;
}
}
/* Handle the weird spots with no pellets*/
for(int i = 5;i<14;i++)
{
for(int j = 5;j<12;j++)
{
pellets[i][j]=false;
}
}
pellets[9][7] = false;
pellets[8][8] = false;
pellets[9][8] = false;
pellets[10][8] = false;
}
/* Function is called during drawing of the map.
Whenever the a portion of the map is covered up with a barrier,
the map and pellets arrays are updated accordingly to note
that those are invalid locations to travel or put pellets
*/
public void updateMap(int x,int y, int width, int height)
{
for (int i =x/gridSize; i<x/gridSize+width/gridSize;i++)
{
for (int j=y/gridSize;j<y/gridSize+height/gridSize;j++)
{
state[i-1][j-1]=false;
pellets[i-1][j-1]=false;
}
}
}
/* Draws the appropriate number of lives on the bottom left of the screen.
Also draws the menu */
public void drawLives(Graphics g)
{
g.setColor(Color.BLACK);
/*Clear the bottom bar*/
g.fillRect(0,max+5,600,gridSize);
g.setColor(Color.YELLOW);
for(int i = 0;i<numLives;i++)
{
/*Draw each life */
g.fillOval(gridSize*(i+1),max+5,gridSize,gridSize);
}
/* Draw the menu items */
g.setColor(Color.YELLOW);
g.setFont(font);
g.drawString("Reset",100,max+5+gridSize);
g.drawString("Clear High Scores",180,max+5+gridSize);
g.drawString("Exit",350,max+5+gridSize);
}
/* This function draws the board. The pacman board is really complicated and can only feasibly be done
manually. Whenever I draw a wall, I call updateMap to invalidate those coordinates. This way the pacman
and ghosts know that they can't traverse this area */
public void drawBoard(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,600,600);
g.setColor(Color.BLACK);
g.fillRect(0,0,420,420);
g.setColor(Color.BLACK);
g.fillRect(0,0,20,600);
g.fillRect(0,0,600,20);
g.setColor(Color.WHITE);
g.drawRect(19,19,382,382);
g.setColor(Color.BLUE);
g.fillRect(40,40,60,20);
updateMap(40,40,60,20);
g.fillRect(120,40,60,20);
updateMap(120,40,60,20);
g.fillRect(200,20,20,40);
updateMap(200,20,20,40);
g.fillRect(240,40,60,20);
updateMap(240,40,60,20);
g.fillRect(320,40,60,20);
updateMap(320,40,60,20);
g.fillRect(40,80,60,20);
updateMap(40,80,60,20);
g.fillRect(160,80,100,20);
updateMap(160,80,100,20);
g.fillRect(200,80,20,60);
updateMap(200,80,20,60);
g.fillRect(320,80,60,20);
updateMap(320,80,60,20);
g.fillRect(20,120,80,60);
updateMap(20,120,80,60);
g.fillRect(320,120,80,60);
updateMap(320,120,80,60);
g.fillRect(20,200,80,60);
updateMap(20,200,80,60);
g.fillRect(320,200,80,60);
updateMap(320,200,80,60);
g.fillRect(160,160,40,20);
updateMap(160,160,40,20);
g.fillRect(220,160,40,20);
updateMap(220,160,40,20);
g.fillRect(160,180,20,20);
updateMap(160,180,20,20);
g.fillRect(160,200,100,20);
updateMap(160,200,100,20);
g.fillRect(240,180,20,20);
updateMap(240,180,20,20);
g.setColor(Color.BLUE);
g.fillRect(120,120,60,20);
updateMap(120,120,60,20);
g.fillRect(120,80,20,100);
updateMap(120,80,20,100);
g.fillRect(280,80,20,100);
updateMap(280,80,20,100);
g.fillRect(240,120,60,20);
updateMap(240,120,60,20);
g.fillRect(280,200,20,60);
updateMap(280,200,20,60);
g.fillRect(120,200,20,60);
updateMap(120,200,20,60);
g.fillRect(160,240,100,20);
updateMap(160,240,100,20);
g.fillRect(200,260,20,40);
updateMap(200,260,20,40);
g.fillRect(120,280,60,20);
updateMap(120,280,60,20);
g.fillRect(240,280,60,20);
updateMap(240,280,60,20);
g.fillRect(40,280,60,20);
updateMap(40,280,60,20);
g.fillRect(80,280,20,60);
updateMap(80,280,20,60);
g.fillRect(320,280,60,20);
updateMap(320,280,60,20);
g.fillRect(320,280,20,60);
updateMap(320,280,20,60);
g.fillRect(20,320,40,20);
updateMap(20,320,40,20);
g.fillRect(360,320,40,20);
updateMap(360,320,40,20);
g.fillRect(160,320,100,20);
updateMap(160,320,100,20);
g.fillRect(200,320,20,60);
updateMap(200,320,20,60);
g.fillRect(40,360,140,20);
updateMap(40,360,140,20);
g.fillRect(240,360,140,20);
updateMap(240,360,140,20);
g.fillRect(280,320,20,40);
updateMap(280,320,20,60);
g.fillRect(120,320,20,60);
updateMap(120,320,20,60);
drawLives(g);
}
/* Draws the pellets on the screen */
public void drawPellets(Graphics g)
{
g.setColor(Color.YELLOW);
for (int i=1;i<20;i++)
{
for (int j=1;j<20;j++)
{
if ( pellets[i-1][j-1])
g.fillOval(i*20+8,j*20+8,4,4);
}
}
}
/* Draws one individual pellet. Used to redraw pellets that ghosts have run over */
public void fillPellet(int x, int y, Graphics g)
{
g.setColor(Color.YELLOW);
g.fillOval(x*20+28,y*20+28,4,4);
}
/* This is the main function that draws one entire frame of the game */
public void paint(Graphics g)
{
/* If we're playing the dying animation, don't update the entire screen.
Just kill the pacman*/
if (dying > 0)
{
/* Stop any pacman eating sounds */
sounds.nomNomStop();
/* Draw the pacman */
g.drawImage(pacmanImage,player.x,player.y,Color.BLACK,null);
g.setColor(Color.BLACK);
/* Kill the pacman */
if (dying == 4)
g.fillRect(player.x,player.y,20,7);
else if ( dying == 3)
g.fillRect(player.x,player.y,20,14);
else if ( dying == 2)
g.fillRect(player.x,player.y,20,20);
else if ( dying == 1)
{
g.fillRect(player.x,player.y,20,20);
}
/* Take .1 seconds on each frame of death, and then take 2 seconds
for the final frame to allow for the sound effect to end */
long currTime = System.currentTimeMillis();
long temp;
if (dying != 1)
temp = 100;
else
temp = 2000;
/* If it's time to draw a new death frame... */
if (currTime - timer >= temp)
{
dying--;
timer = currTime;
/* If this was the last death frame...*/
if (dying == 0)
{
if (numLives==-1)
{
/* Demo mode has infinite lives, just give it more lives*/
if (demo)
numLives=2;
else
{
/* Game over for player. If relevant, update high score. Set gameOver flag*/
if (currScore > highScore)
{
updateScore(currScore);
}
overScreen=true;
}
}
}
}
return;
}
/* If this is the title screen, draw the title screen and return */
if (titleScreen)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,600,600);
g.drawImage(titleScreenImage,0,0,Color.BLACK,null);
/* Stop any pacman eating sounds */
sounds.nomNomStop();
New = 1;
return;
}
/* If this is the win screen, draw the win screen and return */
else if (winScreen)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,600,600);
g.drawImage(winScreenImage,0,0,Color.BLACK,null);
New = 1;
/* Stop any pacman eating sounds */
sounds.nomNomStop();
return;
}
/* If this is the game over screen, draw the game over screen and return */
else if (overScreen)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,600,600);
g.drawImage(gameOverImage,0,0,Color.BLACK,null);
New = 1;
/* Stop any pacman eating sounds */
sounds.nomNomStop();
return;
}
/* If need to update the high scores, redraw the top menu bar */
if (clearHighScores)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,600,18);
g.setColor(Color.YELLOW);
g.setFont(font);
clearHighScores= false;
if (demo)
g.drawString("DEMO MODE PRESS ANY KEY TO START A GAME\t High Score: "+highScore,20,10);
else
g.drawString("Score: "+(currScore)+"\t High Score: "+highScore,20,10);
}
/* oops is set to true when pacman has lost a life */
boolean oops=false;
/* Game initialization */
if (New==1)
{
reset();
player = new Player(200,300);
ghost1 = new Ghost(180,180);
ghost2 = new Ghost(200,180);
ghost3 = new Ghost(220,180);
ghost4 = new Ghost(220,180);
currScore = 0;
drawBoard(g);
drawPellets(g);
drawLives(g);
/* Send the game map to player and all ghosts */
player.updateState(state);
/* Don't let the player go in the ghost box*/
player.state[9][7]=false;