Update actors/ghost.py

This commit is contained in:
2025-04-15 21:40:20 +02:00
parent c7b1285879
commit 679a1e31fa

View File

@@ -1,11 +1,12 @@
import pygame
from enums import GhostColor, GhostMode
from enums import GhostColor, GhostMode, GhostBehavior
from behaviors import path_toward # required if you want a fallback
class ActorGhost(pygame.sprite.Sprite):
def __init__(self, name, color_enum, position, speed):
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)
@@ -14,14 +15,16 @@ class ActorGhost(pygame.sprite.Sprite):
self.mode = GhostMode.SCATTER
self.home_position = position
def update(self, maze):
new_pos = self.rect.move(self.direction.x * self.speed, self.direction.y * self.speed)
if not maze.is_wall(new_pos.center):
self.rect = new_pos
def update(self, maze, pacman):
if self.mode == GhostMode.FRIGHTENED:
self.change_direction_randomly(maze)
else:
self.change_direction(maze)
self.direction = self.behavior.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):
self.rect = new_pos
def change_direction(self, maze):
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)]
@@ -37,16 +40,16 @@ class ActorGhost(pygame.sprite.Sprite):
class Blinky(Ghost):
def __init__(self, position):
super().__init__("Blinky", GhostColor.BLINKY, position, speed=2)
super().__init__("Blinky", GhostColor.BLINKY, GhostBehavior.BLINKY, position, speed=2)
class Pinky(Ghost):
def __init__(self, position):
super().__init__("Pinky", GhostColor.PINKY, position, speed=2)
super().__init__("Pinky", GhostColor.PINKY, GhostBehavior.PINKY, position, speed=2)
class Inky(Ghost):
def __init__(self, position):
super().__init__("Inky", GhostColor.INKY, position, speed=2)
super().__init__("Inky", GhostColor.INKY, GhostBehavior.INKY, position, speed=2)
class Clyde(Ghost):
def __init__(self, position):
super().__init__("Clyde", GhostColor.CLYDE, position, speed=2)
super().__init__("Clyde", GhostColor.CLYDE, GhostBehavior.CLYDE, position, speed=2)