diff --git a/actors/enums.py b/actors/enums.py index 56200f0..664c934 100644 --- a/actors/enums.py +++ b/actors/enums.py @@ -39,13 +39,4 @@ class GhostBehavior(Enum): BLINKY = "blinky_behavior" PINKY = "pinky_behavior" INKY = "inky_behavior" - CLYDE = "clyde_behavior" - - def decide_direction(self, ghost, pacman, maze): - strategy = { - GhostBehavior.BLINKY: blinky_behavior, - GhostBehavior.PINKY: pinky_behavior, - GhostBehavior.INKY: inky_behavior, - GhostBehavior.CLYDE: clyde_behavior, - }[self] - return strategy(ghost, pacman, maze) \ No newline at end of file + CLYDE = "clyde_behavior" \ No newline at end of file diff --git a/actors/ghost.py b/actors/ghost.py index f8576d1..8c71338 100644 --- a/actors/ghost.py +++ b/actors/ghost.py @@ -1,5 +1,5 @@ from .enums import GhostColor, GhostMode, GhostBehavior -from .ghost_behaviors import path_toward # required if you want a fallback +import actors.ghost_behaviors as GB import pygame class Ghost(pygame.sprite.Sprite): @@ -16,13 +16,22 @@ class Ghost(pygame.sprite.Sprite): 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.behavior.decide_direction(self, pacman, maze) + 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.center): + if not maze.is_wall(new_pos[0], new_pos[1]): self.rect = new_pos def change_direction_randomly(self, maze): diff --git a/actors/ghost_behaviors.py b/actors/ghost_behaviors.py index 87da6ce..8f55a56 100644 --- a/actors/ghost_behaviors.py +++ b/actors/ghost_behaviors.py @@ -3,12 +3,12 @@ import random def blinky_behavior(ghost, pacman, maze): # Simple chase: move toward Pac-Man's position - return path_toward(ghost, pacman.rect.center, maze) + return path_toward(ghost, (pacman.x, pacman.y), maze) def pinky_behavior(ghost, pacman, maze): # Aim 4 tiles ahead of Pac-Man - offset = pacman.direction * 64 - target = (pacman.rect.centerx + offset.x, pacman.rect.centery + offset.y) + offset = pacman.x + 128 + target = (pacman.x + offset, pacman.y + offset) return path_toward(ghost, target, maze) def inky_behavior(ghost, pacman, maze): @@ -17,10 +17,10 @@ def inky_behavior(ghost, pacman, maze): def clyde_behavior(ghost, pacman, maze): # If close to Pac-Man, scatter; otherwise chase - distance = pygame.Vector2(pacman.rect.center).distance_to(ghost.rect.center) + distance = pygame.Vector2(pacman.x).distance_to(ghost.rect.center) if distance < 100: return path_toward(ghost, ghost.home_position, maze) - return path_toward(ghost, pacman.rect.center, maze) + return path_toward(ghost, pacman.x, maze) def path_toward(ghost, target_pos, maze): # Placeholder logic: pick a direction that reduces distance to target @@ -30,7 +30,7 @@ def path_toward(ghost, target_pos, maze): min_dist = float("inf") for d in directions: test_pos = ghost.rect.move(d.x * ghost.speed, d.y * ghost.speed) - if not maze.is_wall(test_pos.center): + if not maze.is_wall(test_pos[0], test_pos[1]): dist = pygame.Vector2(target_pos).distance_to(test_pos.center) if dist < min_dist: min_dist = dist diff --git a/pman.py b/pman.py index 6d489af..e920388 100644 --- a/pman.py +++ b/pman.py @@ -7,7 +7,7 @@ from actors.ghost_mode_controller import GhostModeController from hud import HUD from maze import Maze -__version__ = "0.4.0" +__version__ = "0.4.1" # Constants HAT_REPEAT_DELAY = 300 # milliseconds before first repeat @@ -62,12 +62,12 @@ class Game: """ offset = 20 # spread out a little bit inside ghost home - blinky = Blinky((center_position[0] - offset, center_position[1] - offset)) - pinky = Pinky((center_position[0] + offset, center_position[1] - offset)) - inky = Inky((center_position[0] - offset, center_position[1] + offset)) - clyde = Clyde((center_position[0] + offset, center_position[1] + offset)) + self.blinky = Blinky((center_position[0] - offset, center_position[1] - offset)) + self.pinky = Pinky((center_position[0] + offset, center_position[1] - offset)) + self.inky = Inky((center_position[0] - offset, center_position[1] + offset)) + self.clyde = Clyde((center_position[0] + offset, center_position[1] + offset)) - return pygame.sprite.Group(blinky, pinky, inky, clyde) + return pygame.sprite.Group(self.blinky, self.pinky, self.inky, self.clyde) def handle_hat_repeat(self): now = pygame.time.get_ticks() @@ -104,7 +104,7 @@ class Game: for ghost in self.ghosts: ghost.set_mode(self.current_mode) - #ghost.update(maze, pacman) + ghost.update(self.maze, self.player) self.ghosts.draw(self.screen) pygame.display.flip()