So apparently if you don't put all of this in it's own directory, pip explodes.

This commit is contained in:
2025-06-20 18:05:57 +01:00
parent f4e0e24774
commit 2c79adaf36
4 changed files with 5 additions and 4 deletions

View File

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

85
src/gameUtils/anim.py Normal file
View File

@ -0,0 +1,85 @@
import pygame
import time
class AnimationHandler(pygame.Surface):
"""
TODO: Write documentation on this, it takes in a
webp file, and handles drawing it to the screen in
a way that doesn't rely on frame counters.
"""
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:
"""
"""
def __init__(self, baseFrame):
"""
Args:
baseFrame: A still image that gets displayed when no other animation is playing.
"""
self._currentAnim = None
self._animStart = None
self._animations = {}
self.baseFrame = pygame.image.load(baseFrame)
self._surf = pygame.Surface(self.baseFrame.size)
def getFrame(self) -> pygame.Surface:
"""
Gets the current frame based on which animation is playing, and the current time.
Returns:
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):
"""
Adds an animation to the object.
Args:
animation (AnimationHandler): The actual animation.
animationID (str): The ID that's later used to play the animation.
"""
self._animations.update({animation.animationID: animation})
def playAnim(self, animationID: str):
"""
Plays an animation.
Args:
animationID (str): The animation ID to play.
"""
self._animStart = time.perf_counter()
self._currentAnim = self._animations[animationID]
def get_view(self):
print(f'get_view called')
return super().get_view()
def get_buffer(self):
print(f'get_buffer called')
return super().get_buffer()
def copy(self):
print(f'copy called')
return super().copy()

26
src/gameUtils/base.py Normal file
View File

@ -0,0 +1,26 @@
import tomllib
class Game:
def __init__(self, surface):
"""
Initialises some things for the game developers.
Args:
surface (pygame.Surface): The surface the game devs draw on.
"""
self.surf = surface
self.size = self.surf.size
self.pm = None
with open('./game.toml', 'r') as f:
self.cfg = tomllib.loads(f.read())
def update(self):
pass
def onEvent(self, event):
pass
def close(self):
pass

14
src/gameUtils/utils.py Normal file
View File

@ -0,0 +1,14 @@
import pygame
def centre(surface: pygame.Surface, rect: tuple[int, int, int, int]) -> pygame.Surface:
"""
Centres a surface within a given rectangle.
Args:
surface: The surface to be centred.
rect: The rectangle the surface will be centred in.
Returns:
pygame.Surface
"""
pass