From: Simon Cross Date: Sat, 5 Mar 2016 14:52:58 +0000 (+0200) Subject: Actually add animsurf. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=c9dc996ae2822889c815a9552adb584356f86af8;p=koperkapel.git Actually add animsurf. --- diff --git a/koperkapel/actors/animsurf.py b/koperkapel/actors/animsurf.py new file mode 100644 index 0000000..fb9e73a --- /dev/null +++ b/koperkapel/actors/animsurf.py @@ -0,0 +1,23 @@ +""" Surface-based actor with multiple frames. """ + +import random +from pgzero.clock import each_tick +from .orientatedsurf import OrientatedSurfActor + + +class AnimatedSurfActor(OrientatedSurfActor): + def __init__(self, frames, cycle_dt=0.2): + self._frames = frames + self._frame = random.randint(0, len(frames) - 1) + self._dt = 0 + self._cycle_dt = cycle_dt + each_tick(self.update) + super().__init__(surf=frames[self._frame], angle=0) + + def update(self, dt): + self._dt += dt + while self._dt > self._cycle_dt: + self._dt -= self._cycle_dt + self._frame += 1 + self._frame %= len(self._frames) + self.surf = self._frames[self._frame]