forked from skenny/Pacman
/
pacman.c
1709 lines (1499 loc) · 45.1 KB
/
pacman.c
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
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
/******************* BEGIN DECLARATIONS *******************/
/* Global Variables */
char board[32][32]; /* array storing board layout */
char ghostSPHome[32][32]; /* ghosts shortest-path home directions */
GLfloat wallHeight = 1.0; /* heighte of maze walls */
GLuint walls; /* wall display list */
int boardWidth = 0; /* unit width of board */
int boardHeight = 0; /* unit hight of board */
int pellets = 0; /* number of pellets */
int superPellets = 0; /* number of super-pellets */
int powerupTime = 7; /* super-pellet powerup duration (secs) */
int playersLeft = 3; /* number of remaining "men" */
int gameSpeed = 100; /* speed of the movement function called */
int powerupTimeBase = 0; /* start time of pacman power-up */
int ghostScoreMultiplier = 1; /* score multiplier when eating a ghost */
int windowHeight = 400; /* game window height */
int windowWidth = 400; /* game window width */
int level = 0; /* current level */
int numLevels = 0; /* number of levels */
int tiltBoard = 0; /* tilt board during play */
int gameStarted = 0; /* set to 1 when playing */
int flashReady = 0; /* flash READY!*/
int flashPointsX; /* x position to position flashing text */
int flashPointsY; /* y position to position flashing text */
int flashPointsScore; /* points to flash at (x,y) */
int flashPoints = 0; /* flash the points? */
int pacmanDefaultSpeed = 125; /* default pacman speed */
int ghostDefaultSpeed = 125; /* default ghost speed */
long score = 0; /* player score */
long topScore = 10000; /* top player score */
int newTopScore = 0; /* did the player get a new top score? */
/* Array of level filenames - 32 max */
char levels[32][32];
/* Camera positioning and rotation angles */
GLfloat camera[] = {10.0, 1.0, 35.0}; //{13.0, 6.0, 45.0};
GLfloat theta[] = {-20.0, 0.0, 0.0};
/* Player structure attribute values */
enum directions {up, down, left, right};
enum status {dead, scared, normal, poweredUp};
/* Player structure; collection of attributes for pacman & ghosts */
struct player {
int x, y; /* x, y position */
int startX, startY; /* starting x, y position */
int speed; /* player's current movement speed */
enum directions direction; /* direction player is moving */
enum directions nextDirection; /* player's next direction */
GLfloat colorf[3]; /* color of player */
GLfloat size; /* size of player */
enum status state; /* status of player */
} pacman, ghosts[4];
/* Lighting variables */
GLfloat l0_diffuse[] = {0.9, 0.9, 0.9, 1.0};
GLfloat l0_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat l0_specular[] = {0.7, 0.7, 0.7, 1.0};
GLfloat l0_pos[] = {5.0, 0.0, 5.0, 0.0};
/* Display functions */
void gameDisplay(void);
void gameOverDisplay(void);
void instrDisplay(void);
void killPacmanDisplay(void);
void levelUpDisplay(void);
void titleScreenDisplay(void);
void drawPacman(void);
void drawGhost(int);
void reshapeWindow(int, int);
/* Initialization functions */
void loadLevel(char*);
void initialize(void);
/* Input handler functions */
void keyInput(unsigned char, int, int);
void specialKeyInput(int, int, int);
void menu(int);
/* Player movement functions */
void movement(int);
void movePacman(int);
void moveGhosts(int);
void newGhostDirection(int);
void detectCollision(void);
int canMove(GLfloat, GLfloat, int);
/* Helper functions */
void colorGhost(int);
void colorGhosts(void);
void playerSetup(int);
int randomNum(int);
void disableFlashPoints(int);
void disableFlashReady(int);
void togglePause(void);
void toggleTilt(void);
void displayText(GLfloat, GLfloat, GLfloat, char*);
/******************** END DECLARATIONS ********************/
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("PacmanGL");
printf("PacmanGL\n\n");
printf("COMP3773 Project\n");
printf("Shaun Kenny\n");
printf("034616k@acadiau.ca\n\n");
printf("Controls:\n Movement: up, down, left, right arrows\n");
printf(" p: Pause/unpause\n");
printf(" t: Toggle board tilt during play\n");
printf(" q: Quit\n");
printf("Use the right mouse button for menus\n\n");
/* Load board, initialize opengl, setup player/ghosts */
initialize();
/* Enable lighting */
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, l0_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
/* Display title screen */
glutDisplayFunc(titleScreenDisplay);
glutMainLoop();
}
/* Function: gameDisplay
* Parameters: none
* Display function used when the player is playing the game.
* Draws score, top score, the board, pacman and ghosts. Sets
* up right click menu with "Pause", "Toggle Tilt", "End Game"
* and "Quit" options. If the gameStarted flag is set, re-registers
* timer functions for movement.
*/
void gameDisplay() {
int x, y, i;
int digits = 1;
char scoreStr[6];
GLUquadricObj *sphere = gluNewQuadric();
/* Build Menu */
glutCreateMenu(menu);
if (gameStarted)
glutAddMenuEntry("Pause", 5);
else
glutAddMenuEntry("Un-pause", 5);
glutAddMenuEntry("Toggle Tilt", 10);
glutAddMenuEntry("End Game", 3);
glutAddMenuEntry("Quit", 99);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
/* Translate center of board to origin for rotation */
glTranslatef(boardWidth / 2.0, boardHeight / 2.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
glTranslatef(-(boardWidth / 2.0), -(boardHeight / 2.0), 0.0);
/* Draw the floor */
glBegin(GL_QUADS);
glColor3f(0.5, 0.2, 0.2);
glVertex2f(0.0, 0.0);
glVertex2f(boardWidth, 0.0);
glVertex2f(boardWidth, boardHeight);
glVertex2f(0.0, boardHeight);
glEnd();
/* Draw the walls */
glCallList(walls);
/* Draw everything else */
for (y = 0; y < boardHeight; y++) {
for (x = 0; x < boardWidth; x++) {
/* Pellet */
if (board[y][x] == '.') {
glDisable(GL_LIGHTING);
glBegin(GL_POINTS);
glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0));
glVertex3f((GLfloat)x + 0.5, boardHeight - (GLfloat)y - 0.5, 0.5);
glEnd();
glEnable(GL_LIGHTING);
}
/* Super-pellet */
if (board[y][x] == 'P') {
glTranslatef(x + 0.5, ((boardHeight - y) - 0.5), 0.5);
glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0));
gluSphere(sphere, 0.3, 12, 12);
glTranslatef(-(x + 0.5), -((boardHeight - y) - 0.5), -0.5);
}
/* Ghost door */
if (board[y][x] == 'G') {
glBegin(GL_QUADS);
glColor3f((252.0 / 255.0), (182.0 / 255.0), (220.0 / 255.0));
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y - 0.5, 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y - 0.5, 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y - 0.5, wallHeight);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y - 0.5, wallHeight);
glEnd();
}
}
}
/* Draw pacman */
glPushMatrix();
glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), (pacman.size / 2.0));
drawPacman();
glPopMatrix();
/* Draw the ghosts */
for (i = 0; i < 4; i++) {
glPushMatrix();
glTranslatef(ghosts[i].x + (ghosts[i].size / 2.0), ghosts[i].y - (ghosts[i].size / 2.0), 0.0);
drawGhost(i);
glPopMatrix();
}
/* Draw players left */
glPushMatrix();
glTranslatef(boardWidth - 1.0, boardHeight + 2.0, 0.5);
for (i = 0; i < playersLeft; i++) {
glColor3fv(pacman.colorf);
gluSphere(sphere, (pacman.size / 2.0) - 0.1, 10, 10);
glTranslatef(-1.0, 0.0, 0.0);
}
glPopMatrix();
/* Flash points if Pacman ate a ghost */
if (flashPoints) {
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 1.0);
displayText((GLfloat)flashPointsX, (GLfloat)flashPointsY, 2.5, itoa(flashPointsScore, scoreStr, 10));
glEnable(GL_LIGHTING);
}
/* Flash READY! if just starting */
if (flashReady > 0) {
if (flashReady == 1 || flashReady == 3 || flashReady == 5) {
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 1.0);
displayText(((GLfloat)boardWidth / 2.0) - 2.0, (GLfloat)boardHeight / 2.0, 2.0, "READY!");
glEnable(GL_LIGHTING);
}
}
/* Print score */
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 1.0);
displayText(0.5, (GLfloat)boardHeight + 3.0, 1.0, "SCORE");
displayText(0.5, (GLfloat)boardHeight + 1.5, 1.0, itoa(score, scoreStr,10));
displayText(10.0, (GLfloat)boardHeight + 3.0, 1.0, "TOP SCORE");
displayText(11.0, (GLfloat)boardHeight + 1.5, 1.0, itoa(topScore, scoreStr, 10));
glEnable(GL_LIGHTING);
gluDeleteQuadric(sphere);
glFlush();
glutSwapBuffers();
}
/* Function: gameOverDisplay
* Parameters: none
* Display function used when the player runs out of remaining
* lives.
*/
void gameOverDisplay() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Reset level, score */
score = 0;
level = 0;
/* Build Menu */
glutCreateMenu(menu);
glutAddMenuEntry("Play Again", 1);
glutAddMenuEntry("Title Screen", 3);
glutAddMenuEntry("Quit", 99);
glutAttachMenu(GLUT_RIGHT_BUTTON);
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 1.0);
displayText(10.0, 14.0, 0.0, "GAME OVER!");
if (newTopScore) {
displayText(4.5, 11.0, 0.0, "You got a new high score!");
}
glEnable(GL_LIGHTING);
glFlush();
glutSwapBuffers();
}
/* Function: instrDisplay
* Parameters: none
* Display function used when the player is at the instructions
* screen. Prints a lot of text and rotating player models. Sets
* up right click menu with "Play", "Title Screen" and "Quit"
* options. Continuously redisplays.
*/
void instrDisplay() {
static int angle = 0;
int i;
GLUquadricObj *sphere = gluNewQuadric();
/* Build Menu */
glutCreateMenu(menu);
glutAddMenuEntry("Play!", 1);
glutAddMenuEntry("Title Screen", 3);
glutAddMenuEntry("Quit", 99);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
/* Turn off lighting, print text */
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_LIGHTING);
displayText(9.0, 31.0, 0.0, "Instructions");
displayText(11.0, 27.5, 0.0, "PLAYERS");
displayText(2.0, 24.5, 0.0, "You:");
displayText(15.0, 24.5, 0.0, "The Enemy:");
displayText(11.0, 15.0, 0.0, "SCORING");
displayText(13.0, 13.0, 0.0, "10 pts");
displayText(13.0, 11.0, 0.0, "50 pts");
displayText(10.0, 8.5, 0.0, "1st: 200 pts");
displayText(10.0, 7.0, 0.0, "2nd: 400 pts");
displayText(10.0, 5.5, 0.0, "3rd: 800 pts");
displayText(10.0, 4.0, 0.0, "4th: 1600 pts");
displayText(4.0, 1.0, 0.0, "Extra player every 20000 pts");
displayText(6.0, -1.0, 0.0, "Use arrow keys to move");
glEnable(GL_LIGHTING);
/* You: pacman */
glPushMatrix();
glTranslatef(7.0, 25.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glScalef(2.0, 2.0, 2.0);
drawPacman();
glPopMatrix();
/* Enemy: ghosts */
glPushMatrix();
glTranslatef(25.0, 24.5, 0.0);
glScalef(2.0, 2.0, 2.0);
for (i = 0; i < 4; i++) {
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
drawGhost(i);
glPopMatrix();
glTranslatef(0.0, -1.0, 0.0);
}
glPopMatrix();
/* Scoring: small pellet */
glPushMatrix();
glTranslatef(11.0, 13.5, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0));
gluSphere(sphere, 0.25, 10, 10);
glPopMatrix();
/* Scoring: large pellet */
glPushMatrix();
glTranslatef(11.0, 11.5, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0));
gluSphere(sphere, 0.5, 10, 10);
glPopMatrix();
/* Scoring: blue ghosts */
glPushMatrix();
glTranslatef(8.0, 8.0, 0.0);
glScalef(2.0, 2.0, 2.0);
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
ghosts[0].colorf[0] = 0.0;
ghosts[0].colorf[1] = 0.0;
ghosts[0].colorf[2] = 1.0;
drawGhost(0);
glPopMatrix();
glTranslatef(0.0, -1.0, 0.0);
colorGhosts();
glPopMatrix();
angle = (angle + 2) % 360; /* rotation angle */
gluDeleteQuadric(sphere);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
/* Function: killPacmanDisplay
* Parameters: none
* Display function used when the player is killed by a ghost.
* Draws pacman at the current position and redisplays with a
* static scale variable to shrink pacman off the screen. When
* this function is done drawing the death sequence, it resets
* the player positions (pacman & ghosts) and registers the
* next display function (gameDisplay if the player has
* remaining lives or gameOverDisplay if the player has run out
* of remaining lives). Continually redisplays.
*/
void killPacmanDisplay() {
static GLfloat scale = 1.0;
static int count = 0;
/* Stop movement */
pacman.direction = -1;
pacman.nextDirection = -1;
gameStarted = 0;
glutSpecialFunc(NULL);
/* If tilt is enabled, return angle to starting position */
if (tiltBoard) {
theta[0] = -6.0;
theta[1] = 0.0;
theta[2] = 0.0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
/* Draw pacman */
glPushMatrix();
glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), pacman.size / 2.0);
glColor3fv(pacman.colorf);
glScalef(scale, scale, scale);
drawPacman();
glPopMatrix();
glFlush();
glutSwapBuffers();
if (count == 150) {
/* Reset count, scale */
count = 0;
scale = 1.0;
/* No more players? */
if (playersLeft == 0) {
glutDisplayFunc(gameOverDisplay);
glutKeyboardFunc(NULL);
} else {
/* Reposition players */
playerSetup(1);
/* Disable point flashing */
flashPoints = 0;
/* Register callbacks */
glutSpecialFunc(specialKeyInput);
glutDisplayFunc(gameDisplay);
flashReady = 5;
glutTimerFunc(500, disableFlashReady, 1);
}
} else {
if (scale > 0)
scale -= 0.012;
count++;
}
glutPostRedisplay();
}
/* Function: levelUpDisplay
* Parameters: none
* Display function used when the player is has completed a level.
* Draws the board and pacman at the current position, and uses a
* static rotation and scale varibale to spin the board around the
* origin and scale it down off the screen, while scaling pacman
* to the inverse of the scale variable to make him fly upward and
* through the screen. Continuously redisplays.
*/
void levelUpDisplay() {
static GLfloat scale = 1.0;
static int angle = 0;
static int count = 0;
/* Stop movement */
gameStarted = 0;
glutSpecialFunc(NULL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
/* Draw pacman */
glPushMatrix();
glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), pacman.size / 2.0);
glScalef(1/scale, 1/scale, 1/scale);
glColor3fv(pacman.colorf);
drawPacman();
glPopMatrix();
/* Draw board */
glPushMatrix();
glRotatef(angle, 0.0, 0.0, -1.0);
glScalef(scale, scale, scale);
glCallList(walls);
glPopMatrix();
angle = (angle + 10) % 360;
scale -= 0.025;
count++;
glFlush();
glutSwapBuffers();
if (count == 40) {
/* Reset count, angle and scale */
count = 0;
scale = 1.0;
angle = 0;
/* Load next level */
level = (level + 1) % numLevels;
loadLevel(levels[level]);
/* Speed up players */
ghostDefaultSpeed -= 5;
pacmanDefaultSpeed -= 3;
/* Reduce power-up time */
if (powerupTime > 3)
powerupTime--;
/* Register callbacks */
glutDisplayFunc(gameDisplay);
glutSpecialFunc(specialKeyInput);
flashReady = 5;
glutTimerFunc(500, disableFlashReady, 0);
}
glutPostRedisplay();
}
/* Function: titleScreenDisplay
* Parameters: none
* Display function used while the player is at the title
* screen. Sets up right click menu with "Play", "Instructions"
* and "Quit" options. Continuously redisplays.
*/
void titleScreenDisplay() {
static int angle = 0;
int digits = 1;
char scoreStr[6];
GLUquadricObj *sphere = gluNewQuadric();
/* Build Menu */
glutCreateMenu(menu);
glutAddMenuEntry("Play!", 1);
glutAddMenuEntry("Instructions", 2);
glutAddMenuEntry("Quit", 99);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Position camera */
gluLookAt(camera[0], camera[1], camera[2],
boardWidth / 2.0, boardWidth / 2.0, 1.0,
0.0, 1.0, 0.0);
/* Turn off lighting, print text */
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_LIGHTING);
displayText(9.5, 32.0, 0.0, "TOP SCORE:");
displayText(11, 30.5, 0.0, itoa(topScore, scoreStr, 10));
displayText(10.5, 27.0, 0.0, "PacmanGL");
displayText(7.0, 1.0, 0.0, "Right Click For Menu");
glEnable(GL_LIGHTING);
/* Draw big pacman */
glPushMatrix();
glTranslatef((GLfloat)boardWidth / 2.0, (GLfloat)boardHeight / 2.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glColor3fv(pacman.colorf);
gluSphere(sphere, 10.0, 24, 24);
glPushMatrix();
glTranslatef(-3.0, 1.5, 9.5);
glColor3f(0.0, 0.0, 0.0);
gluSphere(sphere, 1.0, 12, 12);
glTranslatef(6.0, 0.0, 0.0);
gluSphere(sphere, 1.0, 12, 12);
glPopMatrix();
glPopMatrix();
angle = (angle + 2) % 360; /* Rotation angle */
gluDeleteQuadric(sphere);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
/* Function: drawPacman
* Parameters: none
* Draws pacman player on the screen at the current position.
* Therefore, the co-ordinate system must be translated to the
* correct position and scaled if required before drawPacman
* is called.
*/
void drawPacman() {
GLUquadricObj *sphere = gluNewQuadric();
/* rotate pacman to face moving direction */
switch (pacman.direction) {
case left:
glRotatef(-90, 0.0, 0.0, 1.0);
break;
case right:
glRotatef(90, 0.0, 0.0, 1.0);
break;
case up:
glRotatef(180, 0.0, 0.0, 1.0);
break;
}
/* body */
glColor3fv(pacman.colorf);
gluSphere(sphere, (pacman.size / 2.0) - 0.1, 10, 10);
/* left eye */
glPushMatrix();
glTranslatef(-0.2, -0.1, (pacman.size / 2.0) - 0.05);
glColor3f(0.0, 0.0, 0.0);
gluSphere(sphere, 0.1, 8, 8);
glPopMatrix();
/* right eye */
glPushMatrix();
glTranslatef(0.2, -0.1, (pacman.size / 2.0) - 0.05);
gluSphere(sphere, 0.1, 8, 8);
glPopMatrix();
/* sunglasses bar */
glPushMatrix();
glTranslatef(-0.2, -0.05, (pacman.size / 2.0) - 0.05);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.4, 0.0, 0.0);
glEnd();
glPopMatrix();
/* mouth */
glPushMatrix();
glTranslatef(-0.2, -0.3, 0.2);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.4, 0.0, 0.0);
glEnd();
glPopMatrix();
gluDeleteQuadric(sphere);
}
/* Function: drawGhost
* Parameters: int i
* Draws the ghost specified by i on the screen at the current
* position. Therefore, the co-ordinate system must be translated
* to the correct position and scaled if required before drawGhost
* is called.
*/
void drawGhost(int i) {
GLUquadricObj *sphere = gluNewQuadric();
if (ghosts[i].state != dead) {
/* rotate ghost to face moving direction */
switch (ghosts[i].direction) {
case left:
glRotatef(-90, 0.0, 0.0, 1.0);
break;
case right:
glRotatef(90, 0.0, 0.0, 1.0);
break;
case up:
glRotatef(180, 0.0, 0.0, 1.0);
break;
}
/* body */
glColor3fv(ghosts[i].colorf);
gluCylinder(sphere, (ghosts[i].size / 2.0) - 0.1, (ghosts[i].size / 2.0) - 0.1, (ghosts[i].size / 2.0), 10, 10);
/* head */
glPushMatrix();
glTranslatef(0.0, 0.0, (ghosts[i].size) / 2.0);
gluSphere(sphere, (ghosts[i].size / 2.0) - 0.1, 10, 10);
}
/* Left eye */
glPushMatrix();
glTranslatef(-0.15, -0.2, -0.1);
glColor3f(1.0, 1.0, 1.0);
gluSphere(sphere, 0.2, 8, 8);
glPopMatrix();
/* Pupil */
glPushMatrix();
glTranslatef(-0.20, -0.3, -0.1);
glColor3f(0.0, 0.0, 0.0);
gluSphere(sphere, 0.1, 5, 5);
glPopMatrix();
/* Right eye */
glPushMatrix();
glTranslatef(0.15, -0.2, -0.1);
glColor3f(1.0, 1.0, 1.0);
gluSphere(sphere, 0.2, 8, 8);
glPopMatrix();
/* Pupil */
glPushMatrix();
glTranslatef(0.20, -0.3, -0.1);
glColor3f(0.0, 0.0, 0.0);
gluSphere(sphere, 0.1, 5, 5);
glPopMatrix();
/* If the ghost is alive, we pushed the matrix before
drawing the head, so we have to pop it now */
if (ghosts[i].state != dead)
glPopMatrix();
gluDeleteQuadric(sphere);
}
/* Function: reshapeWindow
* Parameters: int w, int h
* Callback function for when the window is resized
*/
void reshapeWindow(int w, int h) {
/* Prevent problems when a window is too short */
/* Windows cannot be resized to 0 width */
if (h == 0)
h = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w > h)
gluPerspective(45, w / h, 3, -3);
else
gluPerspective(45, h / w, 3, -3);
glMatrixMode(GL_MODELVIEW);
}
/* Function: loadLevel
* Parameters: char* filename
* Loads the level specified by filename into the board array,
* loads the ghost shortest path home board into the ghostSPHome
* array, sets up initial player positions and generates a display
* list to draw the maze walls */
void loadLevel(char* filename) {
int i, j, x, y;
int wallColorv[3];
int readGhostboard = 0;
char lineChar = '0';
FILE* handle;
/* Reset some global variables */
boardHeight = 0;
boardWidth = 0;
pellets = 0;
superPellets = 0;
handle = fopen(filename, "r");
if (handle == NULL) {
printf("Could not open board file: %s\n", filename);
exit(1);
}
/* Read level wall colours */
fscanf(handle, "%d", &(wallColorv[0]));
fscanf(handle, "%d", &(wallColorv[1]));
fscanf(handle, "%d", &(wallColorv[2]));
fscanf(handle, "%c", &lineChar);
/* Scan file to determine board width and height */
while (!feof(handle)) {
fscanf(handle, "%c", &lineChar);
/* If end of board layout */
if (lineChar == 'Z')
break;
while(lineChar != '\n' && !feof(handle)) {
if (boardHeight == 0) boardWidth++;
fscanf(handle, "%c", &lineChar);
}
boardHeight++;
}
rewind(handle);
/* Read level wall colours again */
fscanf(handle, "%d", &(wallColorv[0]));
fscanf(handle, "%d", &(wallColorv[1]));
fscanf(handle, "%d", &(wallColorv[2]));
fscanf(handle, "%c", &lineChar);
/* Read level into array, set pacman/ghost starting positions */
for (j = 0; j < boardHeight; j++) {
for (i = 0; i < boardWidth; i++) {
fscanf(handle, "%c", &lineChar);
if (lineChar == 'S') {
pacman.startY = boardHeight - j;
pacman.startX = i;
}
if (lineChar == 'g') {
ghosts[0].startY = boardHeight - j + 2;
ghosts[0].startX = i + 1;
ghosts[1].startY = boardHeight - j;
ghosts[1].startX = i;
ghosts[2].startY = boardHeight - j;
ghosts[2].startX = i + 1;
ghosts[3].startY = boardHeight - j;
ghosts[3].startX = i + 2;
}
if (lineChar == '.')
pellets++;
if (lineChar == 'P')
superPellets++;
board[j][i] = lineChar;
}
fscanf(handle, "%c", &lineChar); /* read EOL (\n) char */
}
fscanf(handle, "%c", &lineChar); /* read Z char */
fscanf(handle, "%c", &lineChar); /* read EOL (\n) char */
/* Read ghost shortest path home into array */
for (j = 0; j < boardHeight; j++) {
for (i = 0; i < boardWidth; i++) {
fscanf(handle, "%c", &lineChar);
ghostSPHome[j][i] = lineChar;
}
fscanf(handle, "%c", &lineChar); /* read EOF (\n) char */
}
fclose(handle);
/* Setup ghosts/pacman attributes */
playerSetup(1);
/* Generate display list for the walls */
walls = glGenLists(1);
glNewList(walls, GL_COMPILE);
for (y = 0; y < boardHeight; y++) {
for (x = 0; x < boardWidth; x++) {
if (board[y][x] == '#') {
glBegin(GL_QUADS);
glColor3f(wallColorv[0] / 255.0, wallColorv[1] / 255.0, wallColorv[2] / 255.0);
/* top */
glNormal3i(0, 0, 1);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y, wallHeight);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y, wallHeight);
glVertex3f((GLfloat)x + 1.0, boardHeight - ((GLfloat)y + 1.0), wallHeight);
glVertex3f((GLfloat)x, boardHeight - ((GLfloat)y + 1.0), wallHeight);
/* left side */
glNormal3i(-1, 0, 0);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y, 0.0);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y, wallHeight);
glVertex3f((GLfloat)x, boardHeight - ((GLfloat)y + 1.0), wallHeight);
glVertex3f((GLfloat)x, boardHeight - ((GLfloat)y + 1.0), 0.0);
/* right side */
glNormal3i(0, 1, 0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y, 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y, wallHeight);
glVertex3f((GLfloat)x + 1.0, boardHeight - ((GLfloat)y + 1.0), wallHeight);
glVertex3f((GLfloat)x + 1.0, boardHeight - ((GLfloat)y + 1.0), 0.0);
/* front */
glNormal3i(0, -1, 0);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y, 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y, 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y, wallHeight);
glVertex3f((GLfloat)x, boardHeight - (GLfloat)y, wallHeight);
/* back */
glNormal3i(0, 1, 0);
glVertex3f((GLfloat)x, boardHeight - ((GLfloat)y + 1.0), 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - ((GLfloat)y + 1.0), 0.0);
glVertex3f((GLfloat)x + 1.0, boardHeight - ((GLfloat)y + 1.0), wallHeight);
glVertex3f((GLfloat)x, boardHeight - ((GLfloat)y + 1.0), wallHeight);
glEnd();
}
}
}
glEndList();
}
/* Function: initialize
* Parameters: none
* Reads list of levels into levels array, loads the first level,
* sets up some OpenGL parameters */
void initialize() {
FILE* handle;
/* Seed random number generator */
srand( (unsigned)time( NULL ) );
/* Read level list */
handle = fopen("levels.dat", "r");
if (handle == NULL) {
printf("Could not open board data file: levels.dat\n");
exit(1);
}
while (!feof(handle)) {
fscanf(handle, "%s", &levels[numLevels++]);
if (numLevels == 32) break; /* only load 32 levels */
}
fclose(handle);
/* Load 1st level */
loadLevel(levels[level]);
/* Setup OpenGL */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, windowWidth / windowHeight, 3, -3);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glPointSize(2.0);
}
/* Function: keyInput
* Parameters: unsigned char key, int x, int y
* Keyboard callback function to handle key presses
*/
void keyInput(unsigned char key, int x, int y) {
switch (key) {
case 'x':
camera[0] += 0.5;
break;
case 'X':
camera[0] -= 0.5;
break;
case 'y':
camera[1] += 0.5;
break;