from .enums import GhostColor, GhostMode, GhostBehavior import actors.ghost_behaviors as GB import pygame 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 decide_direction(self, ghost, pacman, maze): strategy = { GhostBehavior.BLINKY: GB.blinky_behavior, GhostBehavior.PINKY: GB.pinky_behavior, GhostBehavior.INKY: GB.inky_behavior, GhostBehavior.CLYDE: GB.clyde_behavior, } return strategy[self.behavior](ghost, pacman, maze) def update(self, maze, pacman): if self.mode == GhostMode.FRIGHTENED: self.change_direction_randomly(maze) else: self.direction = self.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[0], new_pos[1]): 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)