Added pacman movement. /JL
This commit is contained in:
@@ -12,10 +12,6 @@ class GhostDirection(Enum):
|
||||
GhostUp = 3
|
||||
GhostDown = 4
|
||||
|
||||
class PillType(Enum):
|
||||
PillTypeRegular = 1
|
||||
PillTypePower = 2
|
||||
|
||||
class Colors(Enum):
|
||||
Yellow = (255, 255, 0)
|
||||
Black = (0, 0, 0)
|
||||
|
@@ -1,20 +1,26 @@
|
||||
from .enums import PlayerDirection, Colors
|
||||
from maze import Maze
|
||||
import pygame
|
||||
import math
|
||||
|
||||
class ActorPacman:
|
||||
def __init__(self, scr, center):
|
||||
def __init__(self, scr, center, start_location = (0, 0), maze = None):
|
||||
self.screen = scr
|
||||
self.direction = PlayerDirection.DirectionRight
|
||||
self.speed = 3
|
||||
self.speed = 2
|
||||
self.x, self.y = center
|
||||
self.radius = 10
|
||||
self.radius = 12
|
||||
self.mouth_angle_deg = 45
|
||||
self.min_mouth_deg = 5
|
||||
self.max_mouth_deg = 45
|
||||
self.animate_speed = 2
|
||||
self.mouth_closing = False
|
||||
self.location = start_location
|
||||
self.maze = maze
|
||||
|
||||
def update(self):
|
||||
self.animate()
|
||||
|
||||
def animate(self):
|
||||
if self.mouth_closing:
|
||||
self.mouth_angle_deg -= self.animate_speed
|
||||
@@ -26,18 +32,8 @@ class ActorPacman:
|
||||
if self.mouth_angle_deg >= self.max_mouth_deg:
|
||||
self.mouth_angle_deg = self.max_mouth_deg
|
||||
self.mouth_closing = True
|
||||
match self.direction:
|
||||
case PlayerDirection.DirectionRight:
|
||||
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))
|
||||
#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):
|
||||
mouth_angle = math.radians(self.mouth_angle_deg)
|
||||
@@ -53,9 +49,22 @@ class ActorPacman:
|
||||
points.append((x, y))
|
||||
angle += math.radians(1)
|
||||
pygame.draw.polygon(self.screen, Colors.Yellow.value, points)
|
||||
|
||||
|
||||
def move_right(self):
|
||||
pass
|
||||
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):
|
||||
self.direction = dir
|
1
maze.py
1
maze.py
@@ -36,6 +36,7 @@ class Maze:
|
||||
self.power_pellets.add((x, y))
|
||||
elif char == "P":
|
||||
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):
|
||||
for y, row in enumerate(self.layout):
|
||||
|
16
pman.py
16
pman.py
@@ -1,12 +1,12 @@
|
||||
import pygame
|
||||
from actors.enums import Colors, PlayerDirection, PillType
|
||||
from actors.enums import Colors, PlayerDirection
|
||||
from actors.pacman import ActorPacman
|
||||
from actors.ghost import Blinky, Pinky, Inky, Clyde # adjust import path as needed
|
||||
from actors.ghost_mode_controller import GhostModeController
|
||||
from hud import HUD
|
||||
from maze import Maze
|
||||
|
||||
__version__ = "0.2.4"
|
||||
__version__ = "0.2.7"
|
||||
|
||||
|
||||
def spawn_ghosts(center_position):
|
||||
@@ -38,7 +38,7 @@ def main() -> None:
|
||||
hud = HUD(screen_width, screen_height, cherry_image=cherry_img)
|
||||
maze = Maze("maze/pacman_maze.txt")
|
||||
|
||||
player = ActorPacman(screen, center=maze.pacman_start)
|
||||
player = ActorPacman(screen, center=maze.pacman_start, start_location=maze.pacman_location, maze=maze)
|
||||
ghost_mode_controller = GhostModeController()
|
||||
ghost_home_center = (screen_width // 2, (screen_height) // 2)
|
||||
ghosts = spawn_ghosts(ghost_home_center)
|
||||
@@ -56,28 +56,36 @@ def main() -> None:
|
||||
match event.key:
|
||||
case pygame.K_RIGHT:
|
||||
player.direction = PlayerDirection.DirectionRight
|
||||
player.move_right()
|
||||
case pygame.K_LEFT:
|
||||
player.direction = PlayerDirection.DirectionLeft
|
||||
player.move_left()
|
||||
case pygame.K_UP:
|
||||
player.direction = PlayerDirection.DirectionUp
|
||||
player.move_up()
|
||||
case pygame.K_DOWN:
|
||||
player.direction = PlayerDirection.DirectionDown
|
||||
player.move_down()
|
||||
case pygame.JOYHATMOTION:
|
||||
print("Hat has been pressed.")
|
||||
hat_x, hat_y = event.value
|
||||
if hat_x == 1:
|
||||
player.direction = PlayerDirection.DirectionRight
|
||||
player.move_right()
|
||||
elif hat_x == -1:
|
||||
player.direction = PlayerDirection.DirectionLeft
|
||||
player.move_left()
|
||||
elif hat_y == 1:
|
||||
player.direction = PlayerDirection.DirectionUp
|
||||
player.move_up()
|
||||
elif hat_y == -1:
|
||||
player.direction = PlayerDirection.DirectionDown
|
||||
player.move_down()
|
||||
|
||||
# In your main game loop:
|
||||
hud.draw(screen)
|
||||
maze.draw(screen)
|
||||
player.animate()
|
||||
player.update()
|
||||
player.draw()
|
||||
ghost_mode_controller.update()
|
||||
current_mode = ghost_mode_controller.mode
|
||||
|
Reference in New Issue
Block a user