Merge branch '0.2.7'

This commit is contained in:
2025-04-19 19:29:14 +02:00
4 changed files with 38 additions and 24 deletions

View File

@@ -12,10 +12,6 @@ class GhostDirection(Enum):
GhostUp = 3 GhostUp = 3
GhostDown = 4 GhostDown = 4
class PillType(Enum):
PillTypeRegular = 1
PillTypePower = 2
class Colors(Enum): class Colors(Enum):
Yellow = (255, 255, 0) Yellow = (255, 255, 0)
Black = (0, 0, 0) Black = (0, 0, 0)

View File

@@ -1,23 +1,26 @@
from .enums import PlayerDirection, Colors from .enums import PlayerDirection, Colors
from maze import Maze
import pygame import pygame
import math import math
class ActorPacman: class ActorPacman:
def __init__(self, scr, center): def __init__(self, scr, center, start_location = (0, 0), maze = None):
self.screen = scr self.screen = scr
self.direction = PlayerDirection.DirectionRight self.direction = PlayerDirection.DirectionRight
self.speed = 3 self.speed = 2
self.x, self.y = center self.x, self.y = center
self.radius = 10 self.radius = 12
self.mouth_angle_deg = 45 self.mouth_angle_deg = 45
self.min_mouth_deg = 5 self.min_mouth_deg = 5
self.max_mouth_deg = 45 self.max_mouth_deg = 45
self.animate_speed = 2 self.animate_speed = 2
self.mouth_closing = False self.mouth_closing = False
self.location = start_location
self.maze = maze
def update(self): def update(self):
self.animate() self.animate()
def animate(self): def animate(self):
if self.mouth_closing: if self.mouth_closing:
self.mouth_angle_deg -= self.animate_speed self.mouth_angle_deg -= self.animate_speed
@@ -29,18 +32,8 @@ class ActorPacman:
if self.mouth_angle_deg >= self.max_mouth_deg: if self.mouth_angle_deg >= self.max_mouth_deg:
self.mouth_angle_deg = self.max_mouth_deg self.mouth_angle_deg = self.max_mouth_deg
self.mouth_closing = True self.mouth_closing = True
match self.direction: #self.x = max(self.radius, min(self.x, 400 - self.radius))
case PlayerDirection.DirectionRight: #self.y = max(self.radius, min(self.y, 400 - self.radius))
self.x += self.speed
case PlayerDirection.DirectionLeft:
self.x -= self.speed
case PlayerDirection.DirectionUp:
self.y -= self.speed
case PlayerDirection.DirectionDown:
self.y += self.speed
self.x = max(self.radius, min(self.x, 400 - self.radius))
self.y = max(self.radius, min(self.y, 400 - self.radius))
def draw(self): def draw(self):
mouth_angle = math.radians(self.mouth_angle_deg) mouth_angle = math.radians(self.mouth_angle_deg)
@@ -56,6 +49,22 @@ class ActorPacman:
points.append((x, y)) points.append((x, y))
angle += math.radians(1) angle += math.radians(1)
pygame.draw.polygon(self.screen, Colors.Yellow.value, points) pygame.draw.polygon(self.screen, Colors.Yellow.value, points)
def move_right(self):
for _ in range(16):
self.x += self.speed
def move_left(self):
for _ in range(16):
self.x -= self.speed
def move_up(self):
for _ in range(16):
self.y -= self.speed
def move_down(self):
for _ in range(16):
self.y += self.speed
def set_direction(self, dir: PlayerDirection): def set_direction(self, dir: PlayerDirection):
self.direction = dir self.direction = dir

View File

@@ -36,6 +36,7 @@ class Maze:
self.power_pellets.add((x, y)) self.power_pellets.add((x, y))
elif char == "P": elif char == "P":
self.pacman_start = (x * TILE_SIZE + TILE_SIZE // 2, y * TILE_SIZE + TILE_SIZE // 2 + 32) self.pacman_start = (x * TILE_SIZE + TILE_SIZE // 2, y * TILE_SIZE + TILE_SIZE // 2 + 32)
self.pacman_location = (x, y)
def draw(self, screen): def draw(self, screen):
for y, row in enumerate(self.layout): for y, row in enumerate(self.layout):

16
pman.py
View File

@@ -1,12 +1,12 @@
import pygame import pygame
from actors.enums import Colors, PlayerDirection, PillType from actors.enums import Colors, PlayerDirection
from actors.pacman import ActorPacman from actors.pacman import ActorPacman
from actors.ghost import Blinky, Pinky, Inky, Clyde # adjust import path as needed from actors.ghost import Blinky, Pinky, Inky, Clyde # adjust import path as needed
from actors.ghost_mode_controller import GhostModeController 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.2.6" __version__ = "0.2.7"
def spawn_ghosts(center_position): def spawn_ghosts(center_position):
@@ -38,7 +38,7 @@ def main() -> None:
hud = HUD(screen_width, screen_height, cherry_image=cherry_img) hud = HUD(screen_width, screen_height, cherry_image=cherry_img)
maze = Maze("maze/pacman_maze.txt") maze = Maze("maze/pacman_maze.txt")
player = ActorPacman(screen, center=maze.pacman_start, start_location=maze.pacman_location) player = ActorPacman(screen, center=maze.pacman_start, start_location=maze.pacman_location, maze=maze)
ghost_mode_controller = GhostModeController() ghost_mode_controller = GhostModeController()
ghost_home_center = (screen_width // 2, (screen_height) // 2) ghost_home_center = (screen_width // 2, (screen_height) // 2)
ghosts = spawn_ghosts(ghost_home_center) ghosts = spawn_ghosts(ghost_home_center)
@@ -56,28 +56,36 @@ def main() -> None:
match event.key: match event.key:
case pygame.K_RIGHT: case pygame.K_RIGHT:
player.direction = PlayerDirection.DirectionRight player.direction = PlayerDirection.DirectionRight
player.move_right()
case pygame.K_LEFT: case pygame.K_LEFT:
player.direction = PlayerDirection.DirectionLeft player.direction = PlayerDirection.DirectionLeft
player.move_left()
case pygame.K_UP: case pygame.K_UP:
player.direction = PlayerDirection.DirectionUp player.direction = PlayerDirection.DirectionUp
player.move_up()
case pygame.K_DOWN: case pygame.K_DOWN:
player.direction = PlayerDirection.DirectionDown player.direction = PlayerDirection.DirectionDown
player.move_down()
case pygame.JOYHATMOTION: case pygame.JOYHATMOTION:
print("Hat has been pressed.") print("Hat has been pressed.")
hat_x, hat_y = event.value hat_x, hat_y = event.value
if hat_x == 1: if hat_x == 1:
player.direction = PlayerDirection.DirectionRight player.direction = PlayerDirection.DirectionRight
player.move_right()
elif hat_x == -1: elif hat_x == -1:
player.direction = PlayerDirection.DirectionLeft player.direction = PlayerDirection.DirectionLeft
player.move_left()
elif hat_y == 1: elif hat_y == 1:
player.direction = PlayerDirection.DirectionUp player.direction = PlayerDirection.DirectionUp
player.move_up()
elif hat_y == -1: elif hat_y == -1:
player.direction = PlayerDirection.DirectionDown player.direction = PlayerDirection.DirectionDown
player.move_down()
# In your main game loop: # In your main game loop:
hud.draw(screen) hud.draw(screen)
maze.draw(screen) maze.draw(screen)
player.animate() player.update()
player.draw() player.draw()
ghost_mode_controller.update() ghost_mode_controller.update()
current_mode = ghost_mode_controller.mode current_mode = ghost_mode_controller.mode