from .enums import GhostColor, GhostMode, GhostBehavior from .behaviors import path_toward # required if you want a fallback 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) self.speed = speed self.direction = pygame.Vector2(1, 0) self.mode = GhostMode.SCATTER self.home_position = position def update(self, maze, pacman): if self.mode == GhostMode.FRIGHTENED: self.change_direction_randomly(maze) else: 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_randomly(self, maze): import random directions = [pygame.Vector2(1, 0), pygame.Vector2(-1, 0), pygame.Vector2(0, 1), pygame.Vector2(0, -1)] random.shuffle(directions) for d in directions: test_pos = self.rect.move(d.x * self.speed, d.y * self.speed) if not maze.is_wall(test_pos.center): self.direction = d break def set_mode(self, mode: GhostMode): self.mode = mode if mode == GhostMode.FRIGHTENED: self.image.fill((33, 33, 255)) # dark blue else: self.image.fill(self.color) class Blinky(Ghost): def __init__(self, position): super().__init__("Blinky", GhostColor.BLINKY, GhostBehavior.BLINKY, position, speed=2) class Pinky(Ghost): def __init__(self, position): super().__init__("Pinky", GhostColor.PINKY, GhostBehavior.PINKY, position, speed=2) class Inky(Ghost): def __init__(self, position): super().__init__("Inky", GhostColor.INKY, GhostBehavior.INKY, position, speed=2) class Clyde(Ghost): def __init__(self, position): super().__init__("Clyde", GhostColor.CLYDE, GhostBehavior.CLYDE, position, speed=2)