-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1430 lines (1339 loc) · 55.5 KB
/
main.py
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
# Mi proyecto de pacman
# Mario Suero
import copy
from board import boards
import pygame
import math
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(f"Proyecto PM/Pacman Music.wav")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.5)
# Configuraciones basicas del juego
# Tamaño de pixeles del juego
width = 900
height = 950
screen = pygame.display.set_mode([width, height])
timer = pygame.time.Clock()
fps = 60
font = pygame.font.Font("freesansbold.ttf", 20)
#Configuracion de pantalla de intro
White = (255, 255, 255)
Black = (0, 0, 0)
Yellow = (255, 255, 0)
font_title = pygame.font.Font(None, 74)
font_button = pygame.font.Font(None, 36)
font_control = pygame.font.Font(None, 28)
text_title = font_title.render("Pac-Man", True, Yellow)
text_button = font_button.render("Iniciar", True, White)
imageninicio = pygame.image.load(f"Proyecto PM/Maquinainicio.jpg")
imageninicio = pygame.transform.scale(imageninicio, (900, 1000))
button_rect = pygame.Rect(width // 2 - 50, height // 2, 100, 50)
control_text = [
"",
"Clickea Iniciar Para Empezar",
"",
"Controles:",
"Flecha Arriba: Mover Hacia Arriba",
"Flecha Abajo: Mover Hacia Abajo",
"Flecha Izquierda: Mover Hacia La Izquierda",
"Flecha Derecha: Mover Hacia La Derecha",
"Espacio: Reiniciar Cuando Pierda O Gane",
"P: Para Pausar El Juego",
"",
"",
"2024 Namco LTD."
]
# Función de pantalla de inicio
def pantalla_inicio():
screen.fill(Black)
while True:
screen.blit(imageninicio, (0, 0))
screen.blit(text_title, (width // 2 - text_title.get_width() // 2, height // 4))
pygame.draw.rect(screen, Yellow, button_rect)
screen.blit(text_button, (button_rect.x + button_rect.width // 2 - text_button.get_width() // 2,
button_rect.y + button_rect.height // 2 - text_button.get_height() // 2))
for i, linea in enumerate(control_text):
text_control = font_control.render(linea, True, Yellow)
screen.blit(text_control, (width // 2 - text_control.get_width() // 2,
button_rect.y + button_rect.height + 30 + i * 30))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
return
pygame.display.flip()
timer.tick(fps)
pantalla_inicio()
level = copy.deepcopy(boards)
color = "blue"
pi = math.pi
player_images = []
for i in range(1, 5):
# Imagenes De Jugador 1 Y Enemigos
player_images.append(pygame.transform.scale(pygame.image.load(f"Proyecto PM/player_images/{i}.png"), (45, 45)))
blinky_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/red.png"), (45, 45))
pinky_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/pink.png"), (45, 45))
inky_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/blue.png"), (45, 45))
clyde_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/orange.png"), (45, 45))
spooked_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/powerup.png"), (45, 45))
dead_img = pygame.transform.scale(pygame.image.load(f"Proyecto PM/ghost_images/dead.png"), (45, 45))
cherry_image = pygame.transform.scale(pygame.image.load(f"Proyecto PM/cherry.png"), (35, 35))
# Coordenadas De Personajes
# Coordenadas Donde Se Encuentra El Jugador Y Los Enemigos
# Jugador 1
player_x = 450
player_y = 663
direction = 0
# Fantasma blinky(Rojo)
blinky_x = 56
blinky_y = 58
blinky_direction = 0
# Fantasma inky (Azul)
inky_x = 440
inky_y = 388
inky_direction = 2
# Fantasma pinky (Rosa)
pinky_x = 440
pinky_y = 438
pinky_direction = 0
# Fantasma clyde (Naranja)
clyde_x = 440
clyde_y = 438
clyde_direction = 0
counter = 0
flicker = False
# Derecha, Arriba, Izquierda, Abajo
turns_allowed = [False, False, False, False]
direction_command = 0
# Velocidad de jugador
player_speed = 3
# Puntos
score = 0
# Powerup, conteo de powerup y fantasmas comidos
power_up = False
powerup_counter = 0
eaten_ghost = [False, False, False, False]
# Objetivos De Los Enemigos (Fantasmas)
targets = [(player_x, player_y), (player_x, player_y), (player_x, player_y), (player_x, player_y)]
# Muertes De Enemigos (Fantasmas)
blinky_dead = False
pinky_dead = False
inky_dead = False
clyde_dead = False
# Fantasmas En La Caja De Inicio
blinky_box = False
pinky_box = False
inky_box = False
clyde_box = False
moving = False
ghost_speeds = [3, 3, 3, 3]
startup_counter = 0
# Vidas Del Pacman (Jugador 1)
lives = 3
game_over = False
game_won = False
#Declarando Clase Para Los Fantasmas
class Ghost:
def __init__(self, x_coord, y_coord, target, speed, img, direct, dead, box, id):
self.x_pos = x_coord
self.y_pos = y_coord
self.center_x = self.x_pos + 22
self.center_y = self.y_pos + 22
self.target = target
self.speed = speed
self.img = img
self.direction = direct
self.dead = dead
self.in_box = box
self.id = id
self.turns, self.in_box = self.check_collisions()
self.rect = self.draw()
# Definiendo La Logica De Los Fantasmas Comidos Y El Efecto Powerup
def draw(self):
if (not power_up and not self.dead) or (eaten_ghost[self.id] and power_up and not self.dead):
screen.blit(self.img, (self.x_pos, self.y_pos))
elif power_up and not self.dead and not eaten_ghost[self.id]:
screen.blit(spooked_img, (self.x_pos, self.y_pos))
else:
screen.blit(dead_img, (self.x_pos, self.y_pos))
ghost_rect = pygame.rect.Rect((self.center_x - 18, self.center_y - 18), (36, 36))
return ghost_rect
#Check Collision Para enemigos (Fantasmas)
#Colision De Fantasmas, Paredes Y Caja
def check_collisions(self):
# Derecha, Arriba, Izquierda, Abajo
num1 = ((height - 50) // 32)
num2 = (width // 30)
num3 = 15
self.turns = [False, False, False, False]
if 0 < self.center_x // 30 < 29:
if level[(self.center_y - num3) // num1][self.center_x // num2] == 9:
self.turns[2] = True
if level[self.center_y // num1][(self.center_x - num3) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x - num3) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[1] = True
if level[self.center_y // num1][(self.center_x + num3) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x + num3) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[0] = True
if level[(self.center_y + num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y + num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[3] = True
if level[(self.center_y - num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y - num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[2] = True
if self.direction == 2 or self.direction == 3:
if 12 <= self.center_x % num2 <= 18:
if level[(self.center_y + num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y + num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[3] = True
if level[(self.center_y - num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y - num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[2] = True
if 12 <= self.center_y % num1 <= 18:
if level[self.center_y // num1][(self.center_x - num2) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x - num2) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[1] = True
if level[self.center_y // num1][(self.center_x + num2) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x + num2) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[0] = True
if self.direction == 0 or self.direction == 1:
if 12 <= self.center_x % num2 <= 18:
if level[(self.center_y + num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y + num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[3] = True
if level[(self.center_y - num3) // num1][self.center_x // num2] < 3 \
or (level[(self.center_y - num3) // num1][self.center_x // num2] == 9 and (
self.in_box or self.dead)):
self.turns[2] = True
if 12 <= self.center_y % num1 <= 18:
if level[self.center_y // num1][(self.center_x - num3) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x - num3) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[1] = True
if level[self.center_y // num1][(self.center_x + num3) // num2] < 3 \
or (level[self.center_y // num1][(self.center_x + num3) // num2] == 9 and (
self.in_box or self.dead)):
self.turns[0] = True
else:
self.turns[0] = True
self.turns[1] = True
if 350 < self.x_pos < 550 and 370 < self.y_pos < 480:
self.in_box = True
else:
self.in_box = False
return self.turns, self.in_box
#Declarando Movimientos O Giros De Clyde (Fantasma Naranja)
def move_clyde(self):
# Derecha, Arriba, Izquierda, Abajo
if self.direction == 0:
if self.target[0] > self.x_pos and self.turns[0]:
self.x_pos += self.speed
elif not self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
else:
self.x_pos += self.speed
elif self.direction == 1:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
elif self.target[0] < self.x_pos and self.turns[1]:
self.x_pos -= self.speed
elif not self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
else:
self.x_pos -= self.speed
elif self.direction == 2:
if self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif not self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
else:
self.y_pos -= self.speed
elif self.direction == 3:
if self.target[1] > self.y_pos and self.turns[3]:
self.y_pos += self.speed
elif not self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
else:
self.y_pos += self.speed
if self.x_pos < -30:
self.x_pos = 900
elif self.x_pos > 900:
self.x_pos - 30
return self.x_pos, self.y_pos, self.direction
#Declarando Movimientos O Giros De Blinky (Fantasma Rojo)
def move_blinky(self):
#Blinky Se Va Moviendo Libre Rodeando Las Paredes Y Continuando Donde No Hay Colision
# Derecha, Arriba, Izquierda, Abajo
if self.direction == 0:
if self.target[0] > self.x_pos and self.turns[0]:
self.x_pos += self.speed
elif not self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
self.x_pos += self.speed
elif self.direction == 1:
if self.target[0] < self.x_pos and self.turns[1]:
self.x_pos -= self.speed
elif not self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
self.x_pos -= self.speed
elif self.direction == 2:
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif not self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[2]:
self.y_pos -= self.speed
elif self.direction == 3:
if self.target[1] > self.y_pos and self.turns[3]:
self.y_pos += self.speed
elif not self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.y_pos += self.speed
if self.x_pos < -30:
self.x_pos = 900
elif self.x_pos > 900:
self.x_pos - 30
return self.x_pos, self.y_pos, self.direction
#Declarando Movimientos O Giros De Clyde (Fantasma naranja)
#Declarando Movimientos O Giros De Inky (Fantasma Azul)
def move_inky(self):
# Derecha, Arriba, Izquierda, Abajo
# Inky Se Movera Libremente Girando Hacia Arriba Y Hacia Abajo Pero Girara Hacia La
# Izquierda Y Derecha Solo, Si Hay Colision
if self.direction == 0:
if self.target[0] > self.x_pos and self.turns[0]:
self.x_pos += self.speed
elif not self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
else:
self.x_pos += self.speed
elif self.direction == 1:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
elif self.target[0] < self.x_pos and self.turns[1]:
self.x_pos -= self.speed
elif not self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
else:
self.x_pos -= self.speed
elif self.direction == 2:
if self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif not self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[2]:
self.y_pos -= self.speed
elif self.direction == 3:
if self.target[1] > self.y_pos and self.turns[3]:
self.y_pos += self.speed
elif not self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
self.y_pos += self.speed
if self.x_pos < -30:
self.x_pos = 900
elif self.x_pos > 900:
self.x_pos - 30
return self.x_pos, self.y_pos, self.direction
#Declarando Movimientos O Giros De Pinky (Fantasma Rosa)
def move_pinky(self):
# Derecha, Arriba, Izquierda, Abajo
# Inky Girara Hacia La Derecha O Izquierda Buscando Ventaja Contra El Jugador Y Solo Girara Hacia
# Arriba O Abajo Cuando Sienta Colision
if self.direction == 0:
if self.target[0] > self.x_pos and self.turns[0]:
self.x_pos += self.speed
elif not self.turns[0]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
self.x_pos += self.speed
elif self.direction == 1:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
elif self.target[0] < self.x_pos and self.turns[1]:
self.x_pos -= self.speed
elif not self.turns[1]:
if self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[1]:
self.x_pos -= self.speed
elif self.direction == 2:
if self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif not self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] > self.y_pos and self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[3]:
self.direction = 3
self.y_pos += self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[2]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
else:
self.y_pos -= self.speed
elif self.direction == 3:
if self.target[1] > self.y_pos and self.turns[3]:
self.y_pos += self.speed
elif not self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.target[1] < self.y_pos and self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[2]:
self.direction = 2
self.y_pos -= self.speed
elif self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
elif self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.turns[3]:
if self.target[0] > self.x_pos and self.turns[0]:
self.direction = 0
self.x_pos += self.speed
elif self.target[0] < self.x_pos and self.turns[1]:
self.direction = 1
self.x_pos -= self.speed
else:
self.y_pos += self.speed
if self.x_pos < -30:
self.x_pos = 900
elif self.x_pos > 900:
self.x_pos - 30
return self.x_pos, self.y_pos, self.direction
#Configuracion al ganar o perder
def draw_misc():
score_text = font.render(f"Puntos: {score}", True, "white" )
screen.blit(score_text, (10, 920))
if power_up:
screen.blit(cherry_image, (135, 915))
for i in range(lives):
screen.blit(pygame.transform.scale(player_images[0], (30, 30)), (650 + i * 40, 915))
jugador_text = font.render(f"1UP",True, "white")
screen.blit(jugador_text, (780, 920))
vidas_text = font.render(f"Vidas:",True, "white")
screen.blit(vidas_text, (580, 920))
if game_over:
pygame.draw.rect(screen, "red", [50, 200, 800, 300], 10, 20)
pygame.draw.rect(screen, "dark gray", [70, 220, 760, 260], 10, 20)
game_over_text = font.render("Game Over, Mala Suerte! Espacio Para Reiniciar", True, "white")
screen.blit(game_over_text, (100, 300))
if game_won:
pygame.draw.rect(screen, "green", [50, 200, 800, 300], 10, 20)
pygame.draw.rect(screen, "dark gray", [70, 220, 760, 260], 10, 20)
game_won_text = font.render("Felicidades, Has Ganado! Espacio Para Reiniciar", True, "white")
screen.blit(game_won_text, (100, 300))
#Reset al ganar o perder
def reset_game():
global score,lives,game_over,game_won,paused,level,player_x,player_y,direction,blinky_x,blinky_y,blinky_direction,inky_x,inky_y,inky_direction,pinky_x,pinky_y,pinky_direction,clyde_x,clyde_y,clyde_direction,startup_counter
lives = 3
score = 0
game_over = False
game_won = False
paused = False
level = copy.deepcopy(boards)
player_x = 450
player_y = 663
direction = 0
blinky_x = 56
blinky_y = 58
blinky_direction = 0
inky_x = 440
inky_y = 388
inky_direction = 2
pinky_x = 440
pinky_y = 438
pinky_direction = 0
clyde_x = 440
clyde_y = 438
clyde_direction = 0
startup_counter = 0
#Check collision para jugador
#Puntos y colision de bolitas que dan puntos tambien
def check_collisions(Score, powerup, powerup_count, eaten_ghosts):
num1 = (height - 50) // 32
num2 = width//30
if 0 < player_x < 870:
if level[center_y // num1][center_x // num2] == 1:
level[center_y // num1][center_x // num2] = 0
#valor de bolitas pequeñas
Score += 10
if level[center_y // num1][center_x // num2] == 2:
level[center_y // num1][center_x // num2] = 0
#valor de bolitas grandes
Score += 50
#powerup y valor del powerup
powerup = True
powerup_count = 0
#Fantasmas comidos
eaten_ghosts = [False, False, False, False]
return Score, powerup, powerup_count, eaten_ghosts
#Diseño de nivel
def draw_board(lvl):
num1 = ((height - 50) // 32)
num2 = (width // 30)
for i in range(len(lvl)):
for j in range(len(level[i])):
if level[i][j] == 1:
pygame.draw.circle(screen, "white", (j * num2 + (0.5*num2), i * num1 + (0.5 * num1)), 4)
if level[i][j] == 2 and not flicker:
pygame.draw.circle(screen, "white", (j * num2 + (0.5*num2), i * num1 + (0.5 * num1)), 10)
if level[i][j] == 3:
pygame.draw.line(screen, color, (j * num2 + (0.5* num2), i*num1),
(j * num2 + (0.5 * num2), i*num1+ num1), 3)
if level[i][j] == 4:
pygame.draw.line(screen, color, (j * num2, i*num1 + (0.5*num1)),
(j * num2 + num2, i*num1 + (0.5*num1)), 3)
if level[i][j] == 5:
pygame.draw.arc(screen, color, [(j*num2 - (num2*0.4)) - 2, (i * num1 + (0.5*num1)), num2, num1], 0, pi/2, 3)
if level[i][j] == 6:
pygame.draw.arc(screen, color,
[(j * num2 + (num2 * 0.5)), (i * num1 + (0.5 * num1)), num2, num1], pi/2, pi, 3)
if level[i][j] == 7:
pygame.draw.arc(screen, color, [(j*num2 + (num2*0.5)), (i * num1 - (0.4*num1)), num2, num1], pi, 3*pi/2, 3)
if level[i][j] == 8:
pygame.draw.arc(screen, color,
[(j * num2 - (num2 * 0.4)) - 2, (i * num1 - (0.4 * num1)), num2, num1], 3*pi/2, 2*pi, 3)
if level[i][j] == 9:
pygame.draw.line(screen, "white",(j * num2, i*num1 + (0.5*num1)),
(j * num2 + num2, i*num1 + (0.5*num1)), 3)
#Diseño de jugador
def draw_player():
# 0- Derecha, 1- izquierda, 2- arriba, 3- abajo
if direction == 0:
screen.blit(player_images[counter // 5], (player_x, player_y))
elif direction == 1:
screen.blit(pygame.transform.flip(player_images[counter // 5],True, False), (player_x, player_y))
elif direction == 2:
screen.blit(pygame.transform.rotate(player_images[counter // 5], 90,), (player_x, player_y))
elif direction == 3:
screen.blit(pygame.transform.rotate(player_images[counter // 5], 270), (player_x, player_y))
#Validaciones de posiciones y giros
def check_position(centerx, centery):
turns = [False, False, False, False]
num1 = (height - 50)//32
num2 = (width // 30)
num3 = 15
# revisar colision basada en centro de x y centro del jugador +/- numero falso
if centerx // 30 < 29:
if direction == 0:
if level[centery // num1][(centerx - num3) // num2] < 3:
turns[1] = True
if direction == 1:
if level[centery // num1][(centerx + num3) // num2] < 3:
turns[0] = True
if direction == 2:
if level[(centery + num3) //num1][centerx // num2] < 3:
turns[3] = True
if direction == 3:
if level[(centery - num3) // num1][(centerx // num2)] < 3:
turns[2] = True
if direction == 2 or direction == 3:
if 12 <= centerx % num2 <= 18:
if level[(centery + num3) // num1][centerx // num2] < 3:
turns[3] = True
if level[(centery - num3) // num1][centerx // num2] < 3:
turns[2] = True
if 12 <= centery % num1 <= 18:
if level[centery // num1][(centerx - num2) // num2] < 3:
turns[1] = True
if level[centery // num1][(centerx + num2) // num2] < 3:
turns[0] = True
if direction == 0 or direction == 1:
if 12 <= centerx % num2 <= 18:
if level[(centery + num1) // num1][centerx // num2] < 3:
turns[3] = True
if level[(centery - num1) // num1][centerx // num2] < 3:
turns[2] = True
if 12 <= centery % num1 <= 18:
if level[centery // num1][(centerx - num3) // num2] < 3:
turns[1] = True
if level[centery // num1][(centerx + num3) // num2] < 3:
turns[0] = True
else:
turns[0] = True
turns[1] = True
return turns
# Movimiento de jugador uno (Pacman)
def move_player(play_x, play_y):
# 0 - derecha, 1 - izquierda, 2 - arriba, 3 - abajo
if direction == 0 and turns_allowed[0]:
play_x += player_speed
elif direction == 1 and turns_allowed[1]:
play_x -= player_speed
if direction == 2 and turns_allowed[2]:
play_y -= player_speed
elif direction == 3 and turns_allowed[3]:
play_y += player_speed
return play_x, play_y
# Movimiento de cada fantasma y cual zona debe frecuentar
def get_targets(blink_x, blink_y, ink_x, ink_y, pink_x, pink_y, clyd_x, clyd_y):
if player_x < 450:
runaway_x = 900
else:
runaway_x = 0
if player_y < 450:
runaway_y = 900
else:
runaway_y = 0
return_target = (380, 400)
if power_up:
if not blinky.dead and not eaten_ghost[0]:
blink_target = (runaway_x, runaway_y)
elif not blinky.dead and eaten_ghost[0]:
if 340 < blink_x < 560 and 340 < blink_y < 500:
blink_target = (400, 100)
else:
blink_target = (player_x, player_y)
else:
blink_target = return_target
if not inky.dead and not eaten_ghost[1]:
ink_target = (runaway_x, player_y)
elif not inky.dead and eaten_ghost[1]:
if 340 < ink_x < 560 and 340 < ink_y < 500:
ink_target = (400, 100)
else:
ink_target = (player_x, player_y)
else:
ink_target = return_target
if not pinky.dead:
pink_target = (player_x, runaway_y)
elif not pinky.dead and eaten_ghost[2]:
if 340 < pink_x < 560 and 340 < pink_y < 500:
pink_target = (400, 100)
else:
pink_target = (player_x, player_y)
else:
pink_target = return_target
if not clyde.dead and not eaten_ghost[3]:
clyd_target = (450, 450)
elif not clyde.dead and eaten_ghost[3]:
if 340 < clyd_x < 560 and 340 < clyd_y < 500:
clyd_target = (400, 100)
else:
clyd_target = (player_x, player_y)
else:
clyd_target = return_target
else:
if not blinky.dead:
if 340 < blink_x < 560 and 340 < blink_y < 500:
blink_target = (400, 100)
else: