From f1c6c7c23dbb13e2ca0ab2e349e626d6167c48e0 Mon Sep 17 00:00:00 2001 From: Brosef Date: Fri, 20 Jun 2025 12:27:48 +0100 Subject: [PATCH] Implemented AnimatedObject --- src/anim.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/anim.py b/src/anim.py index 500a36f..e4c87bd 100644 --- a/src/anim.py +++ b/src/anim.py @@ -1,4 +1,5 @@ import pygame +import time class AnimationHandler(pygame.Surface): """ @@ -6,9 +7,13 @@ class AnimationHandler(pygame.Surface): webp file, and handles drawing it to the screen in a way that doesn't rely on frame counters. """ - def __init__(self, animationPath): - pass + def __init__(self, animationID: str, animationPath: str, fps: int): + self.animationID = animationID + self.frames = pygame.image.load_animation(animationPath) + self.fps = fps + self.frameCount = len(self.frames) +# TODO: Add optional transparancy? class AnimatedObject: """ @@ -22,7 +27,8 @@ class AnimatedObject: self._currentAnim = None self._animStart = None self._animations = {} - #self.baseFrame = pygame.image.load(baseFrame) + self.baseFrame = pygame.image.load(baseFrame) + self._surf = pygame.Surface(self.baseFrame.size) def getFrame(self) -> pygame.Surface: """ @@ -32,9 +38,21 @@ class AnimatedObject: pygame.Surface: The current frame """ + self._surf.fill((0, 0, 0)) + + if self._currentAnim == None: + self._surf.blit(self.baseFrame, (0, 0)) + else: + frame = min(round((time.perf_counter() - self._animStart) * self._currentAnim.fps), self._currentAnim.frameCount - 1) + self._surf.blit(self._currentAnim.frames[frame][0], (0, 0)) + if frame == self._currentAnim.frameCount - 1: + self._currentAnim = None + self._animStart = None + + return self._surf - def addAnimation(self, animation: AnimationHandler, animationID: str): + def addAnimation(self, animation: AnimationHandler): """ Adds an animation to the object. @@ -42,7 +60,7 @@ class AnimatedObject: animation (AnimationHandler): The actual animation. animationID (str): The ID that's later used to play the animation. """ - self.animations.update({animationID: animation}) + self._animations.update({animation.animationID: animation}) def playAnim(self, animationID: str): """