Actually add animsurf.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 14:52:58 +0000 (16:52 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 14:52:58 +0000 (16:52 +0200)
koperkapel/actors/animsurf.py [new file with mode: 0644]

diff --git a/koperkapel/actors/animsurf.py b/koperkapel/actors/animsurf.py
new file mode 100644 (file)
index 0000000..fb9e73a
--- /dev/null
@@ -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]