-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2042 lines (1854 loc) · 95 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Pacman</title>
</style>
</head>
<body>
<canvas id="canvasOne"></canvas>
<script type="text/javascript">
//2 canvas variables used in drawing the canvas, theses are integral to the program
var ctx;
var canvas = document.getElementById("canvasOne");
//create the map array that will be processed to draw the map on screen
var mapArray = [
[4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
[2, 0, 9, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 0, 9, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 0, 9, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 0, 2],
[2, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 2],
[2, 0, 9, 6, 6, 6, 6, 9, 0, 4, 6, 6, 9, 0, 9, 6, 6, 6, 6, 6, 3, 0, 4, 6, 6, 5, 0, 3, 6, 6, 6, 6, 6, 9, 0, 9, 6, 6, 5, 0, 9, 6, 6, 6, 6, 9, 0, 2],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0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6, 6, 8, 0, 2],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
[7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8]
];
//currentMap is used to enable me to make multiple different maps
//I simply assign it to the map I want to use now
//the rest of the code should be dynamic enough to adjust
var currentMap;
//boundYSortArr will hold the processed objects that are formed from the 2-dim map array
var boundYSortArr = [];
//this array will store the highscores of past users
var highscores = [];
//last press holds the last key pressed by the user, it needs to be global to keep it in scope
var lastPress;
//"not pressed" controls whether or not to use the last press or the current press
var notPressed = false;
//global score variable, is incremented when a user eats food
var score = 0;
//cuurGridScrVal is incremented when the grid is setup up
//1 is added to its value whenever the setup search finds a food item
//1 is taken away whenever the user eats food
//when all the food is eaten, this number will be 0, the code will then reset the map, keeping the current score
//effectively creating a new level
var currGridScrVal = 0;
//portal channel is used to stop the AI from going near the portal blocks
var portalChannel;
//opnClsBool controls the motion of pacmans mouth movement
//when 0 his mouth is open
//when 1 his mouth is closed
var opnClsBool = 0;
//strtGameCntrl is a global variable that is used to control the flow of the game between either menus and the game itself
var strtGameCntrl = true;
//objectStrtPos is an array that stores needed information for when the user loses a life
//such as the AI's starting position, and the user's starting position
var objectStrtPos = [];
//deadloop is used in the dead pacman animation, as there are 12 sprites in that animation, deadloop iterates every
//new frame, and when it hits 12, it is used to close the function and return to the game
var deadLoop = 0;
//this is an object definition that takes in 2 values and stores them in an object
//the values passed will be the x/y co-ordinates of another object
function coOrds(x, y) {
this.x = x;
this.y = y;
}
//this is a character definition that stores position, speed and the type of character
//position and speed are co-ords that store an x and y value
//type should be just a string
//currPos will be updated whenever the the algorithm finds the user's position
function pacman(position, speed, type) {
this.position = new coOrds(position.x, position.y);
this.speed = new coOrds(speed.x, speed.y);
//type is a string used to differentiate between a pacman and an enemy
this.type = type;
this.currPos = 0;
//inGame is a boolean used to see if pacman is in the game at the current time or not
this.inGame = true;
//lives keeps the number of retries (or "lives") the user has left until the user loses
this.lives = 3;
//stunned is used to control the canvas when the user dies
//without this control, the code continues to draw the game, calculating everything while also killing the game
//this looks weird and is not the effect i'm looking for
this.stunned = false;
//dead is turned on when all lives are lost
//it will trigger the highscore screen when true
this.dead = false;
//img is linked to my spriteMap
this.img = new Image();
this.img.src = "ImageMap.png";
//pacman moves when he's alive
//updating his position by way of his speed, which is calculated elsewhere
this.updatePacman = function () {
if (this.dead == false) {
this.position.x += this.speed.x;
this.position.y += this.speed.y;
}
}
//reset is called when a new game is started and also when the user loses a life
//it resets the properties needed from the start of the game
this.reset = function () {
//if reset is called and pacman is not dead, a life will be lost
if (!this.dead) {
this.lives--;
//this sets dead to true should all lives be lost
if (this.lives <= 0) {
this.dead = true;
}
//afterDeathReset is called here, which resets rest of the game
afterDeathReset(objectStrtPos[0], objectStrtPos[1], objectStrtPos[2], objectStrtPos[3]);
} else {
//if reset is called and pacman is already dead, various needed variables will be reset
this.position = new coOrds(position.x, position.y);
this.speed = new coOrds(speed.x, speed.y);
this.type = type;
this.currPos = 0;
this.lives = 3;
this.stunned = false;
this.dead = false;
}
}
//this simply draws an image of a life for every life that is left
this.drawLives = function () {
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.font = "30px Arial";
ctx.fillText("Lives: ", 400, 680);
ctx.closePath();
for (var liveCount = 0; liveCount < this.lives; liveCount++) {
ctx.drawImage(this.img, 80, 160, 20, 20, 480 + (liveCount * 20), 650, 40, 40);
}
}
}
//Enemy is used to create a new enemy object, such as a new ghost
function enemy(position, speed, type, imgY) {
//position is the x and y co-ordinates of this ghost
this.position = new coOrds(position.x, position.y);
//speed is used to control the ghost's movements
this.speed = new coOrds(speed.x, speed.y);
//startingPos is the position that the ghost will move to when it is acive but not in game
//it will be a position slightly outside of it's cage
//when it hits this position, inGame will be set to true, and the ghost will start to move to pacman instead
this.strtingPos = new coOrds(510, 210);
//type is an identifier for the type of object in use i.e. enemy or pacman
this.type = type;
//currPos is updated every move, it will store the grid position of the enemy
this.currPos = null;
//this is the sprite map image
this.img = new Image();
this.img.src = "ImageMap.png";
//scareValue triggers the ghost to turn blue and white when scared
this.scareValue = 2;
//this is the value used to change the sprite from blue to white when scared
this.scareSpd = 20;
//incrementing 20 to the x position grabs the next ghost face of that type
this.imgXPos = 20;
//these variables are used to control the sprite position
this.imgYPos = imgY;
this.storedYPos = imgY;
this.imgH = 20;
this.imgW = 20;
this.sprW = 40;
this.sprH = 40;
this.leftEyes = 100;
this.downEyes = 60;
this.rightEyes = 120;
this.upEyes = 20;
this.scatterCounter = 7;
this.scatterTurnOffNum = 0;
//this counts every turn taken by the enemy
//will be helpful in allowing the ghosts better movement
//will stop need to recalculate path every turn
this.enemyTurnCount = 0;
this.scareConst = 40;
this.scareAdapt = 0;
//when true, the AI will recalculate their direction
//this is only triggered when there is a new possible move for it, or if it is colliding with another ghost
//the AI will never be able to go back on itself unless it is in scared or scatter mode
this.recalcDirec = true;
//these vars are self explantory
//when any of the "Modes" are switched on, the ghost will have different behaviour
this.scaredMode = false;
this.deadMode = false;
this.scatterMode = false;
//these vars are used to control the ghost as it leaves the cage
//instead of randomly moving around the cage, it is given a co-ordinate to move toward
//located just outside of the cage, when it hits that position, inGame is set to true
//isActive is triggered when the user has predetermind score
this.inGame = false;
this.isActive = false;
//this resets lots of variables when called
this.reset = function () {
this.scatterCounter = 7;
this.scatterTurnOffNum = 0;
this.enemyTurnCount = 0;
this.recalcDirec = true;
this.scaredMode = false;
this.scatterMode = false;
this.inGame = false;
this.isActive = false;
this.scareValue = 2;
this.scareSpd = 20;
this.currPos = null;
this.deadMode = false;
}
//this changes the sprite depending on it's speed
//it gives the illusion that the ghost is looking in the direction it is moving toward
this.updateEyes = function () {
if (!this.deadMode) {
if (this.speed.x < 0) {
this.imgXPos = this.leftEyes;
} else if (this.speed.x > 0) {
this.imgXPos = this.rightEyes;
} else if (this.speed.y > 0) {
this.imgXPos = this.downEyes;
} else {
this.imgXPos = this.upEyes;
}
}
}
//turns scatter mode on or off depending on certain variables' values
/*
scatter mode is used to make the enemy move away from the user
it allows for a bit of breathing room at the start
and that breathing rooom shrinks the longer the user plays
for
Further down in the code I check the scattermode value
when checked if true, the enemy moves away from the user by selecting the
longest line distance from the user, rather than the shortest
*/
this.scatterModeSwitcher = function () {
//to turn scatter mode on, the enemy has to have moved 20 places, and the scatter controller cant be greater than 4
//the enemy also needs to be chasing the enemy
if (this.enemyTurnCount % 20 == 0 && this.scatterCounter > 4 && this.inGame) {
this.scatterMode = true;
//this calculates the point at which the scattermode is turned off
this.scatterTurnOffNum = score + this.scatterCounter;
//once the enemy has moved that ammount of moves, scatter mode is turned off
} else if (this.enemyTurnCount == this.scatterTurnOffNum) {
this.scatterMode = false;
}
//the scatter controller is decremented every 2nd scatterMode
//this allows for shorter scattermodes as the game progresses
//effectively making the game harder the longer the user plays
if (this.enemyTurnCount % 40 == 0) {
this.scatterCounter--;
}
}
//scared switch controls the sprite used when in scared mode
/*
Scared mode occurs when pacman eats some superfood
the ghosts instantly become terrified of pacman and try to run away from him
as effectively as they possibly can
If a ghost collides with pacman when in scared mode, the ghost's mode turns to deadMode
*/
this.scaredSwitch = function () {
//since scaredMode and deadMode are linked
//i check their values, and provided scaredMode returns true and
//deadMode returns false, the ghost's sprite is switched between blue and white
if (this.scaredMode && this.deadMode == false) {
//simple initilaiser to see if this is the first time that
//the enemy has been in the current scaredmode
if (this.enemyTurnCount + 1 == this.scareValue) {
//if true then the sprite is initilaised to 40 on the x value
//this denotes the column change needed to occur to change that sprite
this.imgXPos = 40;
}
//the imgYPos needs to be 160 always when in scared mode as that is the row with the scared faces sprites
this.imgYPos = 160;
//scareSpd will change every turn, when it changes
//scareSpd becomes the value of the blue or white white sprite
this.scareSpd = -this.scareSpd;
//scareAdapt keeps a count of the time the enemy stays in scaredMode
//it "Adapts" if the user hits another superfood while the enemy is still in scaredMode
//then if the enemy has more turns than the adapted Scare number, it will exit scaredMode and go on the attack
if (this.enemyTurnCount > this.scareAdapt) {
this.updateScaredMode(false);
this.updateGhost();
}
//if its not in scareMode, then it might be dead
//if its dead, the ghost needs to use the dead sprite
//the sprite x and y positions are thus altered
} else if (this.deadMode) {
this.scaredMode = false;
this.imgXPos = 40;
this.imgYPos = 200;
//if at this point, the enemy is neither dead nor scared, the used variables need to reset
//imgYPos is reset to the normal ghost sprite
//enemyTurnCount is reset as i do not need to keep track of the ghost's movements if its in normal mode
//and scareSpd is reset to 20 as this is the value of the scared sprite initially
} else {
this.imgYPos = this.storedYPos;
this.enemyTurnCount = 0;
this.scareSpd = 20;
}
}
//update ghost checks that the ghost has been activated, then checks for any possible modes that might be on
//moveBool controls wether the ghosts can move or not
//if it is true then the ghost will naturally move in the direction it's speed dictates
this.updateGhost = function (moveBool) {
if (this.isActive) {
this.scaredSwitch();
this.scatterModeSwitcher();
if (moveBool == true) {
this.position.x += this.speed.x;
this.position.y += this.speed.y;
}
//scareSpd as stated above, controls the scaredmode sprites
//every turn in scared mode, the ghost will switch between blue and black because of this line
if (this.scaredMode) {
this.imgXPos += this.scareSpd;
}
}
}
//updateScaredMode is called when a new superfood is eaten
//scaredMode is set to modeBool, and scareAdapt gets the value of scareConst added to it
this.updateScaredMode = function (modeBool) {
if (this.inGame&&this.deadMode==false) {
this.scaredMode = modeBool;
this.scareAdapt += this.scareConst;
}
}
}
//creating 3 character objects
//the first 2 are enemies while the third is our hero pac
var pinky = new enemy({
x: 450,
y: 270
}, {
x: 20,
y: 0
}, "enemy", 100);
var angry = new enemy({
x: 530,
y: 270
}, {
x: -20,
y: 0
}, "enemy", 80);
var pac = new pacman({
x: 750,
y: 210
}, {
x: 20,
y: 0
}, "pac");
function homeScreen() {
//homescreen is the opening screen the user sees when they open the game
//it consists of a logo, some animated text and moving ghosts
canvas.height = 700;
canvas.width = 1000;
ctx = canvas.getContext("2d");
ctx.fillRect(0, 0, canvas.width, canvas.height);
//menu ghosts are setup and initilaised, they will move across the bottom of the screen
var menuPinky = new enemy({
x: 800,
y: 450
}, {
x: -20,
y: 0
}, "enemy", 80);
var menuAngry = new enemy({
x: 200,
y: 550
}, {
x: 20,
y: 0
}, "enemy", 100);
//imageObj is the logo image shown
var imageObj = new Image();
imageObj.src = "PacLogo.png";
//when the image is loaded into memory, it is shown to the screen
imageObj.onload = function () {
ctx.drawImage(imageObj, 190, 50);
}
//the words click too start are displayed in the center of the screen
ctx.beginPath();
ctx.fillStyle = 'pink';
ctx.textAlign = 'center';
ctx.font = "30px Arial";
ctx.fillText("Click to start", 475, 380);
ctx.closePath();
var pX = menuPinky.speed.x;
var aX = menuAngry.speed.x;
var num = 1;
dotAnim(menuPinky, menuAngry, pX, aX, num);
}
//dotAnim handles the animation needed for the homeScreen
//namely that of the animated text and it also calls the ghost movement function
/*
the two "menu" parameters are ghosts, the pX and aX are their current speeds
num controls which dot on the menu to change the colour of
*/
function dotAnim(menuPinky, menuAngry, pX, aX, num) {
//due to stack issues caused by moving between menus and the game
//strtGameCntrl rejects access to this function should the user have clicked the screen
//without this, the game will continue to print the ghosts and dots briefly while they are in stack at the same time as the game
if (strtGameCntrl == true) {
//the ghosts aremoved
pX = menuEnMv(pX, menuPinky);
//the ghosts are drawn
enemyDraw(menuPinky);
aX = menuEnMv(aX, menuAngry);
enemyDraw(menuAngry);
//the previous animated dot is emptied
//and a black square is filled in in its place
ctx.clearRect(578 + (17 * num), 375, 5, 5);
ctx.fillStyle = 'black';
ctx.fillRect(568, 370, 100, 20);
//three dots are drawn after the text written in homeScreen()
ctx.beginPath();
ctx.fillStyle = 'pink';
ctx.textAlign = 'center';
ctx.font = "30px Arial";
ctx.fillText(". . .", 598, 380);
ctx.closePath();
//the current dot is painted white, in contrast to the pink dots, it creates a fluid animation
//like a loading screen
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.font = "30px Arial";
ctx.fillText(".", 581 + (17 * num), 380);
ctx.closePath();
num++;
//recursively calling the dotAnim function every 150 milliseconds
setTimeout(function () {
dotAnim(menuPinky, menuAngry, pX, aX, num)
}, 150);
}
if (num == 3) {
num = 0;
}
}
homeScreen();
//click event, where if the game is on a menu screen, it listens for a click and activates the game
canvas.addEventListener('click', function (event) {
if (strtGameCntrl == true) {
strtGameCntrl = false;
//if pac is alive then the game is starting from an initial state, so init needs to be called
if (!pac.dead) {
canvas.width = canvas.width;
init(mapArray, {
x: 450,
y: 270
}, {
x: 530,
y: 270
}, {
x: 750,
y: 210
}, {
x: 510,
y: 210
}, 2);
//as opposed to if it is starting from the highscores screen where pacman is dead
} else {
restartGame(objectStrtPos[0], objectStrtPos[1], objectStrtPos[2], objectStrtPos[3], mapArray, 2);
}
}
}, false);
//init(mapArray1, {x:310, y:310}, {x:350, y:330}, {x:250, y:250}, {x:350, y:270}, 5);
//the init function initialises the canvas as well as calling the functions to popAccBoundArr and the draw function
function init(currentMapVal, pinkyPos, angryPos, pacPos, strtPos, corridorDist) {
//init canvas
currentMap = currentMapVal;
pac.stunned = false;
//initialising the objectStrtPos array with the reset positions needed to put ghosts and pacman back to their intended positions upon game state change
objectStrtPos.push(new coOrds(pinkyPos.x, pinkyPos.y));
objectStrtPos.push(new coOrds(angryPos.x, angryPos.y));
objectStrtPos.push(new coOrds(pacPos.x, pacPos.y));
objectStrtPos.push(new coOrds(strtPos.x, strtPos.y));
//the grid is setup here
boundYSortArr = [];
portalChannel = corridorDist;
//the map is read. and it is converted to a single dim array of integers
popAccBoundArr();
//draw is called which controls most of the game, it calls most functions, and draws everything to the screen
draw();
}
function drawDeadPacman() {
//this draws the canvas rectangle
//and fills it with a dark blue colour
//it then draws the current deadPac image
canvas.height = canvas.height;
ctx.beginPath()
ctx.lineWidth = 2;
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgb(0,0,0)";
ctx.fill();
ctx.closePath()
drawItems();
ctx.drawImage(pac.img, 0 + (20 * deadLoop), 240, 20, 20, pac.position.x - 20, pac.position.y - 20, 40, 40);
deadLoop++;
}
function afterDeathReset(pinkyPos, angryPos, pacPos, strtPos) {
//this function loops through the dead pacman sprites
//when its done, it resets some properties and sends the game back into playing mode
drawDeadPacman();
//pac.stunned is used to control the stack when using specialized
pac.stunned = true;
//setInterval works like setTimeout but with a loop built into its functionality
//with setInterval the code iterates indefinatly until the clearInterval is called
var intrvl = setInterval(function () {
drawDeadPacman();
//check on the state of the animation, and the state of pacman
//if the animation and pac is still alive, required variables are reset
if (deadLoop > 12 && pac.dead == false && pac.stunned == true) {
pac.stunned = false;
pac.position.x = pacPos.x;
pac.position.y = pacPos.y;
pinky.position.x = pinkyPos.x;
pinky.position.y = pinkyPos.y;
pinky.strtingPos = strtPos;
angry.position.x = angryPos.x;
angry.position.y = angryPos.y;
angry.strtingPos.x = strtPos.x;
angry.strtingPos.y = strtPos.y;
pac.speed.y = 0;
pac.speed.x = 20;
angry.reset();
pinky.reset();
draw();
deadLoop = 0;
clearInterval(intrvl);
//if the user is dead after the animation is complete, then the highscore code needs to be called
} else if (pac.dead && deadLoop > 12) {
canvas.width = 1000;
setupLS();
clearInterval(intrvl);
}
}, 100);
}
//this function is called when the game wants to restart
//used primarily when moving from highscore to in game
//and also when the game runs out of food and needs to get some more
function restartGame(pinkyPos, angryPos, pacPos, strtPos, nMapArr, corridorDis) {
if (currentMap != nMapArr) {
currentMap = nMapArr;
portalChannel = corridorDis;
}
deadLoop = 0;
pac.position.x = pacPos.x;
pac.position.y = pacPos.y;
pinky.position.x = pinkyPos.x;
pinky.position.y = pinkyPos.y;
pinky.strtingPos = strtPos;
angry.position.x = angryPos.x;
angry.position.y = angryPos.y;
angry.strtingPos.x = strtPos.x;
angry.strtingPos.y = strtPos.y;
pinky.reset();
angry.reset();
//if restart is called and pac is dead, then the score and pacman is reset
if (pac.dead) {
score = 0;
pac.reset();
}
//emptying the objectStrtPos array and calling init which will repopulate objectStrtPos with new values
objectStrtPos = [];
init(nMapArr, pinkyPos, angryPos, pacPos, strtPos, corridorDis);
}
//the draw function draws the canvas
function draw() {
if (!pac.stunned) {
//this draws the canvas rectangle
//and fills it with a dark blue colour
canvas.height = canvas.height;
ctx.beginPath()
ctx.lineWidth = 2;
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgb(0,0,0)";
ctx.fill();
ctx.closePath();
//keypress events are handled here
//the if statement checks the state of the notpressed boolean var
//essentially it takes the current key pressed or the last key pressed
//and passes that number to a move function that handles either presses
if (notPressed == false) {
document.addEventListener('keydown', function (event) {
move(event.keyCode);
});
} else if (notPressed == true) {
move(lastPress);
}
//call 3 functions
update();
pcDirctnSwtch();
drawScore();
//code for highly buggy bonus level
//lots of unexpected bugs occur when using this map
//for now I will stick to the map i created
//possibly in the future I may implement this
/* if (score == 3000) {
canvas.width = 600;
score++;
boundYSortArr = [];
popAccBoundArr();
objectStrtPos = [];
pac.stunned = true;
currGridScrVal = 0;
setTimeout(function () {
restartGame({
x: 230,
y: 310
}, {
x: 250,
y: 350
}, {
x: 250,
y: 250
}, {
x: 230,
y: 250
}, mapArray1, 5)
}, 500);
}*/
}
}
function drawScore() {
//score is drawn on the screen here
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.font = "30px Arial";
ctx.fillText("Score: " + score, 100, 680);
ctx.closePath();
}
//this fucntion takes in a number "press"
//it checks for collisions and then alters the user speeds accordingly
function move(press) {
if (!pac.dead) {
//key press handled here
//checks collisions to the direction of pacman,
//if it's a valid place to move, speeds are altered
//if it would cause a collision, the key will be saved for later and processed at the next available opportunity
//each press is effectively the same, only changing the needed speeds and checking the correct collisions
//left key press
if (press == 37 || press == 65) {
if ((boundYSortArr[pac.currPos - 1].type == 3 || boundYSortArr[pac.currPos - 1].type == 0 || boundYSortArr[pac.currPos - 1].type == 13)) {
pac.speed.x = -20;
pac.speed.y = 0;
notPressed = false
} else {
lastPress = press;
notPressed = true;
}
}
//right key press
else if (press == 39 || press == 68) {
if ((boundYSortArr[pac.currPos + 1].type == 3 || boundYSortArr[pac.currPos + 1].type == 0 || boundYSortArr[pac.currPos + 1].type == 13)) {
pac.speed.x = 20;
pac.speed.y = 0;
notPressed = false
} else {
lastPress = press;
notPressed = true;
}
}
//down key press
else if (press == 40 || press == 83) {
if ((boundYSortArr[pac.currPos + currentMap[1].length].type == 3 || boundYSortArr[pac.currPos + currentMap[1].length].type == 0 || boundYSortArr[pac.currPos + currentMap[1].length].type == 13)) {
pac.speed.x = 0;
pac.speed.y = 20;
notPressed = false
} else {
lastPress = press;
notPressed = true;
}
}
//up key press
else if (press == 38 || press == 87) {
if ((boundYSortArr[pac.currPos - currentMap[1].length].type == 3 || boundYSortArr[pac.currPos - currentMap[1].length].type == 0 || boundYSortArr[pac.currPos - currentMap[1].length].type == 13)) {
pac.speed.x = 0;
pac.speed.y = -20;
notPressed = false
} else {
lastPress = press;
notPressed = true;
}
}
}
}
//this function checks the speed of pacman
//based on which speed is not 0, pacman will be drawn in a different direction
//it calls pacDraw which takes in variables needed to draw the 3/4 circle that is pacman
function pcDirctnSwtch() {
//for pacman to face right
if (pac.speed.x > 0) {
pacDraw(1.8, 0.2);
//for pacman to face to the left
} else if (pac.speed.x < 0) {
pacDraw(0.8, 1.2);
//for pacman to face down
} else if (pac.speed.y > 0) {
pacDraw(0.2, 0.8)
//for pacman to face up
} else if (pac.speed.y < 0) {
pacDraw(1.2, 1.8);
}
}
//this function is used to draw the main man
//it takes in two variables that are multiplied by pi
//essentially these variables control the direction pacman will face
function pacDraw(pi1, pi2) {
//Code for unused soundfile to play the traditional "Waka Waka" sound the original pacman made.
//I chose against using it for now as getting it to synchronize with the movement of pacman was a nightmare
//for now it is simply easier to play the silent game
//var snd = new Audio("waka waka.mp3"); // buffers automatically when created
//snd.play();
//draw all is called to draw the enemies and the map
drawAll();
//pacman is drawn
ctx.beginPath();
//pacman-yellow is set as the current fill style
ctx.fillStyle = "rgb(255,238,0)";
//opnClsBool controls the open close nature of pacmans mouth
//when "on" it draws him open mouthed
//when off it draws him closed mouth
if (opnClsBool == 0) {
//open mouth drawn
ctx.arc(pac.position.x, pac.position.y, 15, pi1 * Math.PI, pi2 * Math.PI, true);
ctx.lineTo(pac.position.x, pac.position.y);
//sets the variable to 1 so that on next draw it will draw a full circle
opnClsBool = 1;
} else {
//draw a full circle
ctx.arc(pac.position.x, pac.position.y, 15, 2 * Math.PI, 0 * Math.PI, true);
//set the variable to 0, effectively resetting pacman for the next draw to be open mouthed
opnClsBool = 0;
}
//fills in the pacman object drawn to the canvas
ctx.stroke();
ctx.fill();
ctx.closePath();
ctx.fillStyle = "rgb(0,0,0)";
//checks stae of opnClsBool
//if only half way through the animation, redraw the pacman
//or else redraw the canvas
if (opnClsBool == 1) {
setTimeout(function () {
pacDraw(pi1, pi2);
}, 100);
} else {
if (!pac.stunned) {
setTimeout(draw, 150);
}
}
}
//draw all simply calls draw functions to draw the rest of the objects
function drawAll() {
drawItems();
enemyDraw(pinky);
enemyDraw(angry);
}
//the update function calls other functions
//these functions update the positions of the objects
function update() {
usrUpdate(pac);
pinkGhostUpdate(pinky);
redGhostUpdate(angry);
}
//usrUpdate takes in a character object
//this object will always be pacman, but incase i would want to turn this into multiplayer at a later date
//it takes the objcet rather than calling the object directly
function usrUpdate(pac) {
//possMove is the move that pacman wants to move to
var possMove;
//bool is just a generic boolean
var bool;
var pos, pos1;
//determines which axis to alter
//if ySpeed is 0 then xSpeed will have a value
// and vice versa
if (pac.speed.x != 0) {
//chk the space to the left or right
//possmove is now calculated by doing the math that will occur if the move is good
possMove = pac.position.x + pac.speed.x;
//creating 2 objects, one with the current position and one with the coordinates of the next move
pos = {
y: pac.position.y,
x: pac.position.x,
name: pac.type
};
pos1 = {
y: pac.position.y,
x: possMove
};
//this else statement is effectively the same as above, the only difference
//is that it affects the y coordinates rather than the x coordinates
} else if (pac.speed.y != 0) {
//chk the space up or down
possMove = pac.position.y + pac.speed.y;
pos = {
y: pac.position.y,
x: pac.position.x,
name: pac.type
};
pos1 = {
y: possMove,
x: pac.position.x
};
}
var mapPos = getMapPos(pos);
//setPossDifference alters mapPos so that it represents the
//coOrdinates of the position to be moved to
mapPos = setPossDifference(pos, pos1, mapPos);
bool = chkMapNoType(mapPos, pos, pac);
if (bool) {
pac.currPos = mapPos;
//as long as the move is valid, it will update the position
//the amount of the speed
pac.updatePacman();
}
}
//this function loops through the array, it first finds the row of the current pacman position
//it then searches through that column to find the row
function getMapPos(curPos) {
for (mapPos = 0; mapPos < boundYSortArr.length; ++mapPos) {
//if the column is not correct, add a the length of a row to mapPos so that it gets the next row
if (curPos.y != boundYSortArr[mapPos].y) {
mapPos += currentMap[1].length - 1;
//to get to this part of the code, the search would have to find the row of pacman
//at this point, we search through this row to find it's exact location
} else if (curPos.x == boundYSortArr[mapPos].x) {
//found currunt position at this point
//this returned position is returned to the parent function
return mapPos;
}
}
}
function setPossDifference(curPos, possPos, mapPos) {
//posDiff checks x value and y value of curPos and possPos
//it returns an 'x' if the x positions are different and a 'y' if the ys are different
if (posDiff(curPos, possPos) == 'x') {
//a check to see in what way are they different
//if current is bigger than potential, then pacman wants to move right
if (curPos.x > possPos.x) {
mapPos--;
//otherwise pacman wants to move left
} else if (curPos.x < possPos.x) {
mapPos++;
}
//if 'x' wasnt return than it has to be the y that is different
//in this case pacman wants to move up or down
} else {
//if up then pacman needs to shift the length of a row in the 2-dim array
if (curPos.y > possPos.y) {
mapPos -= currentMap[0].length;
//otherwise he needs to shift back the same length as he is going down
} else {
mapPos += currentMap[0].length;
}
}
//at this point, the position that pacman wants to move in is known in the array
//so that position is returned
return mapPos;
}