#2 Pacman moves. /JL

This commit is contained in:
Jan Lerking
2025-04-14 09:47:43 +02:00
parent bf1629406b
commit 6914fd8632
2 changed files with 41 additions and 12 deletions

View File

@@ -3,10 +3,11 @@ import pygame
import math
class ActorPacman:
def __init__(self, scr):
def __init__(self, scr, center):
self.screen = scr
self.direction = PlayerDirection.DirectionRight
self.center = (200, 200)
self.speed = 3
self.x, self.y = center
self.radius = 100
self.mouth_angle_deg = 45
self.min_mouth_deg = 5
@@ -25,17 +26,30 @@ 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))
def draw(self):
mouth_angle = math.radians(self.mouth_angle_deg)
center = (self.x, self.y)
rotation_offset = math.radians(self.direction, 0)
start_angle = mouth_angle / 2 + rotation_offset
end_angle = 2 * math.pi - mouth_angle / 2 + rotation_offset
points = [self.center]
points = [center]
angle = start_angle
while angle <= end_angle:
x = self.center[0] + self.radius * math.cos(angle)
y = self.center[1] + self.radius * math.sin(angle)
x = center[0] + self.radius * math.cos(angle)
y = center[1] + self.radius * math.sin(angle)
points.append((x, y))
angle += math.radians(1)
pygame.draw.polygon(self.screen, Colors.Yellow, points)

25
pman.py
View File

@@ -1,8 +1,8 @@
import pygame
from actors.enums import Colors
from actors.enums import Colors, PlayerDirection
from actors.pacman import ActorPacman
__version__ = "0.0.2"
__version__ = "0.1.0"
def main() -> None:
pygame.init()
@@ -10,16 +10,31 @@ def main() -> None:
pygame.display.set_caption("Pac-Man " + __version__)
player = ActorPacman()
clock = pygame.time.Clock()
running = True
while running:
screen.fill(Colors.Black)
player.draw_pacman()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
match event.type:
case pygame.QUIT:
running = False
case pygame.KEYDOWN:
match event.key:
case pygame.K_RIGHT:
player.direction = PlayerDirection.DirectionRight
case pygame.K_LEFT:
player.direction = PlayerDirection.DirectionLeft
case pygame.K_UP:
player.direction = PlayerDirection.DirectionUp
case pygame.K_DOWN:
player.direction = PlayerDirection.DirectionDown
player.animate()
player.draw()
pygame.display.flip()
clock.tick(60)
pygame.quit()