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, start_location = (0, 0)): super().__init__() self.name = name self.color = color_enum.value self.behavior = behavior_enum self.image = pygame.Surface((24, 24)) 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 self.location = start_location self.frightned = pygame.Surface((24, 24)) self.ghost_image = pygame.Surface((24, 24)) 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.image = self.frightned self.change_direction_randomly(maze) else: self.image = self.ghost_image 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(self.location[0], self.location[1]): self.rect = new_pos self.location = (new_pos[0] % 32, new_pos[1] % 32) 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[0], test_pos.center[1]): self.direction = d break def set_mode(self, mode: GhostMode): self.mode = mode if mode == GhostMode.FRIGHTENED: self.image = self.frightned else: self.image = self.ghost_image class Blinky(Ghost): def __init__(self, position, start_location): super().__init__("Blinky", GhostColor.BLINKY, GhostBehavior.BLINKY, position, start_location=(0, 0), speed=2) self.ghost_img = pygame.image.load("assets/blinky.png").convert_alpha() self.ghost_image = pygame.transform.scale(self.ghost_img, (24, 24)) if self.ghost_img else None self.fright_img = pygame.image.load("assets/frightned.png").convert_alpha() self.frightened = pygame.transform.scale(self.fright_img, (24, 24)) if self.fright_img else None class Pinky(Ghost): def __init__(self, position, start_location): super().__init__("Pinky", GhostColor.PINKY, GhostBehavior.PINKY, position, start_location=(0, 0), speed=2) self.ghost_img = pygame.image.load("assets/pinky.png").convert_alpha() self.ghost_image = pygame.transform.scale(self.ghost_img, (24, 24)) if self.ghost_img else None self.fright_img = pygame.image.load("assets/frightned.png").convert_alpha() self.frightened = pygame.transform.scale(self.fright_img, (24, 24)) if self.fright_img else None class Inky(Ghost): def __init__(self, position, start_location): super().__init__("Inky", GhostColor.INKY, GhostBehavior.INKY, position, start_location=(0, 0), speed=2) self.ghost_img = pygame.image.load("assets/inky.png").convert_alpha() self.ghost_image = pygame.transform.scale(self.ghost_img, (24, 24)) if self.ghost_img else None self.fright_img = pygame.image.load("assets/frightned.png").convert_alpha() self.frightened = pygame.transform.scale(self.fright_img, (24, 24)) if self.fright_img else None class Clyde(Ghost): def __init__(self, position, start_location): super().__init__("Clyde", GhostColor.CLYDE, GhostBehavior.CLYDE, position, start_location=(0, 0), speed=2) self.ghost_img = pygame.image.load("assets/clyde.png").convert_alpha() self.ghost_image = pygame.transform.scale(self.ghost_img, (24, 24)) if self.ghost_img else None self.fright_img = pygame.image.load("assets/frightned.png").convert_alpha() self.frightened = pygame.transform.scale(self.fright_img, (24, 24)) if self.fright_img else None