From 679a1e31fa095acd6758494179f3aa05bded7c55 Mon Sep 17 00:00:00 2001 From: Lerking Date: Tue, 15 Apr 2025 21:40:20 +0200 Subject: [PATCH] Update actors/ghost.py --- actors/ghost.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/actors/ghost.py b/actors/ghost.py index bfe9ae5..a157d20 100644 --- a/actors/ghost.py +++ b/actors/ghost.py @@ -1,11 +1,12 @@ -import pygame -from enums import GhostColor, GhostMode +from enums import GhostColor, GhostMode, GhostBehavior +from behaviors import path_toward # required if you want a fallback -class ActorGhost(pygame.sprite.Sprite): - def __init__(self, name, color_enum, position, speed): +class Ghost(pygame.sprite.Sprite): + def __init__(self, name, color_enum, behavior_enum, position, speed): super().__init__() self.name = name self.color = color_enum.value + self.behavior = behavior_enum self.image = pygame.Surface((16, 16)) self.image.fill(self.color) self.rect = self.image.get_rect(center=position) @@ -14,14 +15,16 @@ class ActorGhost(pygame.sprite.Sprite): self.mode = GhostMode.SCATTER self.home_position = position - def update(self, maze): - new_pos = self.rect.move(self.direction.x * self.speed, self.direction.y * self.speed) - if not maze.is_wall(new_pos.center): - self.rect = new_pos + def update(self, maze, pacman): + if self.mode == GhostMode.FRIGHTENED: + self.change_direction_randomly(maze) else: - self.change_direction(maze) + self.direction = self.behavior.decide_direction(self, pacman, maze) + new_pos = self.rect.move(self.direction.x * self.speed, self.direction.y * self.speed) + if not maze.is_wall(new_pos.center): + self.rect = new_pos - def change_direction(self, maze): + def change_direction_randomly(self, maze): import random directions = [pygame.Vector2(1, 0), pygame.Vector2(-1, 0), pygame.Vector2(0, 1), pygame.Vector2(0, -1)] @@ -37,16 +40,16 @@ class ActorGhost(pygame.sprite.Sprite): class Blinky(Ghost): def __init__(self, position): - super().__init__("Blinky", GhostColor.BLINKY, position, speed=2) + super().__init__("Blinky", GhostColor.BLINKY, GhostBehavior.BLINKY, position, speed=2) class Pinky(Ghost): def __init__(self, position): - super().__init__("Pinky", GhostColor.PINKY, position, speed=2) + super().__init__("Pinky", GhostColor.PINKY, GhostBehavior.PINKY, position, speed=2) class Inky(Ghost): def __init__(self, position): - super().__init__("Inky", GhostColor.INKY, position, speed=2) + super().__init__("Inky", GhostColor.INKY, GhostBehavior.INKY, position, speed=2) class Clyde(Ghost): def __init__(self, position): - super().__init__("Clyde", GhostColor.CLYDE, position, speed=2) \ No newline at end of file + super().__init__("Clyde", GhostColor.CLYDE, GhostBehavior.CLYDE, position, speed=2) \ No newline at end of file