From 663cf50f914ef479622e517463f4b03604c297c9 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 5 Mar 2016 12:20:39 +0200 Subject: [PATCH] Many-angled roaches --- koperkapel/actors/orientatedsurf.py | 35 +++++++++++++++++++++++++++++ koperkapel/roaches.py | 6 ++--- koperkapel/scenes/level.py | 8 +++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 koperkapel/actors/orientatedsurf.py diff --git a/koperkapel/actors/orientatedsurf.py b/koperkapel/actors/orientatedsurf.py new file mode 100644 index 0000000..5c8df65 --- /dev/null +++ b/koperkapel/actors/orientatedsurf.py @@ -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() diff --git a/koperkapel/roaches.py b/koperkapel/roaches.py index 653f085..0d24482 100644 --- a/koperkapel/roaches.py +++ b/koperkapel/roaches.py @@ -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 diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 867eaac..2484119 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -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 -- 2.34.1