--- /dev/null
+""" 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()
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
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
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