First attempt on ghost movement. /JL
This commit is contained in:
@@ -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)
|
||||
CLYDE = "clyde_behavior"
|
@@ -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):
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user