Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for flipping Actor images horizontally and vertically. #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,29 @@ be floats or the strings ``left``, ``center``/``middle``, ``right``, ``top`` or
``bottom`` as appropriate.


.. _flipping:

Flipping
''''''''

Actors can be flipped, so that their image is mirrored horizontally or
vertically (or both). You can use this to save creating and loading a
separate set of images if you want an Actor to move in both directions.

To flip an Actor horizontally, just set its ``flip`` property to ``True``.
Since our alien faces right, the following will make it face left::

alien = Actor('alien')
alien.flip = True

If you want the alien to flip vertically too (maybe it can walk on the
ceiling?), pass it a tuple ``(horizontal, vertical)`` with the vertical value
set to True::

alien.flip = (True, True)
alien.flip = (False, True)


.. _rotation:

Rotation
Expand Down
22 changes: 19 additions & 3 deletions pgzero/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class Actor:

_anchor = _anchor_value = (0, 0)
_angle = 0.0

_flip = (False, False)

def __init__(self, image, pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, **kwargs):
self._handle_unexpected_kwargs(kwargs)

Expand Down Expand Up @@ -127,7 +128,9 @@ def _init_position(self, pos, anchor, **kwargs):
if anchor is None:
anchor = ("center", "center")
self.anchor = anchor


self.flip = (False, False)

symbolic_pos_args = {
k: kwargs[k] for k in kwargs if k in SYMBOLIC_POSITIONS}

Expand Down Expand Up @@ -184,7 +187,15 @@ def angle(self, angle):
ax, ay = self._untransformed_anchor
self._anchor = transform_anchor(ax, ay, w, h, angle)
self.pos = p

@property
def flip(self):
return self._flip

@flip.setter
def flip(self, horizontal, vertical=False):
self._flip = (bool(horizontal), bool(vertical))

@property
def pos(self):
px, py = self.topleft
Expand Down Expand Up @@ -232,7 +243,12 @@ def _update_pos(self):
self.pos = p

def draw(self):
game.screen.blit(self._surf, self.topleft)
if self._flip == (False, False):
game.screen.blit(self._surf, self.topleft)
else:
flipped = pygame.transform.flip(self._surf, *self.flip)
game.screen.blit(flipped, self.topleft)


def angle_to(self, target):
"""Return the angle from this actors position to target, in degrees."""
Expand Down