Compare commits

...

2 Commits

Author SHA1 Message Date
f1c6c7c23d Implemented AnimatedObject 2025-06-20 12:27:48 +01:00
8ce99ca561 Fixed typo 2025-06-20 12:27:29 +01:00
2 changed files with 24 additions and 6 deletions

View File

@ -1,4 +1,4 @@
from .base import Game from .base import Game
from .utils import centre from .utils import centre
from .anim import AnimatedHandler from .anim import AnimationHandler
from .anim import AnimatedObject from .anim import AnimatedObject

View File

@ -1,4 +1,5 @@
import pygame import pygame
import time
class AnimationHandler(pygame.Surface): class AnimationHandler(pygame.Surface):
""" """
@ -6,9 +7,13 @@ class AnimationHandler(pygame.Surface):
webp file, and handles drawing it to the screen in webp file, and handles drawing it to the screen in
a way that doesn't rely on frame counters. a way that doesn't rely on frame counters.
""" """
def __init__(self, animationPath): def __init__(self, animationID: str, animationPath: str, fps: int):
pass self.animationID = animationID
self.frames = pygame.image.load_animation(animationPath)
self.fps = fps
self.frameCount = len(self.frames)
# TODO: Add optional transparancy?
class AnimatedObject: class AnimatedObject:
""" """
@ -22,7 +27,8 @@ class AnimatedObject:
self._currentAnim = None self._currentAnim = None
self._animStart = None self._animStart = None
self._animations = {} 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: def getFrame(self) -> pygame.Surface:
""" """
@ -32,9 +38,21 @@ class AnimatedObject:
pygame.Surface: The current frame 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. Adds an animation to the object.
@ -42,7 +60,7 @@ class AnimatedObject:
animation (AnimationHandler): The actual animation. animation (AnimationHandler): The actual animation.
animationID (str): The ID that's later used to play the 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): def playAnim(self, animationID: str):
""" """