-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost.py
675 lines (536 loc) · 21.6 KB
/
ghost.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
import pygame as pg
from timer import *
import random
class Ghost:
def __init__(self, screen, graph, settings, pacman):
self.screen = screen
self.graph = graph
self.x = 0
self.y = 0
self.onNode = False
self.node = None
self.direction = "STOP"
self.move_speed = 0.5
self.spritesheet = Spritesheet("./assets/images/pacman-spritesheet.png")
self.images = {"LEFT": None, "RIGHT": None, "DOWN": None, "UP": None}
self.dying_images = self.spritesheet.images_at(
[(161, 65, 30, 30), (161, 97, 30, 30)]
)
self.timer = Timer(self.dying_images)
self.dead_images = {
"LEFT": None,
"RIGHT": None,
"DOWN": None,
"UP": None,
"STOP": None,
}
self.setDeadImages()
self.settings = settings
self.pacman = pacman
self.spawn = False
self.dying = False
self.dying_time = 0
self.dead = False
self.scores = [200, 400, 800, 1600]
self.font = pg.font.Font(f"./assets/fonts/PressStart2P-Regular.ttf", 15)
self.multiplied = False
self.settings.index = 0
self.score_checked = False
self.score = "0"
self.scoreFromDeath = 0
self.death_frame = 0
def setDeadImages(self):
self.dead_images["LEFT"] = self.spritesheet.image_at((129, 129, 32, 32))
self.dead_images["RIGHT"] = self.spritesheet.image_at((129, 161, 32, 32))
self.dead_images["DOWN"] = self.spritesheet.image_at((129, 97, 32, 32))
self.dead_images["UP"] = self.spritesheet.image_at((129, 65, 32, 32))
self.dead_images["STOP"] = self.spritesheet.image_at((129, 129, 32, 32))
def dyingAndEaten(self, framerate):
if (
self.x > self.pacman.x - self.pacman.radius
and self.x < self.pacman.x + self.pacman.radius
) and (
self.y > self.pacman.y - self.pacman.radius
and self.y < self.pacman.y + self.pacman.radius
):
if self.dying and not self.dead:
self.move_speed = 0.5
self.death_frame = framerate
self.dead = True
if not self.dying:
self.pacman.dead = True
def dyingModeTimer(self, time):
if self.dying_time + 2200 > time:
self.image = self.dying_images[0]
if self.dead:
self.move_speed = 0.5
else:
self.move_speed = 0.25
if self.dying_time + 2200 <= time and self.dying_time + 3000 > time:
self.image = self.timer.image()
if self.dying_time + 3000 == time:
self.dying = False
if self.dead:
self.move_speed = 0.5
else:
self.move_speed = 0.5
self.score = 200
self.settings.pellets.dyingSound = False
def update_node(self):
for node in self.graph.nodes:
if node.x == self.x and node.y == self.y and not self.onNode:
self.node = node
self.onNode = True
break
else:
self.onNode = False
def change_direction(self):
if self.onNode:
self.direction = self.target_direction
def move(self, framerate):
self.update_node()
self.dyingAndEaten(framerate)
if self.direction == "UP" and self.node.neighbors["UP"] is not None:
self.y += self.move_speed * -1
self.target = self.node.neighbors["UP"]
if self.direction == "DOWN" and self.node.neighbors["DOWN"] is not None:
self.y += self.move_speed * 1
self.target = self.node.neighbors["DOWN"]
if self.direction == "LEFT" and self.node.neighbors["LEFT"] is not None:
self.x += self.move_speed * -1
self.target = self.node.neighbors["LEFT"]
if self.direction == "RIGHT" and self.node.neighbors["RIGHT"] is not None:
self.x += self.move_speed * 1
self.target = self.node.neighbors["RIGHT"]
def incrementScore(self):
if self.dying and self.dead and not self.multiplied:
self.settings.score += self.scores[self.settings.index]
self.settings.index += 1
self.multiplied = True
if not self.dying:
self.settings.index = 0
self.multiplied = False
self.score_checked = False
# def drawDead(self, framerate):
# if not self.score_checked:
# self.score = str(self.scores[self.settings.index])
# self.image_x = self.x
# self.image_y = self.y
# self.score_checked = True
#
# if self.death_frame + 1000:
# self.dead = False
#
# text = self.font.render(self.score, True, (255, 255, 255))
# text_rec = text.get_rect(center=(self.image_x, self.image_y))
# self.screen.blit(text, text_rec)
#
# self.incrementScore()
def goHome(self):
self.drawDead()
if not self.onNode:
return
x_diff = abs(self.x - 425)
y_diff = abs(self.y - 350)
if x_diff > y_diff:
if self.x < 425 and self.node.neighbors["RIGHT"] is not None:
self.direction = "RIGHT"
self.y = self.node.neighbors["RIGHT"].y
elif self.x > 425 and self.node.neighbors["LEFT"] is not None:
self.direction = "LEFT"
self.y = self.node.neighbors["LEFT"].y
elif self.node.neighbors["UP"] is not None:
self.direction = "UP"
self.x = self.node.neighbors["UP"].x
elif self.node.neighbors["DOWN"] is not None:
self.direction = "DOWN"
self.x = self.node.neighbors["DOWN"].x
elif y_diff > x_diff:
if self.y < 350 and self.node.neighbors["DOWN"] is not None:
self.direction = "DOWN"
self.x = self.node.neighbors["DOWN"].x
elif self.y > 350 and self.node.neighbors["UP"] is not None:
self.direction = "UP"
self.x = self.node.neighbors["UP"].x
elif self.node.neighbors["RIGHT"] is not None:
self.direction = "RIGHT"
self.y = self.node.neighbors["RIGHT"].y
elif self.node.neighbors["LEFT"] is not None:
self.direction = "LEFT"
self.y = self.node.neighbors["LEFT"].y
elif self.x == 425 and self.y == 350:
self.direction == "STOP"
class Blinky(Ghost):
def __init__(self, screen, graph, settings, pacman):
super().__init__(screen, graph, settings, pacman)
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = self.node.x
self.y = self.node.y
self.goal = self.pacman.target
self.direction_change = True
self.setImages()
self.image = self.images["LEFT"]
def checkOppositeDirection(self, direction):
if not self.direction_change:
return False
if direction == "LEFT" and self.direction == "RIGHT":
return False
if direction == "RIGHT" and self.direction == "LEFT":
return False
if direction == "DOWN" and self.direction == "UP":
return False
if direction == "UP" and self.direction == "DOWN":
return False
return True
def search(self, framerate):
if not self.onNode:
return
if self.dead:
return
directions = ["LEFT", "RIGHT", "UP", "DOWN"]
number = random.randrange(0, 4)
if self.node.neighbors[directions[number]]:
if self.checkOppositeDirection(directions[number]):
self.direction = directions[number]
self.direction_change = False
self.frame_change = framerate
if directions[number] == "DOWN" or directions[number] == "UP":
self.x = self.node.neighbors[directions[number]].x
elif directions[number] == "RIGHT" or directions[number] == "LEFT":
self.y = self.node.neighbors[directions[number]].y
else:
self.search(framerate)
def teleport_node(self):
if self.x < 1 and self.direction == "LEFT":
self.x = self.graph.node_right.x
self.node = self.graph.node_right
if self.x > 845 and self.direction == "RIGHT":
self.x = self.graph.node_left.x
self.node = self.graph.node_left
def canChangeDirection(self, framerate):
if self.direction_change == False:
if self.frame_change + 10 == framerate:
self.direction_change = True
def setImages(self):
self.images["LEFT"] = self.spritesheet.image_at((0, 129, 32, 32))
self.images["RIGHT"] = self.spritesheet.image_at((0, 161, 32, 32))
self.images["DOWN"] = self.spritesheet.image_at((0, 97, 32, 32))
self.images["UP"] = self.spritesheet.image_at((0, 65, 32, 32))
def draw(self, time):
if self.dying and not self.dead:
self.dyingModeTimer(time)
elif self.direction == "LEFT":
self.image = self.images["LEFT"]
elif self.direction == "RIGHT":
self.image = self.images["RIGHT"]
elif self.direction == "DOWN":
self.image = self.images["DOWN"]
elif self.direction == "UP":
self.image = self.images["UP"]
rect = self.image.get_rect()
rect.left = self.x - 16
rect.top = self.y - 16
self.screen.blit(self.image, rect)
def reset(self):
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = self.node.x
self.y = self.node.y
self.goal = self.pacman.target
self.direction_change = True
self.direction = "STOP"
self.dying = False
def drawDead(self, framerate):
if not self.score_checked:
self.score = str(self.scores[self.settings.index])
self.image_x = self.x
self.image_y = self.y
self.score_checked = True
if self.death_frame + 1000 == framerate:
self.reset()
self.dead = False
text = self.font.render(self.score, True, (255, 255, 255))
text_rec = text.get_rect(center=(self.image_x, self.image_y))
self.screen.blit(text, text_rec)
self.incrementScore()
class Pinky(Ghost):
def __init__(self, screen, graph, settings, pacman):
super().__init__(screen, graph, settings, pacman)
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = 365
self.y = 460
self.setImages()
self.image = self.images["LEFT"]
def setImages(self):
self.images["LEFT"] = self.spritesheet.image_at((33, 129, 32, 32))
self.images["RIGHT"] = self.spritesheet.image_at((33, 161, 32, 32))
self.images["DOWN"] = self.spritesheet.image_at((33, 97, 32, 32))
self.images["UP"] = self.spritesheet.image_at((33, 65, 32, 32))
def spawnToNode(self):
if self.pacman.score_from_death >= 100 and self.spawn == False:
if self.y > 420:
self.direction = "UP"
self.y -= self.move_speed
elif self.x < 375:
self.direction = "RIGHT"
self.x += self.move_speed
elif self.x == 425 and self.y > 350:
self.direction = "UP"
self.y -= self.move_speed
if self.x == self.node.x and self.y == self.node.y:
self.spawn = True
def reset(self):
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = 365
self.y = 460
self.image = self.images["LEFT"]
self.spawn = False
self.direction = "STOP"
self.dying = False
def moveAround(self):
if self.dead:
return
self.spawnToNode()
if (self.x, self.y) == (425, 350):
self.direction = "LEFT"
if (self.x, self.y) == (287, 350):
self.direction = "DOWN"
if (self.x, self.y) == (287, 435):
self.direction = "LEFT"
if (self.x, self.y) == (200, 435):
self.direction = "DOWN"
if (self.x, self.y) == (200, 620):
self.direction = "LEFT"
if (self.x, self.y) == (50, 620):
self.direction = "DOWN"
if (self.x, self.y) == (50, 710):
self.direction = "RIGHT"
if (self.x, self.y) == (105, 710):
self.direction = "DOWN"
if (self.x, self.y) == (105, 800):
self.direction = "LEFT"
if (self.x, self.y) == (50, 800):
self.direction = "DOWN"
if (self.x, self.y) == (50, 890):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 890):
self.direction = "UP"
if (self.x, self.y) == (800, 800):
self.direction = "LEFT"
if (self.x, self.y) == (745, 800):
self.direction = "UP"
if (self.x, self.y) == (745, 710):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 710):
self.direction = "UP"
if (self.x, self.y) == (800, 620):
self.direction = "LEFT"
if (self.x, self.y) == (650, 620):
self.direction = "UP"
if (self.x, self.y) == (650, 255):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 255):
self.direction = "UP"
if (self.x, self.y) == (800, 50):
self.direction = "LEFT"
if (self.x, self.y) == (475, 50):
self.direction = "DOWN"
if (self.x, self.y) == (475, 165):
self.direction = "LEFT"
if (self.x, self.y) == (375, 165):
self.direction = "UP"
if (self.x, self.y) == (375, 50):
self.direction = "LEFT"
if (self.x, self.y) == (50, 50):
self.direction = "DOWN"
if (self.x, self.y) == (50, 255):
self.direction = "RIGHT"
if (self.x, self.y) == (200, 255):
self.direction = "DOWN"
def draw(self, time):
if self.dying and not self.dead:
self.dyingModeTimer(time)
elif self.direction == "LEFT":
self.image = self.images["LEFT"]
elif self.direction == "RIGHT":
self.image = self.images["RIGHT"]
elif self.direction == "DOWN":
self.image = self.images["DOWN"]
elif self.direction == "UP":
self.image = self.images["UP"]
rect = self.image.get_rect()
rect.left = self.x - 16
rect.top = self.y - 16
self.screen.blit(self.image, rect)
def drawDead(self, framerate):
if not self.score_checked:
self.score = str(self.scores[self.settings.index])
self.image_x = self.x
self.image_y = self.y
self.score_checked = True
if self.death_frame + 1000 == framerate:
self.reset()
self.dead = False
text = self.font.render(self.score, True, (255, 255, 255))
text_rec = text.get_rect(center=(self.image_x, self.image_y))
self.screen.blit(text, text_rec)
self.incrementScore()
class Inkey(Ghost):
def __init__(self, screen, graph, settings, pacman):
super().__init__(screen, graph, settings, pacman)
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = 425
self.y = 460
self.setImages()
self.image = self.images["UP"]
def spawnToNode(self):
if self.pacman.score_from_death >= 50 and self.spawn == False:
if self.y > self.node.y:
self.direction = "UP"
self.y -= self.move_speed
if self.x == self.node.x and self.y == self.node.y:
self.spawn = True
def setImages(self):
self.images["LEFT"] = self.spritesheet.image_at((65, 129, 32, 32))
self.images["RIGHT"] = self.spritesheet.image_at((65, 161, 32, 32))
self.images["DOWN"] = self.spritesheet.image_at((65, 97, 32, 32))
self.images["UP"] = self.spritesheet.image_at((65, 65, 32, 32))
def draw(self, time):
if self.dying:
self.dyingModeTimer(time)
elif self.direction == "LEFT":
self.image = self.images["LEFT"]
elif self.direction == "RIGHT":
self.image = self.images["RIGHT"]
elif self.direction == "DOWN":
self.image = self.images["DOWN"]
elif self.direction == "UP":
self.image = self.images["UP"]
rect = self.image.get_rect()
rect.left = self.x - 16
rect.top = self.y - 16
self.screen.blit(self.image, rect)
def reset(self):
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = 425
self.y = 460
self.image = self.images["UP"]
self.spawn = False
self.direction = "STOP"
self.dying = False
def moveAround(self):
if self.dead:
return
self.spawnToNode()
if (self.x, self.y) == (425, 350):
self.direction = "RIGHT"
if (self.x, self.y) == (567, 350):
self.direction = "DOWN"
if (self.x, self.y) == (567, 435):
self.direction = "RIGHT"
if (self.x, self.y) == (650, 435):
self.direction = "DOWN"
if (self.x, self.y) == (650, 620):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 620):
self.direction = "DOWN"
if (self.x, self.y) == (800, 710):
self.direction = "LEFT"
if (self.x, self.y) == (745, 710):
self.direction = "DOWN"
if (self.x, self.y) == (745, 800):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 800):
self.direction = "DOWN"
if (self.x, self.y) == (800, 890):
self.direction = "LEFT"
if (self.x, self.y) == (50, 890):
self.direction = "UP"
if (self.x, self.y) == (50, 800):
self.direction = "RIGHT"
if (self.x, self.y) == (105, 800):
self.direction = "UP"
if (self.x, self.y) == (105, 710):
self.direction = "LEFT"
if (self.x, self.y) == (50, 710):
self.direction = "UP"
if (self.x, self.y) == (50, 620):
self.direction = "RIGHT"
if (self.x, self.y) == (200, 620):
self.direction = "UP"
if (self.x, self.y) == (200, 255):
self.direction = "LEFT"
if (self.x, self.y) == (50, 255):
self.direction = "UP"
if (self.x, self.y) == (50, 50):
self.direction = "RIGHT"
if (self.x, self.y) == (375, 50):
self.direction = "DOWN"
if (self.x, self.y) == (375, 165):
self.direction = "RIGHT"
if (self.x, self.y) == (475, 165):
self.direction = "UP"
if (self.x, self.y) == (475, 50):
self.direction = "RIGHT"
if (self.x, self.y) == (800, 50):
self.direction = "DOWN"
if (self.x, self.y) == (800, 255):
self.direction = "LEFT"
if (self.x, self.y) == (650, 255):
self.direction = "DOWN"
def drawDead(self, framerate):
if not self.score_checked:
self.score = str(self.scores[self.settings.index])
self.image_x = self.x
self.image_y = self.y
self.score_checked = True
if self.death_frame + 1000 == framerate:
self.reset()
self.dead = False
text = self.font.render(self.score, True, (255, 255, 255))
text_rec = text.get_rect(center=(self.image_x, self.image_y))
self.screen.blit(text, text_rec)
self.incrementScore()
class Clyde(Inkey):
def __init__(self, screen, graph, settings, pacman):
super().__init__(screen, graph, settings, pacman)
self.x = 485
self.image = self.images["RIGHT"]
def setImages(self):
self.images["LEFT"] = self.spritesheet.image_at((97, 129, 32, 32))
self.images["RIGHT"] = self.spritesheet.image_at((97, 161, 32, 32))
self.images["DOWN"] = self.spritesheet.image_at((97, 97, 32, 32))
self.images["UP"] = self.spritesheet.image_at((97, 65, 32, 32))
def spawnToNode(self):
if self.pacman.score_from_death >= 150 and self.spawn == False:
if self.y > 420:
self.direction = "UP"
self.y -= self.move_speed
elif self.x > 425:
self.direction = "LEFT"
self.x -= self.move_speed
elif self.x == 425 and self.y > 350:
self.direction = "UP"
self.y -= self.move_speed
if self.x == self.node.x and self.y == self.node.y:
self.spawn = True
def reset(self):
self.node = self.graph.nodes[len(self.graph.nodes) - 3]
self.x = 485
self.y = 460
self.image = self.images["RIGHT"]
self.spawn = False
self.direction = "STOP"
self.dying = False
def drawDead(self, framerate):
if not self.score_checked:
self.score = str(self.scores[self.settings.index])
self.image_x = self.x
self.image_y = self.y
self.score_checked = True
if self.death_frame + 1000 == framerate:
self.reset()
self.dead = False
text = self.font.render(self.score, True, (255, 255, 255))
text_rec = text.get_rect(center=(self.image_x, self.image_y))
self.screen.blit(text, text_rec)
self.incrementScore()