forked from dtschust/javapacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
819 lines (723 loc) · 23.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
/* Drew Schuster */
import java.awt.*;
import javax.imageio.*;
import javax.swing.JPanel;
import java.lang.Math;
import java.util.*;
import java.io.*;
/*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 = ResourceUtils.loadImage("img/pacman.jpg");
Image pacmanUpImage = ResourceUtils.loadImage("img/pacmanup.jpg");
Image pacmanDownImage = ResourceUtils.loadImage("img/pacmandown.jpg");
Image pacmanLeftImage = ResourceUtils.loadImage("img/pacmanleft.jpg");
Image pacmanRightImage = ResourceUtils.loadImage("img/pacmanright.jpg");
Image ghost10 = ResourceUtils.loadImage("img/ghost10.jpg");
Image ghost20 = ResourceUtils.loadImage("img/ghost20.jpg");
Image ghost30 = ResourceUtils.loadImage("img/ghost30.jpg");
Image ghost40 = ResourceUtils.loadImage("img/ghost40.jpg");
Image ghost11 = ResourceUtils.loadImage("img/ghost11.jpg");
Image ghost21 = ResourceUtils.loadImage("img/ghost21.jpg");
Image ghost31 = ResourceUtils.loadImage("img/ghost31.jpg");
Image ghost41 = ResourceUtils.loadImage("img/ghost41.jpg");
Image titleScreenImage = ResourceUtils.loadImage("img/titleScreen.jpg");
Image gameOverImage = ResourceUtils.loadImage("img/gameOver.jpg");
Image winScreenImage = ResourceUtils.loadImage("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();
long titleTimer = -1;
long gameTimer = -1;
/* 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 */
Highschore score;
/* 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()
{
score = new Highschore();
sounds = new GameSounds();
score.currScore=0;
stopped=false;
max=400;
gridSize=20;
New=0;
titleScreen = 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);
}
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);
}
private void drawGhosts(Graphics g, Image ghost10, Image ghost20, Image ghost30, Image ghost40) {
g.drawImage(ghost10, ghost1.x, ghost1.y, Color.BLACK, null);
g.drawImage(ghost20, ghost2.x, ghost2.y, Color.BLACK, null);
g.drawImage(ghost30, ghost3.x, ghost3.y, Color.BLACK, null);
g.drawImage(ghost40, ghost4.x, ghost4.y, Color.BLACK, null);
}
/* 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 (score.currScore > score.highScore)
{
score.updateScore(score.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: "+score.highScore,20,10);
else
g.drawString("Score: "+(score.currScore)+"\t High Score: "+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);
score.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;
ghost1.updateState(state);
ghost2.updateState(state);
ghost3.updateState(state);
ghost4.updateState(state);
/* Draw the top menu bar*/
g.setColor(Color.YELLOW);
g.setFont(font);
if (demo)
g.drawString("DEMO MODE PRESS ANY KEY TO START A GAME\t High Score: "+score.highScore,20,10);
else
g.drawString("Score: "+(score.currScore)+"\t High Score: "+score.highScore,20,10);
New++;
}
/* Second frame of new game */
else if (New == 2)
{
New++;
}
/* Third frame of new game */
else if (New == 3)
{
New++;
/* Play the newGame sound effect */
sounds.newGame();
timer = System.currentTimeMillis();
return;
}
/* Fourth frame of new game */
else if (New == 4)
{
/* Stay in this state until the sound effect is over */
long currTime = System.currentTimeMillis();
if (currTime - timer >= 5000)
{
New=0;
}
else
return;
}
/* Drawing optimization */
g.copyArea(player.x-20,player.y-20,80,80,0,0);
g.copyArea(ghost1.x-20,ghost1.y-20,80,80,0,0);
g.copyArea(ghost2.x-20,ghost2.y-20,80,80,0,0);
g.copyArea(ghost3.x-20,ghost3.y-20,80,80,0,0);
g.copyArea(ghost4.x-20,ghost4.y-20,80,80,0,0);
/* Detect collisions */
if (player.x == ghost1.x && Math.abs(player.y-ghost1.y) < 10)
oops=true;
else if (player.x == ghost2.x && Math.abs(player.y-ghost2.y) < 10)
oops=true;
else if (player.x == ghost3.x && Math.abs(player.y-ghost3.y) < 10)
oops=true;
else if (player.x == ghost4.x && Math.abs(player.y-ghost4.y) < 10)
oops=true;
else if (player.y == ghost1.y && Math.abs(player.x-ghost1.x) < 10)
oops=true;
else if (player.y == ghost2.y && Math.abs(player.x-ghost2.x) < 10)
oops=true;
else if (player.y == ghost3.y && Math.abs(player.x-ghost3.x) < 10)
oops=true;
else if (player.y == ghost4.y && Math.abs(player.x-ghost4.x) < 10)
oops=true;
/* Kill the pacman */
if (oops && !stopped)
{
/* 4 frames of death*/
dying=4;
/* Play death sound effect */
sounds.death();
/* Stop any pacman eating sounds */
sounds.nomNomStop();
/*Decrement lives, update screen to reflect that. And set appropriate flags and timers */
numLives--;
stopped=true;
drawLives(g);
timer = System.currentTimeMillis();
}
/* Delete the players and ghosts */
g.setColor(Color.BLACK);
g.fillRect(player.lastX,player.lastY,20,20);
g.fillRect(ghost1.lastX,ghost1.lastY,20,20);
g.fillRect(ghost2.lastX,ghost2.lastY,20,20);
g.fillRect(ghost3.lastX,ghost3.lastY,20,20);
g.fillRect(ghost4.lastX,ghost4.lastY,20,20);
/* Eat pellets */
if ( pellets[player.pelletX][player.pelletY] && New!=2 && New !=3)
{
lastPelletEatenX = player.pelletX;
lastPelletEatenY = player.pelletY;
/* Play eating sound */
sounds.nomNom();
/* Increment pellets eaten value to track for end game */
player.pelletsEaten++;
/* Delete the pellet*/
pellets[player.pelletX][player.pelletY]=false;
/* Increment the score */
score.currScore += 50;
/* Update the screen to reflect the new score */
g.setColor(Color.BLACK);
g.fillRect(0,0,600,20);
g.setColor(Color.YELLOW);
g.setFont(font);
if (demo)
g.drawString("DEMO MODE PRESS ANY KEY TO START A GAME\t High Score: "+score.highScore,20,10);
else
g.drawString("Score: "+(score.currScore)+"\t High Score: "+score.highScore,20,10);
/* If this was the last pellet */
if (player.pelletsEaten == 173)
{
/*Demo mode can't get a high score */
if (!demo)
{
if (score.currScore > score.highScore)
{
score.updateScore(score.currScore);
}
winScreen = true;
}
else
{
titleScreen = true;
}
return;
}
}
/* If we moved to a location without pellets, stop the sounds */
else if ( (player.pelletX != lastPelletEatenX || player.pelletY != lastPelletEatenY ) || player.stopped)
{
/* Stop any pacman eating sounds */
sounds.nomNomStop();
}
/* Replace pellets that have been run over by ghosts */
if ( pellets[ghost1.lastPelletX][ghost1.lastPelletY])
fillPellet(ghost1.lastPelletX,ghost1.lastPelletY,g);
if ( pellets[ghost2.lastPelletX][ghost2.lastPelletY])
fillPellet(ghost2.lastPelletX,ghost2.lastPelletY,g);
if ( pellets[ghost3.lastPelletX][ghost3.lastPelletY])
fillPellet(ghost3.lastPelletX,ghost3.lastPelletY,g);
if ( pellets[ghost4.lastPelletX][ghost4.lastPelletY])
fillPellet(ghost4.lastPelletX,ghost4.lastPelletY,g);
/*Draw the ghosts */
if (ghost1.frameCount < 5)
{
/* Draw first frame of ghosts */
drawGhosts(g,ghost10,ghost20,ghost30,ghost40);
ghost1.frameCount++;
}
else
{
/* Draw second frame of ghosts */
drawGhosts(g,ghost11,ghost21,ghost31,ghost41);
if (ghost1.frameCount >=10)
ghost1.frameCount=0;
else
ghost1.frameCount++;
}
/* Draw the pacman */
if (player.frameCount < 5)
{
/* Draw mouth closed */
g.drawImage(pacmanImage,player.x,player.y,Color.BLACK,null);
}
else
{
/* Draw mouth open in appropriate direction */
if (player.frameCount >=10)
player.frameCount=0;
switch(player.currDirection)
{
case 'L':
g.drawImage(pacmanLeftImage,player.x,player.y,Color.BLACK,null);
break;
case 'R':
g.drawImage(pacmanRightImage,player.x,player.y,Color.BLACK,null);
break;
case 'U':
g.drawImage(pacmanUpImage,player.x,player.y,Color.BLACK,null);
break;
case 'D':
g.drawImage(pacmanDownImage,player.x,player.y,Color.BLACK,null);
break;
}
}
/* Draw the border around the game in case it was overwritten by ghost movement or something */
g.setColor(Color.WHITE);
g.drawRect(19,19,382,382);
}
public void repaintBoard()
{
if (player.teleport)
{
repaint(player.lastX-20,player.lastY-20,80,80);
player.teleport=false;
}
repaint(0,0,600,20);
repaint(0,420,600,40);
repaint(player.x-20,player.y-20,80,80);
repaint(ghost1.x-20,ghost1.y-20,80,80);
repaint(ghost2.x-20,ghost2.y-20,80,80);
repaint(ghost3.x-20,ghost3.y-20,80,80);
repaint(ghost4.x-20,ghost4.y-20,80,80);
}
/* Steps the screen forward one frame */
public void stepFrame(boolean New,javax.swing.Timer frameTimer )
{
/* If we aren't on a special screen than the timers can be set to -1 to disable them */
if (!titleScreen && !winScreen && !overScreen)
{
gameTimer = -1;
titleTimer = -1;
}
/* If we are playing the dying animation, keep advancing frames until the animation is complete */
if (dying>0)
{
repaint();
return;
}
/* New can either be specified by the New parameter in stepFrame function call or by the state
of b.New. Update New accordingly */
New = New || (this.New !=0) ;
/* If this is the title screen, make sure to only stay on the title screen for 5 seconds.
If after 5 seconds the user hasn't started a game, start up demo mode */
if (titleScreen)
{
if (titleTimer == -1)
{
titleTimer = System.currentTimeMillis();
}
long currTime = System.currentTimeMillis();
if (currTime - titleTimer >= 5000)
{
titleScreen = false;
demo = true;
titleTimer = -1;
}
repaint();
return;
}
/* If this is the win screen or game over screen, make sure to only stay on the screen for 5 seconds.
If after 5 seconds the user hasn't pressed a key, go to title screen */
else if (winScreen || overScreen)
{
if (timer == -1)
{
gameTimer = System.currentTimeMillis();
}
long currTime = System.currentTimeMillis();
if (currTime - gameTimer >= 5000)
{
winScreen = false;
overScreen = false;
titleScreen = true;
gameTimer = -1;
}
repaint();
return;
}
/* If we have a normal game state, move all pieces and update pellet status */
if (!New)
{
/* The pacman player has two functions, demoMove if we're in demo mode and move if we're in
user playable mode. Call the appropriate one here */
if (demo)
{
player.demoMove();
}
else
{
player.move();
}
/* Also move the ghosts, and update the pellet states */
ghost1.move();
ghost2.move();
ghost3.move();
ghost4.move();
player.updatePellet();
ghost1.updatePellet();
ghost2.updatePellet();
ghost3.updatePellet();
ghost4.updatePellet();
}
/* We either have a new game or the user has died, either way we have to reset the board */
if (stopped || New)
{
/*Temporarily stop advancing frames */
frameTimer.stop();
/* If user is dying ... */
while (dying >0)
{
/* Play dying animation. */
stepFrame(false,frameTimer);
}
/* Move all game elements back to starting positions and orientations */
player.currDirection='L';
player.direction='L';
player.desiredDirection='L';
player.x = 200;
player.y = 300;
ghost1.x = 180;
ghost1.y = 180;
ghost2.x = 200;
ghost2.y = 180;
ghost3.x = 220;
ghost3.y = 180;
ghost4.x = 220;
ghost4.y = 180;
/* Advance a frame to display main state*/
repaint(0,0,600,600);
/*Start advancing frames once again*/
stopped=false;
frameTimer.start();
}
/* Otherwise we're in a normal state, advance one frame*/
else
{
repaintBoard();
}
}
}