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 scale_up and scale_down function to Actor to resize images #191

Closed
Closed
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
18 changes: 18 additions & 0 deletions doc/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,24 @@ Remember that angles loop round, so 0 degrees == 360 degrees == 720 degrees.
Likewise -180 degrees == 180 degrees.


Scaling
'''''''
.. versionadded:: 1.3

To resize and Actor, use ``scale_up`` and ``scale_down``. These will change the
dimensions of the Actor by a given factor. For example, ``scale_up(2)``
will increase Actor's width and height 2x.::

alien = Actor('alien')
alien.scale_up(2)

def draw():
alien.draw()

Scaling the Actor does not change the file size of the image. Significantly
scaling images with large file sizes will not reduce the memory taken by the
images.::

Distance and angle to
'''''''''''''''''''''

Expand Down
13 changes: 13 additions & 0 deletions pgzero/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,18 @@ def distance_to(self, target):
dy = ty - myy
return sqrt(dx * dx + dy * dy)

def scale_up(self, value):
"""Scales up the image by a factor of the inputted value."""
self._orig_surf = pygame.transform.scale(self._surf, (int(self._surf.get_width() * value), int(self._surf.get_height() * value)))
self._surf = self._orig_surf
self._update_pos()

def scale_down(self, value):
"""Scales down the image by a factor of the inputted value."""
self._orig_surf = pygame.transform.scale(self._surf, (self._surf.get_width() // value, self._surf.get_height() // value))
self._surf = self._orig_surf
self._update_pos()


def unload_image(self):
loaders.images.unload(self._image_name)