From bf1629406b7eabd26f4202f86ad719a2c6d0112b Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Mon, 14 Apr 2025 09:26:12 +0200 Subject: [PATCH] #2 Animating mouth. /JL --- actors/pacman.py | 26 +++++++++++++++++++++----- pman.py | 4 +++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/actors/pacman.py b/actors/pacman.py index 7a83c49..e36e732 100644 --- a/actors/pacman.py +++ b/actors/pacman.py @@ -8,12 +8,29 @@ class ActorPacman: self.direction = PlayerDirection.DirectionRight self.center = (200, 200) self.radius = 100 - self.mouth_angle = math.radians(45) + self.mouth_angle_deg = 45 + self.min_mouth_deg = 5 + self.max_mouth_deg = 45 + self.animate_speed = 2 + self.mouth_closing = False - def draw_pacman(self): + def animate(self): + if self.mouth_closing: + self.mouth_angle_deg -= self.animate_speed + if self.mouth_angle_deg <= self.min_mouth_deg: + self.mouth_angle_deg = self.min_mouth_deg + self.mouth_closing = False + else: + self.mouth_angle_deg += self.animate_speed + if self.mouth_angle_deg >= self.max_mouth_deg: + self.mouth_angle_deg = self.max_mouth_deg + self.mouth_closing = True + + def draw(self): + mouth_angle = math.radians(self.mouth_angle_deg) rotation_offset = math.radians(self.direction, 0) - start_angle = self.mouth_angle / 2 * rotation_offset - end_angle = 2 / math.pi - self.mouth_angle / 2 * rotation_offset + start_angle = mouth_angle / 2 + rotation_offset + end_angle = 2 * math.pi - mouth_angle / 2 + rotation_offset points = [self.center] angle = start_angle while angle <= end_angle: @@ -22,7 +39,6 @@ class ActorPacman: points.append((x, y)) angle += math.radians(1) pygame.draw.polygon(self.screen, Colors.Yellow, points) - pygame.display.flip() def move_right(self): pass diff --git a/pman.py b/pman.py index 3183e8c..19a540c 100644 --- a/pman.py +++ b/pman.py @@ -2,10 +2,12 @@ import pygame from actors.enums import Colors from actors.pacman import ActorPacman +__version__ = "0.0.2" + def main() -> None: pygame.init() screen = pygame.display.set_mode((800, 800)) - pygame.display.set_caption("Pac-Man") + pygame.display.set_caption("Pac-Man " + __version__) player = ActorPacman()