Update actors/ghost.py
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import pygame
|
from enums import GhostColor, GhostMode, GhostBehavior
|
||||||
from enums import GhostColor, GhostMode
|
from behaviors import path_toward # required if you want a fallback
|
||||||
|
|
||||||
class ActorGhost(pygame.sprite.Sprite):
|
class Ghost(pygame.sprite.Sprite):
|
||||||
def __init__(self, name, color_enum, position, speed):
|
def __init__(self, name, color_enum, behavior_enum, position, speed):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.name = name
|
self.name = name
|
||||||
self.color = color_enum.value
|
self.color = color_enum.value
|
||||||
|
self.behavior = behavior_enum
|
||||||
self.image = pygame.Surface((16, 16))
|
self.image = pygame.Surface((16, 16))
|
||||||
self.image.fill(self.color)
|
self.image.fill(self.color)
|
||||||
self.rect = self.image.get_rect(center=position)
|
self.rect = self.image.get_rect(center=position)
|
||||||
@@ -14,14 +15,16 @@ class ActorGhost(pygame.sprite.Sprite):
|
|||||||
self.mode = GhostMode.SCATTER
|
self.mode = GhostMode.SCATTER
|
||||||
self.home_position = position
|
self.home_position = position
|
||||||
|
|
||||||
def update(self, maze):
|
def update(self, maze, pacman):
|
||||||
new_pos = self.rect.move(self.direction.x * self.speed, self.direction.y * self.speed)
|
if self.mode == GhostMode.FRIGHTENED:
|
||||||
if not maze.is_wall(new_pos.center):
|
self.change_direction_randomly(maze)
|
||||||
self.rect = new_pos
|
|
||||||
else:
|
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
|
import random
|
||||||
directions = [pygame.Vector2(1, 0), pygame.Vector2(-1, 0),
|
directions = [pygame.Vector2(1, 0), pygame.Vector2(-1, 0),
|
||||||
pygame.Vector2(0, 1), pygame.Vector2(0, -1)]
|
pygame.Vector2(0, 1), pygame.Vector2(0, -1)]
|
||||||
@@ -37,16 +40,16 @@ class ActorGhost(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
class Blinky(Ghost):
|
class Blinky(Ghost):
|
||||||
def __init__(self, position):
|
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):
|
class Pinky(Ghost):
|
||||||
def __init__(self, position):
|
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):
|
class Inky(Ghost):
|
||||||
def __init__(self, position):
|
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):
|
class Clyde(Ghost):
|
||||||
def __init__(self, position):
|
def __init__(self, position):
|
||||||
super().__init__("Clyde", GhostColor.CLYDE, position, speed=2)
|
super().__init__("Clyde", GhostColor.CLYDE, GhostBehavior.CLYDE, position, speed=2)
|
Reference in New Issue
Block a user