First attempt on ghost movement. /JL
This commit is contained in:
@@ -40,12 +40,3 @@ class GhostBehavior(Enum):
|
|||||||
PINKY = "pinky_behavior"
|
PINKY = "pinky_behavior"
|
||||||
INKY = "inky_behavior"
|
INKY = "inky_behavior"
|
||||||
CLYDE = "clyde_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)
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from .enums import GhostColor, GhostMode, GhostBehavior
|
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
|
import pygame
|
||||||
|
|
||||||
class Ghost(pygame.sprite.Sprite):
|
class Ghost(pygame.sprite.Sprite):
|
||||||
@@ -16,13 +16,22 @@ class Ghost(pygame.sprite.Sprite):
|
|||||||
self.mode = GhostMode.SCATTER
|
self.mode = GhostMode.SCATTER
|
||||||
self.home_position = position
|
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):
|
def update(self, maze, pacman):
|
||||||
if self.mode == GhostMode.FRIGHTENED:
|
if self.mode == GhostMode.FRIGHTENED:
|
||||||
self.change_direction_randomly(maze)
|
self.change_direction_randomly(maze)
|
||||||
else:
|
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)
|
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
|
self.rect = new_pos
|
||||||
|
|
||||||
def change_direction_randomly(self, maze):
|
def change_direction_randomly(self, maze):
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ import random
|
|||||||
|
|
||||||
def blinky_behavior(ghost, pacman, maze):
|
def blinky_behavior(ghost, pacman, maze):
|
||||||
# Simple chase: move toward Pac-Man's position
|
# 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):
|
def pinky_behavior(ghost, pacman, maze):
|
||||||
# Aim 4 tiles ahead of Pac-Man
|
# Aim 4 tiles ahead of Pac-Man
|
||||||
offset = pacman.direction * 64
|
offset = pacman.x + 128
|
||||||
target = (pacman.rect.centerx + offset.x, pacman.rect.centery + offset.y)
|
target = (pacman.x + offset, pacman.y + offset)
|
||||||
return path_toward(ghost, target, maze)
|
return path_toward(ghost, target, maze)
|
||||||
|
|
||||||
def inky_behavior(ghost, pacman, maze):
|
def inky_behavior(ghost, pacman, maze):
|
||||||
@@ -17,10 +17,10 @@ def inky_behavior(ghost, pacman, maze):
|
|||||||
|
|
||||||
def clyde_behavior(ghost, pacman, maze):
|
def clyde_behavior(ghost, pacman, maze):
|
||||||
# If close to Pac-Man, scatter; otherwise chase
|
# 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:
|
if distance < 100:
|
||||||
return path_toward(ghost, ghost.home_position, maze)
|
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):
|
def path_toward(ghost, target_pos, maze):
|
||||||
# Placeholder logic: pick a direction that reduces distance to target
|
# 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")
|
min_dist = float("inf")
|
||||||
for d in directions:
|
for d in directions:
|
||||||
test_pos = ghost.rect.move(d.x * ghost.speed, d.y * ghost.speed)
|
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)
|
dist = pygame.Vector2(target_pos).distance_to(test_pos.center)
|
||||||
if dist < min_dist:
|
if dist < min_dist:
|
||||||
min_dist = dist
|
min_dist = dist
|
||||||
|
|||||||
14
pman.py
14
pman.py
@@ -7,7 +7,7 @@ from actors.ghost_mode_controller import GhostModeController
|
|||||||
from hud import HUD
|
from hud import HUD
|
||||||
from maze import Maze
|
from maze import Maze
|
||||||
|
|
||||||
__version__ = "0.4.0"
|
__version__ = "0.4.1"
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
HAT_REPEAT_DELAY = 300 # milliseconds before first repeat
|
HAT_REPEAT_DELAY = 300 # milliseconds before first repeat
|
||||||
@@ -62,12 +62,12 @@ class Game:
|
|||||||
"""
|
"""
|
||||||
offset = 20 # spread out a little bit inside ghost home
|
offset = 20 # spread out a little bit inside ghost home
|
||||||
|
|
||||||
blinky = Blinky((center_position[0] - offset, center_position[1] - offset))
|
self.blinky = Blinky((center_position[0] - offset, center_position[1] - offset))
|
||||||
pinky = Pinky((center_position[0] + offset, center_position[1] - offset))
|
self.pinky = Pinky((center_position[0] + offset, center_position[1] - offset))
|
||||||
inky = Inky((center_position[0] - offset, center_position[1] + offset))
|
self.inky = Inky((center_position[0] - offset, center_position[1] + offset))
|
||||||
clyde = Clyde((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):
|
def handle_hat_repeat(self):
|
||||||
now = pygame.time.get_ticks()
|
now = pygame.time.get_ticks()
|
||||||
@@ -104,7 +104,7 @@ class Game:
|
|||||||
|
|
||||||
for ghost in self.ghosts:
|
for ghost in self.ghosts:
|
||||||
ghost.set_mode(self.current_mode)
|
ghost.set_mode(self.current_mode)
|
||||||
#ghost.update(maze, pacman)
|
ghost.update(self.maze, self.player)
|
||||||
|
|
||||||
self.ghosts.draw(self.screen)
|
self.ghosts.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|||||||
Reference in New Issue
Block a user