Many-angled roaches
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 10:20:39 +0000 (12:20 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 10:20:50 +0000 (12:20 +0200)
koperkapel/actors/orientatedsurf.py [new file with mode: 0644]
koperkapel/roaches.py
koperkapel/scenes/level.py

diff --git a/koperkapel/actors/orientatedsurf.py b/koperkapel/actors/orientatedsurf.py
new file mode 100644 (file)
index 0000000..5c8df65
--- /dev/null
@@ -0,0 +1,35 @@
+""" Actor that takes a surface. """
+
+from pygame.transform import rotate
+from pgzero.actor import POS_TOPLEFT, ANCHOR_CENTER
+
+from .surf import SurfActor
+
+class OrientatedSurfActor(SurfActor):
+    def __init__(self, surf, pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, angle=0, **kwargs):
+        self._handle_unexpected_kwargs(kwargs)
+
+        self._angle = angle
+        self._base_surf = surf
+        super().__init__(surf, pos, anchor)
+        self._init_position(pos, anchor, **kwargs)
+
+    @property
+    def angle(self):
+        return self._angle
+
+    @angle.setter
+    def angle(self, angle):
+        self._angle = angle
+        self._surf = self._orig_surf = rotate(self._base_surf, angle)
+
+    @property
+    def surf(self):
+        """ The original surface displaying the actor. """
+        return self._orig_surf
+
+    @surf.setter
+    def surf(self, surf):
+        self._base_surf = surf
+        self._orig_surf = self._surf = rotate(surf, self._angle)
+        self._update_pos()
index 653f085aa6a39f4a64ebc2d9d89311da2dad0f42..0d2448216c16a00900759736ed66e32fe975dbcb 100644 (file)
@@ -4,18 +4,18 @@ import random
 from pgzero.clock import each_tick
 from pgzero.loaders import images
 from pygame.constants import BLEND_RGBA_MULT
-from .actors.surf import SurfActor
+from .actors.orientatedsurf import OrientatedSurfActor
 from .serums import roach_serum_color
 
 
-class RoachActor(SurfActor):
+class RoachActor(OrientatedSurfActor):
     def __init__(self, frames):
         self._frames = frames
         self._frame = random.randint(0, len(frames) - 1)
         self._dt = 0
         self._cycle_dt = 0.2
         each_tick(self.update)
-        super().__init__(surf=frames[self._frame])
+        super().__init__(surf=frames[self._frame], angle=0)
 
     def update(self, dt):
         self._dt += dt
index 867eaacebea738a350fc27c73cdc94d870525c60..2484119350570d93fdfb49b37f5d0c02106876d6 100644 (file)
@@ -87,6 +87,10 @@ class GameLevelScene(BaseLevelScene):
         elif self._mode == 'crawl':
             return self._level.can_crawl(x, y, self._level_layer)
 
+    def _set_angle(self, angle):
+        for roach in self._roaches:
+            roach.angle = angle
+
     def on_key_down(self, key, mod, unicode):
         offset = None
         x, y = self._player_pos
@@ -94,18 +98,22 @@ class GameLevelScene(BaseLevelScene):
             if self._can_move(x, y + 1):
                 self._set_pos(x, y + 1)
                 offset = (0, TILE_SIZE)
+                self._set_angle(180)
         elif key == keys.UP:
             if self._can_move(x, y - 1):
                 self._set_pos(x, y - 1)
                 offset = (0, -TILE_SIZE)
+                self._set_angle(0)
         elif key == keys.LEFT:
             if self._can_move(x - 1, y):
                 self._set_pos(x - 1, y)
                 offset = (-TILE_SIZE, 0)
+                self._set_angle(90)
         elif key == keys.RIGHT:
             if self._can_move(x + 1, y):
                 self._set_pos(x + 1, y)
                 offset = (TILE_SIZE, 0)
+                self._set_angle(270)
         elif key == keys.S:
             if self._level_layer == 'floor' and self._level.can_crawl(x, y, 'floor'):
                 # We're probably on a grate