forked from ziw/Javascript-Pacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
939 lines (821 loc) · 23.1 KB
/
Game.js
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
//////////////////////////////////////////////////////
// Group members: Zi Wang (ziw), Bingying Xia(bxia) //
//////////////////////////////////////////////////////
var canvasID = "myCanvas";
var CANVAS_WIDTH = 510;
var CANVAS_HEIGHT = 510;
var canvas = document.getElementById(canvasID);
var ctx = canvas.getContext("2d");
// game grid
var GRID_WIDTH = 30;
var GRID_HEIGHT = 30;
var WALL_WIDTH = 3;
var numRows = CANVAS_WIDTH/GRID_HEIGHT;
var numCols = CANVAS_HEIGHT/GRID_WIDTH;
// colors for UI & Pacman
var BG_COLOR = "black";
var BORDER_COLOR = "blue";
var BEAN_COLOR = "white";
var PACMAN_COLOR = "yellow";
// colors for ghost
var RED = "red";
var PINK = "#ff9cce";
var CYAN = "#00ffde";
var ORANGE = "#ffb847";
var WEAK_COLOR = "#0031ff";
var BLINKING_COLOR = "white";
// size of sprites
var NORMAL_BEAN_RADIUS = 2;
var POWER_BEAN_RADIUS = 5;
var PACMAN_RADIUS = 9;
var GHOST_RADIUS = 9;
// directions
var UP = 1;
var DOWN = 2;
var LEFT = 3;
var RIGHT = 4;
// game parameters
var intervalId;
var restartTimer = 0;
var timerDelay = 80;
var speed = 5;
var score = 0;
var lives = [];
var MAX_LIFE = 3;
var life = MAX_LIFE;
var weakBonus = 200;
var MAX_BEANS = 136;
var beansLeft = MAX_BEANS;
var weakCounter;
var WEAK_DURATION = 10000/timerDelay;
//bean cases
var NORMAL_BEAN = 1
var POWER_BEAN = 2;
//spirtes instances
var welcomePacman;
var welcomeBlinky;
var welcomeInky;
var mrPacman;
var blinky;
var inky;
var pinky;
var clyde;
var ghosts;
//game state and map
var gameOn = false;
var gamePaused = false;
var maze = new Array(CANVAS_HEIGHT/GRID_HEIGHT);
var mazeContent = [
//row1
[LEFT_TOP, TOP_BOTTOM, TOP_BOTTOM, TOP_ONLY, TOP_BOTTOM,
TOP_BOTTOM, TOP_BOTTOM, RIGHT_TOP, LEFT_TOP, TOP_ONLY,
TOP_ONLY, TOP_ONLY, TOP_ONLY, TOP_ONLY, TOP_ONLY,
TOP_ONLY, RIGHT_TOP],
//row2
[LEFT_RIGHT, BOTTOM_LEFT_TOP, RIGHT_TOP, LEFT_RIGHT, LEFT_TOP,
TO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_BOTTOM,
TOP_BOTTOM, TOP_BOTTOM, TOP_BOTTOM, BOTTOM_ONLY, TOP_BOTTOM,
TOP_BOTTOM, TOP_BOTTOM, RIGHT_BOTTOM, RIGHT_BOTTOM_LEFT, LEFT_BOTTOM,
TOP_BOTTOM, RIGHT_BOTTOM]
];
// grids that don't redraw
var staticGrids = [];
var staticGridsIndex = 0;
// start location of pacman
var pacmanStartLoc = [4,9];
// grids with no beans
var noBean = [pacmanStartLoc,[5,12],[5,13],[5,3],[9,5],[9,6],[1,1],[5,1],[3,0],[2,4],[4,6],[5,6],[5,5],[12,7],[14,5],[12,11],[14,11]];
var noBeanIndex=noBean.length;
// power beans in maze
var powerBeans = [[0,0], [2,13], [16,4], [16,16], [2,5], [14,10]];
// ghost house
var ghostHouse = [];
var ghostHouseIndex = 0;
/*======================END GLOBAL VARs====================*/
/*====================Initialization Methods==============*/
function initCanvas(width, height){
if(width===undefined || !(width instanceof Number)){
width = CANVAS_WIDTH;
}
if(height===undefined || !(height instanceof Number)){
height = CANVAS_HEIGHT;
}
ctx.fillStyle = "black";
ctx.fillRect(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);
}
// draw maze, print instruction on lower-left corner, show lives on top-right corner
function initMaze(){
for(var i=0; i<maze.length; i++){
var oneRow = new Array(CANVAS_WIDTH/GRID_WIDTH);
maze[i] = oneRow;
}
// draw maze with full beans
for( var row = 0; row < CANVAS_HEIGHT/GRID_HEIGHT; row++){
for(var col = 0; col < CANVAS_WIDTH/GRID_WIDTH; col++){
var beanType = NORMAL_BEAN;
var newGrid = new Grid(col*GRID_WIDTH,row*GRID_HEIGHT , mazeContent[row][col],beanType);
maze[row][col] = newGrid;
newGrid.draw();
}
}
//overwrite beans that shouldn't ecist
for(var i=0; i<noBean.length; i++){
var x = noBean[i][0];
var y = noBean[i][1];
maze[x][y].beanType = undefined;
maze[x][y].draw();
}
// draw power beans
for(var i=0; i<powerBeans.length;i++){
var x = powerBeans[i][0];
var y = powerBeans[i][1];
maze[x][y].beanType = POWER_BEAN;
maze[x][y].draw();
}
}
function initFields () {
// body...
for (var i=6; i<10; i++){
ghostHouse[ghostHouseIndex]=[i,9];
ghostHouseIndex++;
}
//fill up staticGrids[]
for (var i=0; i<2; i++){
for (var j=8; j<17; j++){
staticGrids[staticGridsIndex]=[i,j];
staticGridsIndex++;
}
}
for (var i=9; i<17; i++){
for (var j=0; j<4; j++){
staticGrids[staticGridsIndex]=[i,j];
staticGridsIndex++;
}
}
for (var i=2; i<6; i++){
for (var j=14; j<17; j++){
staticGrids[staticGridsIndex]=[i,j];
staticGridsIndex++;
}
}
//fill up noBean[]
for(var i=0; i<2; i++){
for(var j=8; j<17; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for(var i=2; i<6; i++){
for(var j=14; j<17; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for(var i=9; i<17; i++){
for(var j=0; j<4; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for (var i=1; i<6; i++){
noBean[noBeanIndex]=[i,2];
noBeanIndex++;
}
for(var i=1; i<4; i+=2){
for(var j=4; j<7; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for (var j=8; j<13; j++){
noBean[noBeanIndex]=[3,j];
noBeanIndex++;
}
for (var j=1; j<7; j++){
noBean[noBeanIndex]=[7,j];
noBeanIndex++;
}
for (var i=5; i<10; i++){
for(var j=8; j<11; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for (var j=12; j<16; j++){
noBean[noBeanIndex]=[7,j];
noBeanIndex++;
}
for (var j=12; j<16; j++){
noBean[noBeanIndex]=[9,j];
noBeanIndex++;
}
for(var i=11; i<16; i+=2){
for(var j=5; j<8; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for(var i=11; i<16; i+=2){
for(var j=9; j<12; j++){
noBean[noBeanIndex]=[i,j];
noBeanIndex++;
}
}
for(var j=13; j<16; j++){
noBean[noBeanIndex]=[11, j];
noBeanIndex++;
}
for(var i=12; i<16; i++){
noBean[noBeanIndex]=[i, 15];
noBeanIndex++;
}
for(var i=13; i<17; i++){
noBean[noBeanIndex]=[i, 13];
noBeanIndex++;
}
}
/*================END Initialization Methods==============*/
/*====================Util Methods================*/
//draw a circle
function circle(ctx, cx, cy, radius) {
ctx.beginPath();
ctx.arc(cx, cy, radius, 0, 2*Math.PI, true);
ctx.fill();
}
//get opposite direction
function oppositeDir (dir) {
switch(dir){
case UP:
return DOWN;
break;
case DOWN:
return UP;
break;
case LEFT:
return RIGHT;
break;
case RIGHT:
return LEFT;
break;
default:
return -1;//err
}
}
function getRowIndex (yCord) {
if(yCord === undefined){
return -1;//err
}
return parseInt(yCord/GRID_HEIGHT);
}
function getColIndex (xCord) {
if(xCord === undefined){
return -1;//err
}
return parseInt(xCord/GRID_WIDTH);
}
function sleep(ms)
{
var dt = new Date();
dt.setTime(dt.getTime() + ms);
while (new Date().getTime() < dt.getTime());
}
function fixGrids (x, y) {
var row = getRowIndex(y);
var col = getColIndex(x);
if(xOnGridCenter(y)){
maze[row][col].draw();
if(col+1 < maze.length && !staticArrayContains([row, col+1])){
maze[row][col+1].draw();
}
if(col-1 >= 0 && !staticArrayContains([row, col-1])){
maze[row][col-1].draw();
}
}
else if(yOnGridCenter(x)){
maze[row][col].draw();
if(row+1 < maze.length && !staticArrayContains([row+1, col])){
maze[row+1][col].draw();
}
if(row-1 >=0 && !staticArrayContains([row-1,col]) ){
maze[row-1][col].draw();
}
}
}
function staticArrayContains(cord) {
var x = cord[0];
var y = cord[1];
for(var i=0; i< staticGrids.length; i++ ){
if(x=== staticGrids[i][0] &&
y=== staticGrids[i][1]){
return true;
}
}
return false;
}
function ghostHouseContains(cord) {
var x = cord[0];
var y = cord[1];
for(var i=0; i< ghostHouse.length; i++ ){
if(x=== ghostHouse[i][0] &&
y=== ghostHouse[i][1]){
return true;
}
}
return false;
}
function onGridCenter (x,y) {
return xOnGridCenter(y) && yOnGridCenter(x);
}
function xOnGridCenter (y) {
return ((y - GRID_WIDTH/2) % GRID_WIDTH) === 0;
}
function yOnGridCenter (x) {
return ((x - GRID_HEIGHT/2) % GRID_HEIGHT) === 0;
}
//see if sprite can move one more step at the given (x,y) facing the given direction
function canMove (x,y,dir) {
if(!onGridCenter(x,y)){
return true;
}
var canMove = false;
var currGrid = maze[getRowIndex(y)][getColIndex(x)];
var gridType = currGrid.gridType;
switch(dir){
case UP:
if(gridType != LEFT_TOP && gridType != RIGHT_TOP && gridType != TOP_BOTTOM
&& gridType != TOP_ONLY && gridType!= LEFT_TOP_RIGHT
&& gridType != TOP_RIGHT_BOTTOM && gridType!= BOTTOM_LEFT_TOP){
canMove = true;
}
break;
case DOWN:
if(gridType != LEFT_BOTTOM && gridType != TOP_BOTTOM && gridType != RIGHT_BOTTOM
&& gridType != BOTTOM_ONLY && gridType!= RIGHT_BOTTOM_LEFT
&& gridType != BOTTOM_LEFT_TOP && gridType!= TOP_RIGHT_BOTTOM){
canMove = true;
}
break;
case LEFT:
if(gridType != LEFT_BOTTOM && gridType != LEFT_TOP && gridType != LEFT_ONLY
&& gridType != LEFT_RIGHT && gridType!= LEFT_TOP_RIGHT
&& gridType != BOTTOM_LEFT_TOP && gridType!= RIGHT_BOTTOM_LEFT){
canMove = true;
}
break;
case RIGHT:
if(gridType != RIGHT_BOTTOM && gridType != RIGHT_TOP && gridType != RIGHT_ONLY
&& gridType != LEFT_RIGHT && gridType!= RIGHT_BOTTOM_LEFT
&& gridType != TOP_RIGHT_BOTTOM && gridType != LEFT_TOP_RIGHT){
canMove = true;
}
break;
default:
break;
}
return canMove;
}
/*=================END Util Methods================*/
/*=================UI Update Methods===============*/
// draw instructions
function printInstruction () {
ctx.fillStyle = "white";
ctx.font="12px monospace";
ctx.textAlign = "left";
var txt = "WELCOME TO \nPACMAN 15-237!\n\n\nArrow keys or\nWASD to move\n\nQ to pause\nE to resume\nR to restart";
var x = 12;
var y = CANVAS_HEIGHT-200;
var lineheight = 15;
var lines = txt.split('\n');
for (var i = 0; i<lines.length; i++)
ctx.fillText(lines[i], x, y + (i*lineheight) );
if (ghosts.length === 0){
ctx.fillStyle = "black";
ctx.fillRect(x, CANVAS_WIDTH-40, 70, 30);
ctx.fillStyle = "red";
ctx.font = "16px monospace";
ctx.textAlign = "left";
ctx.fillText("GOD MODE", x, CANVAS_WIDTH-20);
}
}
//draw lives on top-right corner
function showLives(){
ctx.fillStyle="black";
ctx.fillRect(CANVAS_WIDTH-80, 10, 70, 30);
for(var i=0; i<life-1; i++){
lives[i] = new Pacman(CANVAS_WIDTH-50+25*i, 30, RIGHT);
lives[i].draw();
}
}
//show welcome screen
function welcomeScreen(){
gameOn = false;
gamePaused = false;
// welcome text
ctx.fillStyle = "white";
ctx.font = "80px monospace";
ctx.textAlign = "center";
ctx.fillText("PACMAN", CANVAS_WIDTH/2, 170);
ctx.font = "20px monospace";
ctx.fillText("Press s to start", CANVAS_WIDTH/2, 220);
ctx.font = "14px monospace";
ctx.fillText("DEVELOPED BY: ZI WANG, BINGYING XIA", CANVAS_WIDTH/2 , CANVAS_HEIGHT/20*19);
welcomePacman = new Pacman(CANVAS_WIDTH/5, CANVAS_HEIGHT/3*2, RIGHT);
welcomePacman.radius = 30;
welcomePacman.draw();
welcomeBlinky = new Ghost(CANVAS_WIDTH/5*3.3, CANVAS_HEIGHT/3*2, RED, LEFT);
welcomeBlinky.radius = 30;
welcomeBlinky.draw();
welcomeInky = new Ghost(CANVAS_WIDTH/5*4, CANVAS_HEIGHT/3*2, CYAN, RIGHT);
welcomeInky.radius = 30;
welcomeInky.draw();
intervalId = setInterval(updateWelcomeScreen, timerDelay*2);
}
//welcome screen animation
function updateWelcomeScreen () {
ctx.fillStyle = "black";
ctx.fillRect(0, CANVAS_HEIGHT/2, CANVAS_WIDTH,140);
welcomePacman.mouthOpen = !welcomePacman.mouthOpen;
welcomeBlinky.isMoving = !welcomeBlinky.isMoving;
welcomeInky.isMoving = !welcomeInky.isMoving;
welcomePacman.draw();
welcomeInky.draw();
welcomeBlinky.draw();
}
//show || update score
function showScore(){
ctx.fillStyle="black";
ctx.fillRect(CANVAS_WIDTH-250, 10, 190, 40);
ctx.fillStyle = "white";
ctx.font = "24px monospace";
ctx.textAlign = "left";
ctx.fillText("score: " + parseInt(score), CANVAS_WIDTH-250, 37);
}
//show win message
function winMessage(){
//draw popup
ctx.fillStyle = "black";
ctx.strokeStyle = "green";
ctx.lineWidth=5;
ctx.fillRect(CANVAS_WIDTH/2-150, CANVAS_HEIGHT/2-40, 300, 100);
ctx.strokeRect(CANVAS_WIDTH/2-150, CANVAS_HEIGHT/2-40, 300, 100);
//write message
ctx.textAlign="center";
ctx.fillStyle = "white";
ctx.font = "16px monospace";
ctx.fillText("Congratulations, you won!", CANVAS_HEIGHT/2, CANVAS_HEIGHT/2+6);
ctx.font = "12px monospace";
ctx.fillText("press R to play again", CANVAS_HEIGHT/2, CANVAS_HEIGHT/2+28);
}
//show lose message
function loseMessage(){
//draw popup
ctx.fillStyle = "black";
ctx.strokeStyle = "red";
ctx.lineWidth=5;
ctx.fillRect(CANVAS_WIDTH/2-100, CANVAS_HEIGHT/2-40, 200, 100);
ctx.strokeRect(CANVAS_WIDTH/2-100, CANVAS_HEIGHT/2-40, 200, 100);
//write message
ctx.textAlign="center";
ctx.fillStyle = "red";
ctx.font = "26px monospace";
ctx.fillText("GAME OVER", CANVAS_HEIGHT/2, CANVAS_HEIGHT/2+7);
ctx.font = "12px monospace";
ctx.fillText("press R to play again", CANVAS_HEIGHT/2, CANVAS_HEIGHT/2+28);
}
//update canvas for each frame.
function updateCanvas() {
restartTimer++;
if (gameOver()===true){
life--;
// mrPacman.dieAnimation();
showLives();
if (life>0){
sleep(500);
clearInterval(intervalId);
fixGrids(mrPacman.x, mrPacman.y);
for(var i=0; i<ghosts.length; i++){
fixGrids(ghosts[i].x, ghosts[i].y);
}
run();
}
else {
clearInterval(intervalId);
sleep(500);
loseMessage();
}
}
else if (pacmanWon()===true){
clearInterval(intervalId);
sleep(500);
winMessage();
}
else{
if(weakCounter>0 && weakCounter<2000/timerDelay){
for(var i=0; i<ghosts.length; i++){
ghosts[i].isBlinking = !ghosts[i].isBlinking;
}
}
if(weakCounter>0){
weakCounter--;
}
if(weakCounter===0){
for(var i=0; i<ghosts.length; i++){
ghosts[i].isDead = false;
ghosts[i].isWeak = false;
ghosts[i.isBlinking = false];
weakBonus= 200;
}
}
eatBean();
eatGhost();
mrPacman.move();
for(var i=0; i<ghosts.length; i++){
if(ghosts[i].isDead === false){
ghosts[i].move();
}
}
fixGrids(mrPacman.x, mrPacman.y);
for(var i=0; i<ghosts.length; i++){
fixGrids(ghosts[i].x, ghosts[i].y);
}
mrPacman.draw();
for(var i=0; i<ghosts.length; i++){
ghosts[i].draw();
}
}
}
//try to eat a bean
function eatBean () {
if(onGridCenter(mrPacman.x, mrPacman.y)){
if(maze[mrPacman.getRow()][mrPacman.getCol()].beanType===NORMAL_BEAN){
score+= parseInt(10);
showScore();
beansLeft--;
}
else if (maze[mrPacman.getRow()][mrPacman.getCol()].beanType===POWER_BEAN){
score+=parseInt(50);
showScore();
beansLeft--;
//ghosts enter weak mode
for(var i=0; i<ghosts.length; i++){
ghosts[i].isWeak=true;
}
weakCounter=WEAK_DURATION;
}
maze[mrPacman.getRow()][mrPacman.getCol()].beanType=undefined;
maze[mrPacman.getRow()][mrPacman.getCol()].draw();
}
}
//try to eat a weak ghost
function eatGhost () {
for(var i=0; i<ghosts.length; i++){
if(Math.abs(mrPacman.x-ghosts[i].x)<=5 && Math.abs(mrPacman.y-ghosts[i].y)<=5
&& ghosts[i].isWeak && !ghosts[i].isDead){
score += parseInt( weakBonus);
weakBonus *=2;
showScore();
ghosts[i].isDead = true;
ghosts[i].toGhostHouse();
}
}
}
function gameOver(){
for(var i=0; i<ghosts.length; i++){
if(Math.abs(mrPacman.x-ghosts[i].x)<=5 && Math.abs(mrPacman.y-ghosts[i].y)<=5
&& !ghosts[i].isWeak){
return true;
}
}
return false;
}
function pacmanWon(){
return beansLeft === 0;
}
//Show a count down each time the game starts
function countDown () {
ctx.fillStyle = "black";
ctx.fillRect(CANVAS_HEIGHT-85, 70, 80,80);
ctx.fillStyle = "red";
ctx.font = "50px monospace";
ctx.textAlign = "center";
ctx.fillText("3",CANVAS_HEIGHT-43, 130);
setTimeout(function () {
ctx.fillStyle = "black";
ctx.fillRect(CANVAS_HEIGHT-85, 70, 80,80);
ctx.fillStyle = "orange";
ctx.fillText("2",CANVAS_HEIGHT-43, 130);
setTimeout(function () {
ctx.fillStyle = "black";
ctx.fillRect(CANVAS_HEIGHT-85, 70, 80,80);
ctx.fillStyle = "yellow";
ctx.fillText("1",CANVAS_HEIGHT-43, 130);
setTimeout(function () {
ctx.fillStyle = "black";
ctx.fillRect(CANVAS_HEIGHT-85, 70, 80,80);
ctx.fillStyle = "green";
ctx.textAlign = "center";
ctx.fillText("GO",CANVAS_HEIGHT-43, 130);
setTimeout(function () {
intervalId = setInterval(updateCanvas, timerDelay);
},500);
}, 1000);
}, 1000);
}, 1000);
}
/*==================END UI Update Methods================*/
/*==================Game Control Methods===================*/
//listen to keyDown event
function onKeyDown (event) {
var keycode = event.keyCode;
var pauseCode = 81; //q to pause
var continueCode = 69; //e to resume
var restartCode = 82; //r to restart
var godModeCode = 71; //g to enter god mode
// wasd
var wCode = 87;
var aCode = 65;
var sCode = 83;
var dCode = 68;
//arrow keys
var leftCode = 37;
var upCode = 38;
var rightCode = 39;
var downCode = 40;
//start game
if(!gameOn){
if(keycode === sCode){
clearInterval(intervalId);
gameOn = true;
gamePaused = false;
initMaze();
run();
return;
}
else if(keycode === godModeCode){
clearInterval(intervalId);
ghosts = [];
gameOn = true;
gamePaused = false;
initMaze();
run(true);
return;
}
}
else{
//pause game
if(keycode === pauseCode && !gamePaused){
clearInterval(intervalId);
gamePaused = true;
return;
}
//resume game
if(keycode === continueCode && gamePaused){
intervalId = setInterval(updateCanvas, timerDelay);
gamePaused = false;
return;
}
//restart game
if( keycode === restartCode && restartTimer > 0) {
//can't restart game if a game was just refreshed.
restartTimer = 0;
clearInterval(intervalId);
gameOn = true;
gamePaused = false;
score = 0;
life = MAX_LIFE;
beansLeft = MAX_BEANS;
initMaze();
run();
}
//4-way controls
switch(keycode){
case upCode:
case wCode:
mrPacman.nextDir = mrPacman.dir===UP ? undefined: UP;
break;
case rightCode:
case dCode:
mrPacman.nextDir = mrPacman.dir===RIGHT? undefined : RIGHT;
break;
case leftCode:
case aCode:
mrPacman.nextDir = mrPacman.dir === LEFT? undefined : LEFT;
break;
case downCode:
case sCode:
mrPacman.nextDir = mrPacman.dir === DOWN? undefined : DOWN;
break;
default:
break;
}
}
}
//run the game. Create mrPacman and 4 ghosts. Reset their positions.
function run(isGodMode) {
showScore();
mrPacman = new Pacman(pacmanStartLoc[1]*GRID_WIDTH + GRID_WIDTH/2, pacmanStartLoc[0]*GRID_HEIGHT + GRID_HEIGHT/2, RIGHT);
if(isGodMode===undefined || !isGodMode){
blinky = new Ghost(0,0, RED, DOWN);
inky = new Ghost(0,0, CYAN, DOWN);
pinky = new Ghost(0,0, PINK, DOWN);
clyde = new Ghost(0,0, ORANGE, DOWN);
blinky.toGhostHouse();
inky.toGhostHouse();
pinky.toGhostHouse();
clyde.toGhostHouse();
ghosts = [blinky, inky, pinky, clyde];
inky.draw();
blinky.draw();
pinky.draw();
clyde.draw();
}
else{
ghosts = [];
}
showLives();
printInstruction();
mrPacman.draw();
countDown();
}
/*===============END Game Control Methods===================*/
/*-----------GAME START-----------*/
initFields();
initCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.addEventListener('keydown', onKeyDown, false);
canvas.setAttribute('tabindex','0');
canvas.focus();
welcomeScreen();