-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacman.py
1524 lines (1431 loc) · 67.7 KB
/
pacman.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
# Build Pac-Man from Scratch in Python with PyGame!!
import copy
from board import boards, test_board
from generate import print_tree
import pygame
import math
from trees import Story, Tree
from splitImage import split_image
from start_screen import change_screen_win_screen
pygame.init()
SPLIT_COUNT = 3
class Shard():
# num_found = 0
# use 3 - SPLIT_COUNT instaed
def __init__(self, sprite, image, split, caption, story, black):
self.sprite = sprite
self.image = image
self.split = split # split in ["left", "right", "up", "down"]
self.caption = caption
self.story = story
self.black = black
def render_text(surface, text, pos, font, color, max_width):
"""
Render text on the given surface with word wrapping.
:param surface: Pygame surface where text will be drawn.
:param text: The text to be rendered.
:param pos: A tuple (x, y) where the text begins.
:param font: Pygame font object used for rendering text.
:param color: Color of the text.
:param max_width: Maximum width in pixels for text lines.
"""
words = text.split()
space = 10 # Width of a space.
max_height = font.get_height()
x, y = pos
line = []
for word in words:
# Check width of the line with the new word added
line_width, _ = font.size(' '.join(line + [word]))
if line_width <= max_width:
line.append(word)
else:
# Draw the line and start a new one
surface.blit(font.render(' '.join(line), True, color), (x, y))
y += max_height # Move to the next line
line = [word]
if line:
surface.blit(font.render(' '.join(line), True, color), (x, y))
def draw_images(shard_list,screen, width, height):
global SPLIT_COUNT
half_width = screen.get_width() // 2
image_height = screen.get_height()
image1 = shard_list[0].image
image2 = shard_list[1].image
image3 = shard_list[2].image
image4 = shard_list[3].image
black = pygame.image.load('./black.png').convert_alpha()
# black_image = pygame.transform.scale(black_image, (half_width//1.15, image_height//2.25))
image1 = pygame.transform.scale(image1, (200, 200)) if SPLIT_COUNT <= 2 else black
image2 = pygame.transform.scale(image2, (200, 200)) if SPLIT_COUNT <= 1 else black
image3 = pygame.transform.scale(image3, (200, 200)) if SPLIT_COUNT <= 0 else black
image4 = pygame.transform.scale(image4, (200, 200)) if SPLIT_COUNT < 0 else black
screen.blit(image1, (0, 200))
screen.blit(image2, (0 + image1.get_width(), 200))
screen.blit(image3, (0, 200 + image1.get_height()))
screen.blit(image4, (0 + image1.get_width(), 200 + image1.get_height()))
def change_screen_shard_collected(screen, width, height, shard_list):
global SPLIT_COUNT
screen = pygame.display.set_mode([width, height])
image = shard_list[SPLIT_COUNT].image
# black = shard_list[SPLIT_COUNT].black
total_story = shard_list[SPLIT_COUNT].story
total_story = total_story.split('. ')
curr_caption = shard_list[SPLIT_COUNT].caption
story_level = ". ".join(total_story[:3 - SPLIT_COUNT + 1])
SPLIT_COUNT -= 1
cont = True
while cont:
for event in pygame.event.get():
if event.type == pygame.QUIT:
cont = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
cont = False
half_width = screen.get_width() // 2
image_height = screen.get_height()
draw_images(shard_list, screen, width, height)
# black_image = pygame.transform.scale(black_image, (half_width//1.15, image_height//2.25))
# image = pygame.transform.scale(image, (half_width//1.15, image_height//2.25))
# screen.blit(image, (0, 200))
# screen.blit(black_image,(0,200))
#screen.blit(curr_caption,(half_width,0))
text = pygame.font.SysFont('Arial', 20).render(curr_caption, True, (255,255,255))
text_rect = text.get_rect(center=(650,250))
screen.blit(text, text_rect)
render_text(screen,story_level,(half_width,image_height//2),pygame.font.SysFont('Arial', 28),"white",half_width)
# render_text(screen,curr_caption,(half_width,image_height),pygame.font.SysFont('Arial', 28),"white",half_width)
pygame.display.update()
return None
def change_screen_level_change(screen, width, height, shard_list):
global SPLIT_COUNT
screen = pygame.display.set_mode([width, height])
image = shard_list[SPLIT_COUNT].image
total_story = shard_list[SPLIT_COUNT].story
total_story = total_story.split('. ')
curr_caption = shard_list[SPLIT_COUNT].caption
story_level = ". ".join(total_story[:3 - SPLIT_COUNT + 1])
SPLIT_COUNT -= 1
cont = True
while cont:
for event in pygame.event.get():
if event.type == pygame.QUIT:
cont = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
cont = False
half_width = screen.get_width() // 2
image_height = screen.get_height()
image = pygame.transform.scale(image, (half_width//1.15, image_height//2.25))
screen.blit(image, (0, 200))
# #screen.blit(curr_caption,(half_width,0))
# text = pygame.font.SysFont('Arial', 20).render(curr_caption, True, (255,255,255))
# text_rect = text.get_rect(center=(650,250))
# screen.blit(text, text_rect)
# render_text(screen,story_level,(half_width,image_height//2),pygame.font.SysFont('Arial', 28),"white",half_width)
pygame.display.update()
return None
# def change_screen_shard_collected(screen, width, height, shard_list):
# global SPLIT_COUNT
# screen = pygame.display.set_mode([width, height])
# image = shard_list[SPLIT_COUNT].image
# total_story = shard_list[SPLIT_COUNT].story
# total_story = total_story.split('. ')
# curr_caption = shard_list[SPLIT_COUNT].caption
# story_level = total_story[:3 - SPLIT_COUNT + 1]
# SPLIT_COUNT -= 1
# font = pygame.font.Font('freesansbold.ttf', 20)
# text_color = (255, 255, 255) # White color
# # Function to split the caption into multiple lines
# def split_caption(caption, line_width):
# words = caption.split(' ')
# lines = []
# current_line = ''
# for word in words:
# if font.size(current_line + word)[0] <= line_width:
# current_line += word + ' '
# else:
# lines.append(current_line)
# current_line = word + ' '
# lines.append(current_line)
# return lines
# caption_lines = split_caption(curr_caption, 880) # Assuming 880 pixels wide text box
# cont = True
# while cont:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# cont = False
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_r:
# cont = False
# screen.blit(image, (0, 0))
# pygame.draw.rect(screen, (255, 255, 255), (0, 0, 100, 50), 3)
# text = font.render('Return', True, text_color)
# screen.blit(text, (10, 10))
# # Create text box for story
# pygame.draw.rect(screen, (0, 0, 0), (0, 600, 900, 350))
# pygame.draw.rect(screen, (255, 255, 255), (0, 600, 900, 350), 3)
# # Display each line of the caption
# line_height = font.get_height()
# for i, line in enumerate(caption_lines.join("")):
# text = font.render(line, True, text_color)
# screen.blit(text, (10, 610 + i * line_height))
# pygame.display.update()
# return None
def run_game(root, tree_level, victory_tracker, board = None):
WIDTH = 900
HEIGHT = 950
screen = pygame.display.set_mode([WIDTH, HEIGHT])
timer = pygame.time.Clock()
fps = 60
font = pygame.font.Font('freesansbold.ttf', 20)
level = test_board if board is None else board
color = 'blue'
PI = math.pi
player_images = []
for i in range(1, 5):
player_images.append(pygame.transform.scale(pygame.image.load(f'assets/player_images/{i}.png'), (45, 45)))
blinky_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/red.png'), (45, 45))
pinky_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/pink.png'), (45, 45))
inky_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/blue.png'), (45, 45))
clyde_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/orange.png'), (45, 45))
spooked_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/powerup.png'), (45, 45))
dead_img = pygame.transform.scale(pygame.image.load(f'assets/ghost_images/dead.png'), (45, 45))
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 = 2
clyde_x = 440
clyde_y = 438
clyde_direction = 2
counter = 0
lost_level = False
flicker = False
# R, L, U, D
turns_allowed = [False, False, False, False]
direction_command = 0
player_speed = 2
score = [0]
powerup = False
power_counter = 0
eaten_ghost = [False, False, False, False]
targets = [(player_x, player_y), (player_x, player_y), (player_x, player_y), (player_x, player_y)]
blinky_dead = False
inky_dead = False
clyde_dead = False
pinky_dead = False
blinky_box = False
inky_box = False
clyde_box = False
pinky_box = False
moving = False
ghost_speeds = [2, 2, 2, 2]
startup_counter = 0
lives = 3
game_over = False
game_won = False
caption = ''
story = ''
if tree_level == 0:
caption = root.caption
story = root.val
elif tree_level == 1:
pass
elif tree_level == 2:
pass
img_pp = pygame.image.load("photo_piece.png").convert_alpha()
img_pp = pygame.transform.scale(img_pp, (30,30))
# img_ppb = pygame.image.load("A_black_image.png").convert_alpha()
# img_ppb = pygame.transform.scale(img_ppb, (30,30))
split_image(root.image, './images', 'testingShard') #root.image
# split_image(root.black, './images', 'blackShard')
#split_image(image_path, destination_path, name
img_pp1 = pygame.image.load('./images/testingShard/top_left.jpg').convert_alpha()
img_pp2 = pygame.image.load('./images/testingShard/top_right.jpg').convert_alpha()
img_pp3 = pygame.image.load('./images/testingShard/bottom_left.jpg').convert_alpha()
img_pp4 = pygame.image.load('./images/testingShard/bottom_right.jpg').convert_alpha()
# img_ppb1 = pygame.image.load('./images/blackShard/top_left.jpg').convert_alpha()
# img_ppb2 = pygame.image.load('./images/blackShard/top_right.jpg').convert_alpha()
# img_ppb3 = pygame.image.load('./images/blackShard/bottom_left.jpg').convert_alpha()
# img_ppb4 = pygame.image.load('./images/blackShard/top_left.jpg').convert_alpha()
# shard_list = [img_pp1, img_pp2, img_pp3, img_pp4]
# if level is not 0, traverse using victory_tracker to find correct image/story
shard1 = Shard(sprite=None, image=img_pp1, split='left', caption=caption, story=story, black='')
shard2 = Shard(sprite=None, image=img_pp2, split='right', caption=caption, story=story, black='')
shard3 = Shard(sprite=None, image=img_pp3, split='up', caption=caption, story=story,black='')
shard4 = Shard(sprite=None, image=img_pp4, split='down', caption=caption, story=story,black='')
shard_list = [shard1, shard2, shard3, shard4]
mute = False
shards = [0]
# Each Ghost's class, each ghost has its own movement, this can be changed to just one movement way
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()
def draw(self):
if (not powerup and not self.dead) or (eaten_ghost[self.id] and powerup and not self.dead):
screen.blit(self.img, (self.x_pos, self.y_pos))
elif powerup 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
def check_collisions(self):
# R, L, U, D
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] == 10 \
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] == 10 \
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] == 10\
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] == 10\
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 // num1][(self.center_x - num3) // num2] == 10\
or ((level[self.center_y // num1][(self.center_x - num3) // 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 // num1][(self.center_x + num3) // num2] == 10\
or ((level[self.center_y // num1][(self.center_x + num3) // 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 + num3) // num1][self.center_x // num2] == 10\
or ((level[(self.center_y + num3) // num1][self.center_x // 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 - num3) // num1][self.center_x // num2] == 10\
or ((level[(self.center_y - num3) // num1][self.center_x // 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 // num1][(self.center_x - num3) // num2] == 10\
or ((level[self.center_y // num1][(self.center_x - num3) // 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 // num1][(self.center_x + num3) // num2] == 10\
or ((level[self.center_y // num1][(self.center_x + num3) // 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 + num3) // num1][self.center_x // num2] == 10\
or ((level[(self.center_y + num3) // num1][self.center_x // 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 - num3) // num1][self.center_x // num2] == 10\
or ((level[(self.center_y - num3) // num1][self.center_x // 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
def move_clyde(self):
# r, l, u, d
# clyde is going to turn whenever advantageous for pursuit
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
def move_blinky(self):
# r, l, u, d
# blinky is going to turn whenever colliding with walls, otherwise continue straight
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
def move_inky(self):
# r, l, u, d
# inky turns up or down at any point to pursue, but left and right only on collision
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
def move_pinky(self):
# r, l, u, d
# inky is going to turn left or right whenever advantageous, but only up or down on collision
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
# Drawing scores
def draw_misc():
nonlocal score
nonlocal shards
font = pygame.font.Font('freesansbold.ttf', 25)
score_text = font.render(f'Score: {score[0]}', True, 'white')
shards_text = font.render(f'Shards: {shards[0]} / 4', True, 'white')
screen.blit(score_text, (WIDTH // 2.26, HEIGHT // 2.35))
screen.blit(shards_text, (WIDTH // 2.4, HEIGHT // 2.15))
if powerup:
pygame.draw.circle(screen, 'blue', (140, 930), 15)
for i in range(lives):
screen.blit(pygame.transform.scale(player_images[0], (30, 30)), (650 + i * 40, 915))
if game_over:
pygame.draw.rect(screen, 'white', [50, 200, 800, 300],0, 10)
pygame.draw.rect(screen, 'dark gray', [70, 220, 760, 260], 0, 10)
gameover_text = font.render('Game over! Space bar to restart!', True, 'red')
screen.blit(gameover_text, (100, 300))
if game_won:
pygame.draw.rect(screen, 'white', [50, 200, 800, 300],0, 10)
pygame.draw.rect(screen, 'dark gray', [70, 220, 760, 260], 0, 10)
gameover_text = font.render('Victory! Space bar to restart!', True, 'green')
screen.blit(gameover_text, (100, 300))
# Checking for collisions
def check_collisions(scor, power, power_count, eaten_ghosts):
nonlocal shards
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
scor[0] += 10
if level[center_y // num1][center_x // num2] == 2:
level[center_y // num1][center_x // num2] = 0
scor[0] += 50
power = True
power_count = 0
eaten_ghosts = [False, False, False, False]
if level[center_y // num1][center_x // num2] == 10:
level[center_y // num1][center_x // num2] = 0
shards[0] += 1
change_screen_shard_collected(screen, WIDTH, HEIGHT, shard_list)
return scor, power, power_count, eaten_ghosts
# Draw the board from board.py
def draw_board():
num1 = ((HEIGHT - 50) // 32)
num2 = (WIDTH // 30)
for i in range(len(level)):
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)
if level[i][j] == 10:
# pygame.draw.circle(screen, 'aqua', (j * num2 + (0.5 * num2), i * num1 + (0.5 * num1)), 10)
screen.blit(img_pp, (j * num2, i * num1))
def draw_player():
# 0-RIGHT, 1-LEFT, 2-UP, 3-DOWN
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))