-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptPacman.js
1047 lines (911 loc) · 48.8 KB
/
scriptPacman.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
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
/**
* Componente canvas que nos permite dibujar en la página
* @type {HTMLElement}
*/
let canvas = document.getElementById("pacmanCanvas");
let ctx = canvas.getContext('2d');
/**
* Contiene el tile de los escenarios principales
* @type {HTMLImageElement}
*/
let imgTile = new Image();
imgTile.src = "imagenes/newResource2.png"
/**
* Carga de imagenes de pacman
* @type {HTMLImageElement}
*/
let derechaPacman = new Image();
derechaPacman.src = "imagenes/pacman_right.png"
let derechaX = 0
let derechaY = 0
let izquierdaPacman = new Image();
izquierdaPacman.src = "imagenes/pacman_left.png"
let izquierdaX = 0
let arribaPacman = new Image();
arribaPacman.src = "imagenes/pacman_up.png"
let arribaX = 0
let abajoPacman = new Image();
abajoPacman.src = "imagenes/pacman_down.png"
let abajoX = 0
let muertePacman = new Image();
muertePacman.src = "imagenes/pacman_death.png"
//Fin carga pacman
//Carga de recursos varios
/**
* Carga de recursos varios
* @type {HTMLImageElement}
*/
let punto = new Image();
punto.src = "imagenes/powerPellet.png"
let ready = new Image();
ready.src = "imagenes/ready.png"
let vidaExtra = new Image();
vidaExtra.src = "imagenes/extra_life.png";
let gameOverImg = new Image();
gameOverImg.src = "imagenes/game_over.png";
let inicioJuego = new Audio("audio/game_start.mp3");
let muerteSonido = new Audio("audio/death.mp3")
let waka1 = new Audio("audio/dot_1.mp3")
let waka2 = new Audio("audio/dot_2.mp3")
//Fin carga recursos varios
//Zona de declaraciones fantasmas
/**
* Carga de fantasmas
* @type {HTMLImageElement}
*/
let blinkyArriba = new Image();
let blinkyAbajo = new Image();
let blinkyDerecha = new Image();
let blinkyIzquierda = new Image();
blinkyArriba.src = "imagenes/blinky_up_angry.png";
blinkyDerecha.src = "imagenes/blinky_right_angry.png";
blinkyAbajo.src = "imagenes/blinky_down_angry.png";
blinkyIzquierda.src = "imagenes/blinky_left_angry.png";
let blinkyTam = 20;
let blinkyMov = 3.25;
let blinkyInc = 20;
let xActBlinky = 150;
let yActBlinky = 125;
let barreraFantasma = false;
let prediccion = 2;
let prediccionBackup = 2;
//Carga de fantasma Pinky
let pinkyArriba = new Image();
let pinkyAbajo = new Image();
let pinkyDerecha = new Image();
let pinkyIzquierda = new Image();
pinkyArriba.src = "imagenes/pinky_up.png";
pinkyDerecha.src = "imagenes/pinky_right.png";
pinkyAbajo.src = "imagenes/pinky_down.png";
pinkyIzquierda.src = "imagenes/pinky_left.png";
let pinkyInc = 20;
let xActPinky = 380;
let yActPinky = 125;
let barreraFantasmaPinky = false;
let prediccionPinky = 1;
let prediccionPinkyBackup = 1;
//Fin de carga fantasmas
//Zona variables pacman
let choqueFantasma = false;
let vidas = 2;
let incNivel = 250;
let decisionAudio = true;
let x = canvas.width;
let key = 0;
let xActual = 245;
let yActual = 120;
let marcador = 0;
let indice = 0;
let inc = 0;
const cantInc = 20;
const y = canvas.height;
let barreraBool = false;
const pacmanTam = 20;
/**
* Indica la cantidad de desplazamiento de Pac-Man
* @type {number}
*/
const pacmanCantiMov = 3.75;
const barrera = canvas.width;
let keyBackup = 1;
let c, r;
let nivel = 0;
let aumentoTam = 10;
let pacmanTamIrrX = pacmanTam;
let pacmanTamIrrY = 12;
let cargaPantalla = false;
let vistazo = false;
let cambio = 0;
let finJuego = false;
let subirNivelBool = false;
let totalPuntos = 0;
/**
* Es una matriz cuyas posiciones permiten dibujar los escenarios del juego.
* Si se quisiera agregar un nuevo escenario, basta con agregar una nueva posición a la matriz con las mismas características
* que las otras posiciones por default.
* Si se quiere cambiar un elemento dentro del escenario, el atributo tipo indica el numero de elemento a dibujar, los cuales
* se espicican dentro de la imagen newResourcesnumeros.
* @example
* {tipo: 10} Es una barra horizontal.
* @name barrerasMatriz
*
*/
let barrerasMatriz =
[
[
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 1}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 8}, {tipo: 9}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 0,},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 25,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 24,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 25}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: 24}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -2}, {tipo: -2}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14,}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 20,}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 5}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 4,},],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}]
],
[
// 0 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
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 1}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 8}, {tipo: 9}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 0,},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 25,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 24,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 25}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: 24}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -2}, {tipo: -2}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14,}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 20,}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 0}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 23}, {tipo: 14}, {tipo: 14}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 1}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: 20}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27}, {tipo: 26}, {tipo: -1,}, {tipo: 27}, {tipo: 20}, {tipo: 20}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 38,}, {tipo: -1}, {tipo: -2}, {tipo: -2}, {tipo: -1,}, {tipo: 23}, {tipo: 38}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 2}, {tipo: 3,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 2}, {tipo: 3}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: 14}, {tipo: 14}, {tipo: 14,}, {tipo: 14}, {tipo: 4}, {tipo: -1}, {tipo: 27,}, {tipo: 26}, {tipo: -1}, {tipo: 27}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 27}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 26}, {tipo: -1}, {tipo: 5}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 5}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 6}, {tipo: 7}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 4,},],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}]
],
[
// 0 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
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}],
[{tipo: 1}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 8}, {tipo: 9}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 0,},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 23,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 25,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 24,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 25}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: 24}, {tipo: -1}, {tipo: 25}, {tipo: -2,}, {tipo: -2}, {tipo: 24}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -2}, {tipo: -2}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14}, {tipo: 14,}, {tipo: 38}, {tipo: -1}, {tipo: 23,}, {tipo: 38}, {tipo: -1}, {tipo: 23}, {tipo: 14,}, {tipo: 14}, {tipo: 38}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 27}, {tipo: 20,}, {tipo: 20}, {tipo: 20}, {tipo: 20}, {tipo: 20,}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 27,}, {tipo: 20}, {tipo: 20}, {tipo: 26,}, {tipo: -1}, {tipo: 2},],
[{tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,},],
[{tipo: 3}, {tipo: 10}, {tipo: 10}, {tipo: 10,}, {tipo: 10}, {tipo: 0}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: 14}, {tipo: 14}, {tipo: 38,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 23}, {tipo: 14}, {tipo: 14}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 1}, {tipo: 10,}, {tipo: 10}, {tipo: 10}, {tipo: 10}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: 20}, {tipo: 20}, {tipo: 26}, {tipo: -1}, {tipo: 27}, {tipo: 26}, {tipo: -1,}, {tipo: 27}, {tipo: 20}, {tipo: 20}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 23}, {tipo: 38,}, {tipo: -1}, {tipo: -2}, {tipo: -2}, {tipo: -1,}, {tipo: 23}, {tipo: 38}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2,}, {tipo: -2}, {tipo: 2}, {tipo: -1},{tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 2}, {tipo: 3,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 2}, {tipo: 3}, {tipo: -1}, {tipo: 2,}, {tipo: 3}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2}, {tipo: -1}, {tipo: 27,}, {tipo: 26}, {tipo: -1}, {tipo: 27}, {tipo: 26,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: 27}, {tipo: 26}, {tipo: -1}, {tipo: 27,}, {tipo: 26}, {tipo: -1}, {tipo: 3}, {tipo: -2,}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: 2}, {tipo: 3}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: -1}, {tipo: -1,}, {tipo: -1}, {tipo: -1}, {tipo: 3}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: -2}, {tipo: 2,},],
[{tipo: 5}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 6}, {tipo: 7}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 12,}, {tipo: 12}, {tipo: 12}, {tipo: 12}, {tipo: 4,},],
[{tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}, {tipo: 100}]
]
]
document.addEventListener('keydown', manejarTecladoAbajo, false);
setInterval(dibujar, 40, key)
/**
*
* Reproduce el sonido inicial del juego, ademas de colocar en pantalla la imagen de READY!
* @function pantallaCarga
* */
function pantallaCarga() {
if (indice < 200) {
/** Permite difuminar la imagen que se está dibujando.
* @name ctx.globalAlpha
* @param cantidad - 0-1
* @global*/
ctx.globalAlpha = 1 - indice / 200;
ctx.drawImage(ready, 0, 0, 48, 16, canvas.width / 2 - 100, canvas.height / 2 - 30, 216, 52);
ctx.globalAlpha = 1;
indice += 2;
inicioJuego.play()
}
}
/**
* Rescata de los eventos del teclado el movimiento de las flechas (izquierda, derecha, arriba, abajo), los almacena en
* una variable.
* 37 izquierda, 39 derecha, 38 arriba, 40 abajo
* @param key - Valor de la tecla pulsada
* @param e - Contiene los eventos ocurridos en el teclado
*/
function manejarTecladoAbajo(e) {
if (e.keyCode === 39) {
key = 39;
}
if (e.keyCode === 37) {
key = 37;
}
if (e.keyCode === 38) {
key = 38;
}
if (e.keyCode === 40) {
key = 40;
}
}
//Vamos a definir los valores de las direcciones para cambiar la imagen del pacman
//2 arriba, 1 izquierda, 0 abajo, 3 derecha
//! Movimientos de PACMAN
/**
* Estructura principal de movimiento de Pac-Man
* Contiene una condicion principal cuya dependecia proviene de no colisionar Pac-man con un fantasma (pinky, blinky)
* @function movimientoPacman
*
*/
function movimientoPacman() {
/**
* Almacenan temporalmente las posiciones x, y actuales de Pac-man, como respaldo en caso de modificaciones.
* @name xTempPacman
* @inner
* @type {number}
*/
let xTempPacman = xActual;
/**
* Almacenan temporalmente las posiciones x, y actuales de Pac-man, como respaldo en caso de modificaciones.
* @name YTempPacman
* @inner
* @type {number}
*/
let yTempPacman = yActual;
if (!choqueFantasma) {
/**
* De manera general, segun el valor de key dado se incrementa la posicion actual de Pac-man para verificar si en
* el siguiente paso colisionará con una barrera. Posteriormente se recupera el estado anterior de la posicion
*/
if (key === 39) {
xActual += pacmanCantiMov;
vistazoBarrera();
xActual = xTempPacman;
}
if (key === 37) {
xActual -= pacmanCantiMov;
vistazoBarrera();
xActual = xTempPacman;
}
if (key === 40) {
yActual += pacmanCantiMov;
vistazoBarrera();
yActual = yTempPacman;
}
if (key === 38) {
yActual -= pacmanCantiMov;
vistazoBarrera();
yActual = yTempPacman;
}
if (key !== keyBackup && barreraBool) {
barreraBool = !barreraBool
}
//37 izquierda, 39 derecha, 38 arriba, 40 abajo
/**
* Se realiza la llamada a la funcion que efectua el movimiento según la tecla presionada
*/
if (key === 39) {
derecha();
}
if (key === 37) {
izquierda();
}
if (key === 40) {
abajo();
}
if (key === 38) {
arriba();
}
keyBackup = key;
barreraBool = false;
}
}
/**
* Realiza una estimación de sí el movimiento que se quiere realizar o se está realizando provoca una colisión con una
* barrera.
* En caso de encontrarse con una barrera entonces encenderá una bandera y tomará el valor anterior de la tecla
* pulsada para que sea el valor actual.
* @example
* Siendo keyBackup = 39 y key = 40, además de barreraBool = true, entonces key tomará el valor de keybackup
* @function vistazoBarrera
*
*/
function vistazoBarrera() {
detectarBarrera();
if (barreraBool) {
key = keyBackup;
}
}
/**
* En general, para las siguientes funciones siguen la misma secuencia de implementación.
* Dibujar el sprite segun la dirección dada respecto al tamaño del objeto y su posicion actual, una vez que se dibujo
* y se hizo la comparación que no existe una barrerra o que no se ha llegado al límite del canvas, entonces se incrementa
* un contador que permite avanzar en la siguiente posicion el sprite de movimiento.
*
* {@link derecha}
* {@link arriba}
* {@link abajo}
* {@link izquierdaFantasma}
* {@link derechaFantasma}
* {@link arribaFantasma}
* {@link abajoFantasma}
*
* @function izquierda
* */
function izquierda() {
ctx.drawImage(izquierdaPacman, izquierdaX + inc, 0, 16, 16, xActual, yActual, pacmanTam + aumentoTam, pacmanTam + aumentoTam);
if ((xActual > 0) && !barreraBool) {
xActual -= pacmanCantiMov;
inc += cantInc - 4;
} else inc = cantInc - 4;
if (inc > 60) inc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda}
*/
function derecha() {
ctx.drawImage(derechaPacman, derechaX + inc, derechaY, 16, 16, xActual, yActual, pacmanTam + aumentoTam, pacmanTam + aumentoTam);
if ((xActual < x - (pacmanTam + pacmanCantiMov)) && !barreraBool) {
xActual += pacmanCantiMov;
inc += cantInc - 4;
} else inc = 16;
if (inc > 60) inc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda}
*/
function arriba() {
ctx.drawImage(arribaPacman, arribaX + inc, 0, 16, 16, xActual, yActual, pacmanTam + aumentoTam, pacmanTam + aumentoTam);
if ((yActual > 0 && yActual < barrera) && !barreraBool) {
yActual -= pacmanCantiMov;
inc += cantInc - 4;
} else inc = cantInc - 4;
if (inc > 60) inc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda}
*/
function abajo() {
ctx.drawImage(abajoPacman, abajoX + inc, 0, 16, 16, xActual, yActual, pacmanTam + aumentoTam, pacmanTam + aumentoTam);
if ((yActual < y - (pacmanTam + pacmanCantiMov)) && !barreraBool) {
yActual += pacmanCantiMov;
inc += cantInc - 4;
} else inc = cantInc - 4;
if (inc > 60) inc = 0;
}
/**
* Primeramente hay algo que remarcar dentro de esta función, esta puede convertirse en un método de un objeto fantasma
* puesto que al existir varios fantasmas, podemos realizar herencia. Sin embargo, por cuestiones de falta de conocimiento
* de implementación e POO en Js, se omitió por el momento el uso de objetos. No se descarta su uso en el futuro.
*
*
* Esta función comprueba en la ejecución del programa si Pac-man ha colisionado con un fantasma (blinky) y envía la
* llamada a otra función.
* @function colisionConFantasma
*/
function colisionConFantasma() {
if (xActBlinky < xActual + 12 && xActBlinky + 12 > xActual && yActBlinky < yActual + blinkyTam && blinkyTam + yActBlinky > yActual) {
choqueFantasma = true;
dibujarMuertePacman();
}
}
/**
* Realiza la misma tarea que {@link colisionConFantasma} con la diferencia que el fantasma es Pinky.
* @function colisionConFantasmaPinky
*/
function colisionConFantasmaPinky() {
if (xActPinky < xActual + 12 && xActPinky + 12 > xActual && yActPinky < yActual + blinkyTam && blinkyTam + yActPinky > yActual) {
choqueFantasma = true;
dibujarMuertePacman();
}
}
/**
* De manera simple se encarga de colocar en pantalla la imagen de Game Over en una posicion por default y reprodicor
* el audio para dicha acción mientras las condiciones se cumplan.
*
* @function dibujarMuertePacman
*/
function dibujarMuertePacman() {
if (cambio <= 12) {
muerteSonido.play();
ctx.drawImage(muertePacman, cambio * 16, 0, 16, 16, xActual, yActual, pacmanTam + aumentoTam, pacmanTam + aumentoTam);
cambio += 1;
} else {
/**
* Si ya se ha terminado de mostrar la imagen anterior, entonces se le resta una vida al jugador y se reinician
* las variables para continuar con el juego.
*/
if (cambio === 13) {
cambio = 0;
choqueFantasma = false;
vidas--;
reinicio();
}
}
}
/**
* Muestra en pantalla la cantidad de vidas que tiene el jugador siempre y cuando este valor no sea inferior a 0
* Si las vidas se han terminado (menores a 0) entonces llama a la función que finaliza el juego
* @function dibujaVidas
*/
function dibujaVidas() {
if (vidas >= 0) {
let contador = 0;
while (contador <= vidas) {
ctx.drawImage(vidaExtra, 0, 0, 16, 16, contador * 16,incNivel, 16, 16);
contador++;
}
} else {
gameOver();
}
}
/**
* Muestra en pantalla la imagen de gameOver en una posicion predefinida y activa una bandera que controla el flujo principal
* del juego
* @function gameOver
*/
function gameOver() {
ctx.drawImage(gameOverImg, 0, 0, 80, 16, (canvas.width / 2) - 75, 120, 160, 32);
finJuego = true;
}
/**
* Se encarga de colocar las variables en su valor por default cuando se sube de nivel o cuando muere Pac-Man
* por lo que algunas variables se reinicializan de distinta forma.
* @example
* Si se ha subido de nivel (subirNivelBool == true) entonces a la variable nivel se le incrementa en uno su
* valor actual.
* @function reinicio
*/
function reinicio() {
if (!subirNivelBool){
xActBlinky = 150;
yActBlinky = 125;
barreraFantasma = false;
prediccion = 1;
prediccionBackup = 2;
choqueFantasma = false;
decisionAudio = true;
x = canvas.width;
key = 0;
xActual = 245;
yActual = 120;
indice = 0;
inc = 0;
barreraBool = false;
keyBackup = 1;
nivel += 0;
cargaPantalla = false;
vistazo = false;
cambio = 0;
}
else{
xActBlinky = 150;
yActBlinky = 125;
barreraFantasma = false;
prediccion = 1;
prediccionBackup = 2;
choqueFantasma = false;
decisionAudio = true;
x = canvas.width;
key = 0;
xActual = 245;
yActual = 120;
indice = 0;
inc = 0;
barreraBool = false;
keyBackup = 1;
nivel ++;
cargaPantalla = false;
vistazo = false;
cambio = 0;
subirNivelBool = false;
}
}
/**
*
* Muestra en pantalla el texto de LEVEL UP cuando el jugador ha consumido todos los puntos posibles dentro del escenario
* Además de reiniciar el escenario y hacer más rápido el movimiento de los fantasmas.
*
* @function
*/
function subirNivel() {
if (totalPuntos === 0){
ctx.font = "30px pacman";
ctx.fillStyle = "white";
ctx.fillText("LEVEL UP", 200, 100);
subirNivelBool = true;
blinkyMov += 0.6;
incNivel = 350;
reinicio();
}
}
// Fin de movimientos pacman
//Movimientos fantasma blinky
/**
* El movimiento que los fantasmas siguen es similar al que Pac-Man sigue, con la única diferencia que a Pac-Man lo controla
* el usuario y los fantasmas no.
* Por lo que si se compara {@link movimientoPacman} con movimientoFantasma nos daremos cuenta que la única sección que
* sufre alteraciones es la de añadir un ciclo while a la función.
* Dentro de este ciclo while se calcula un número de manera aleatoria que sea diferente al movimiento inmediato anterior.
* Siempre y cuando el fantasma se encuentre delante de una barrera y predicción tenga un valor diferente de predicciónBackup
*
* @function movimientoFantasma
*/
function movimientoFantasma() {
let xTempFantasma = xActBlinky;
let yTempFantasma = yActBlinky;
if (!choqueFantasma) {
if (barreraFantasma && prediccionBackup === prediccion) {
while (prediccionBackup === prediccion) {
prediccion = Math.floor(Math.random() * 4) + 1
}
}
if (prediccion === 1) {
xActBlinky += blinkyMov;
vistazoBarreraFantasma();
xActBlinky = xTempFantasma;
}
if (prediccion === 2) {
xActBlinky -= blinkyMov;
vistazoBarreraFantasma();
xActBlinky = xTempFantasma;
}
if (prediccion === 3) {
yActBlinky += blinkyMov;
vistazoBarreraFantasma();
yActBlinky = yTempFantasma;
}
if (prediccion === 4) {
yActBlinky -= blinkyMov;
vistazoBarreraFantasma();
yActBlinky = yTempFantasma;
}
if (prediccion !== prediccionBackup && barreraFantasma) {
barreraFantasma = !barreraFantasma;
}
if (prediccion === 1) {
derechaFantasma();
}
if (prediccion === 2) {
izquierdaFantasma();
}
if (prediccion === 3) {
abajoFantasma();
}
if (prediccion === 4) {
arribaFantasma();
}
prediccionBackup = prediccion;
barreraBool = false;
}
}
/**
* Mismo funcionamiento que {@link vistazoBarrera}, solo cambia el valor de barrera que se evalúa.
* @function vistazoBarreraFantasma
*/
function vistazoBarreraFantasma() {
detectarBarreraFantasma();
if (barreraFantasma) {
prediccion = prediccionBackup;
}
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function izquierdaFantasma() {
ctx.drawImage(blinkyIzquierda, blinkyInc, 0, 16, 16, xActBlinky, yActBlinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((xActBlinky > 0) && !barreraFantasma) {
xActBlinky -= blinkyMov;
blinkyInc += 16;
} else blinkyInc = 0;
if (blinkyInc > 16) blinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function derechaFantasma() {
ctx.drawImage(blinkyDerecha, blinkyInc, derechaY, 16, 16, xActBlinky, yActBlinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((xActBlinky < x - (blinkyTam + blinkyMov)) && !barreraFantasma) {
xActBlinky += blinkyMov;
blinkyInc += 16;
} else blinkyInc = 16;
if (blinkyInc > 16) blinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function arribaFantasma() {
ctx.drawImage(blinkyArriba, blinkyInc, 0, 16, 16, xActBlinky, yActBlinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((yActBlinky > 0 && yActBlinky < barrera) && !barreraFantasma) {
yActBlinky -= blinkyMov;
blinkyInc += 16;
} else blinkyInc = 16;
if (blinkyInc > 16) blinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function abajoFantasma() {
ctx.drawImage(blinkyAbajo, blinkyInc, 0, 16, 16, xActBlinky, yActBlinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((yActBlinky < y - (blinkyTam + blinkyMov)) && !barreraFantasma) {
yActBlinky += blinkyMov;
blinkyInc += 16;
} else blinkyInc = 16;
if (blinkyInc > 16) blinkyInc = 0;
}
//Fin de movimientos blinky
//Movimientos fantasma pinky
/**
* Misma funcionalidad que {@link movimientoFantasma} con la diferencia que cambia el fantasma en el que se evalúan las
* funciones.
*/
function movimientoFantasmaPinky() {
let xTempFantasma = xActPinky;
let yTempFantasma = yActPinky;
if (!choqueFantasma) {
if (barreraFantasmaPinky && prediccionPinkyBackup === prediccionPinky) {
while (prediccionPinkyBackup === prediccionPinky) {
prediccionPinky = Math.floor(Math.random() * 4) + 1
}
}
if (prediccionPinky === 1) {
xActPinky += blinkyMov;
vistazoBarreraFantasmaPinky();
xActPinky = xTempFantasma;
}
if (prediccionPinky === 2) {
xActPinky -= blinkyMov;
vistazoBarreraFantasmaPinky();
xActPinky = xTempFantasma;
}
if (prediccionPinky === 3) {
yActPinky += blinkyMov;
vistazoBarreraFantasmaPinky();
yActPinky = yTempFantasma;
}
if (prediccionPinky === 4) {
yActPinky -= blinkyMov;
vistazoBarreraFantasmaPinky();
yActPinky = yTempFantasma;
}
if (prediccionPinky !== prediccionPinkyBackup && barreraFantasmaPinky) {
barreraFantasmaPinky = !barreraFantasmaPinky;
}
if (prediccionPinky === 1) {
derechaFantasmaPinky();
}
if (prediccionPinky === 2) {
izquierdaFantasmaPinky();
}
if (prediccionPinky === 3) {
abajoFantasmaPinky();
}
if (prediccionPinky === 4) {
arribaFantasmaPinky();
}
prediccionPinkyBackup = prediccionPinky;
barreraBool = false;
}
}
function vistazoBarreraFantasmaPinky() {
detectarBarreraFantasmaPinky();
if (barreraFantasmaPinky) {
prediccionPinky = prediccionPinkyBackup;
}
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function izquierdaFantasmaPinky() {
ctx.drawImage(pinkyIzquierda, pinkyInc, 0, 16, 16, xActPinky, yActPinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((xActPinky > 0) && !barreraFantasmaPinky) {
xActPinky -= blinkyMov;
pinkyInc += 16;
} else pinkyInc = 0;
if (pinkyInc > 16) pinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function derechaFantasmaPinky() {
ctx.drawImage(pinkyDerecha, pinkyInc, derechaY, 16, 16, xActPinky, yActPinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((xActPinky < x - (blinkyTam + blinkyMov)) && !barreraFantasmaPinky) {
xActPinky += blinkyMov;
pinkyInc += 16;
} else pinkyInc = 16;
if (pinkyInc > 16) pinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function arribaFantasmaPinky() {
ctx.drawImage(pinkyArriba, pinkyInc, 0, 16, 16, xActPinky, yActPinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((yActPinky > 0 && yActPinky < barrera) && !barreraFantasmaPinky) {
yActPinky -= blinkyMov;
pinkyInc += 16;
} else pinkyInc = 16;
if (pinkyInc > 16) pinkyInc = 0;
}
/**
* funcion de movimiento, sigue el patrón de {@link izquierda} para fantasma.
*/
function abajoFantasmaPinky() {
ctx.drawImage(pinkyAbajo, pinkyInc, 0, 16, 16, xActPinky, yActPinky, blinkyTam + aumentoTam, blinkyTam + aumentoTam);
if ((yActPinky < y - (blinkyTam + blinkyMov)) && !barreraFantasmaPinky) {
yActPinky += blinkyMov;
pinkyInc += 16;
} else pinkyInc = 16;
if (pinkyInc > 16) pinkyInc = 0;
}
//Fin de movimientos pinky
/**
* Función principal del juego. Dicha funcion se encarga de administrar la ejecucion de las funciones más importantes para
* el funcionamiento del juego.
* Si el juego ocurre con normalidad segun la variable finJuego, entonces seguira ejecutando las funciones principales,
* de lo contrario desplegará la animación de game over.
* Pero además si el nivel máximo fue alcanzado entonces mostrará Fin del juego pero en favor del usuario.
*
* @function dibujar
*/
function dibujar() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!finJuego) {
pantallaCarga();
dibujaBarrera();
dibujarMarcador();
movimientoFantasma();
movimientoFantasmaPinky();
colisionConFantasma();
colisionConFantasmaPinky();
movimientoPacman();
dibujaVidas();
subirNivel();
}
else {
dibujaBarrera();
gameOver();
}
if (nivel > 2){
ctx.font = "30px pacman";
ctx.fillStyle = "white";
ctx.fillText("Fin del Juego", canvas.width / 2 - 170, 350);
}
}
let coord = [
[225, 0], [234, 0], [243, 0], [252, 0], [261, 0], [270, 0], [279, 0], [288, 0], [297, 0], [306, 0], [315, 0], [324, 0], [333, 0], [342, 0], [351, 0], [360, 0],
[225, 9], [234, 9], [243, 9], [252, 9], [261, 9], [270, 9], [279, 9], [288, 9], [297, 9], [306, 9], [315, 9], [324, 9], [333, 9], [342, 9], [351, 9], [360, 9],
[225, 18], [234, 18], [243, 18], [252, 18], [261, 18], [270, 18], [279, 18], [288, 18], [297, 18], [306, 18], [315, 18], [324, 18], [333, 18], [342, 18], [351, 18], [360, 18],
];
/**
* Principal funcion que se encarga de dibujar el escenario, es decir, las barreras. Pero también dibuja en el escenario
* los puntos que Pac-Man se puede comer. Guarda en cada posicion de la matriz los atributos x, y que posteriormente nos
* permitirán conocer la posición actual de una barrera o de un punto.
* @function dibujaBarrera
*/
function dibujaBarrera() {
let recuentoPuntos = 0;
for (c = 0; c < barrerasMatriz[nivel].length; c++) {
for (r = 0; r < barrerasMatriz[nivel][c].length; r++) {
if (barrerasMatriz[nivel][c][r].tipo >= 0 && barrerasMatriz[nivel][c][r].tipo <= 99) {
let accesoBloque = barrerasMatriz[nivel][c][r];
let xBarrera = (r * 18);
let yBarrera = (c * 18);
accesoBloque.x = xBarrera;
accesoBloque.y = yBarrera;
ctx.drawImage(imgTile, coord[accesoBloque.tipo][0], coord[accesoBloque.tipo][1], 7, 7, xBarrera, yBarrera, 18, 18)
} else {
if (barrerasMatriz[nivel][c][r].tipo === -1) {
let accesoBloque = barrerasMatriz[nivel][c][r];
let xBarrera = (r * 18) + 2;
let yBarrera = (c * 18) + 2;
accesoBloque.x = xBarrera;
accesoBloque.y = yBarrera;
recuentoPuntos++;
ctx.drawImage(punto, 0, 0, 8, 8, xBarrera, yBarrera, 6, 6)
}
}
}
}
totalPuntos = recuentoPuntos;
}
/**
* Compara las posiciones de las barreras o los puntos con la posición actual de Pac-Man y determina si existe una colisión
* Sigue la siguiente estructura para hacer la comparación
* rect1.x < rect2.x + rect2.w &&
* rect1.x + rect1.w > rect2.x &&
* rect1.y < rect2.y + rect2.h &&
* rect1.h + rect1.y > rect2.y
* En caso de darse una colisión con una barrera, entonces encenderá una bander para indicar al programa principal la existencia
* de la colisión.
* Si la colisión se da con un punto, entonces incrementará el marcador y reproducirá el sonido para este caso.
* @returns {boolean}
*/
function detectarBarrera() {
for (c = 0; c < barrerasMatriz[nivel].length; c++) {
for (r = 0; r < barrerasMatriz[nivel][c].length; r++) {
let accesoBloque = barrerasMatriz[nivel][c][r];
if (accesoBloque.tipo >= 0) {
/* rect1.x < rect2.x + rect2.w &&
rect1.x + rect1.w > rect2.x &&
rect1.y < rect2.y + rect2.h &&
rect1.h + rect1.y > rect2.y*/
if (accesoBloque.x < xActual + pacmanTamIrrY && accesoBloque.x + pacmanTamIrrY > xActual && accesoBloque.y < yActual + pacmanTamIrrX && pacmanTamIrrY + accesoBloque.y > yActual) {
return barreraBool = true;
}
barreraBool = false;
} else {
if (accesoBloque.tipo === -1) {
if (accesoBloque.x < xActual + pacmanTam && accesoBloque.x + pacmanTam> xActual && accesoBloque.y < yActual + pacmanTam && pacmanTam + accesoBloque.y > yActual) {
marcador += 1;
accesoBloque.tipo = -2;
if (decisionAudio) {
waka1.play();
decisionAudio = false;
} else {
waka2.play()
decisionAudio = true;
}
}
barreraBool = false;
}
}
}
}
}
/**
* Realiza una función parecida que {@link detectarBarrera} con la diferencia que en esta función se compara la posición
* actual del fantassma en lugar de Pac-Man, además de solo hacer la comparasión con las barreras.
* @returns {boolean}
*/
function detectarBarreraFantasma() {
for (c = 0; c < barrerasMatriz[nivel].length; c++) {
for (r = 0; r < barrerasMatriz[nivel][c].length; r++) {
let accesoBloque = barrerasMatriz[nivel][c][r];
if (accesoBloque.tipo >= 0) {