Skip to content

Commit

Permalink
Relative sprite scaling (pythonarcade#492)
Browse files Browse the repository at this point in the history
* Add relative rescale method to Sprite

* Add rescale method to SpriteList
  • Loading branch information
sbischoff-ai authored and pvcraven committed Oct 5, 2019
1 parent ebc1b68 commit 1fa3820
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions arcade/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ def _set_scale(self, new_value: float):

scale = property(_get_scale, _set_scale)

def rescale_relative_to_point(self, point: Point, factor: float) -> None:
""" Rescale the sprite relative to a different point than its center. """
self.scale *= factor
self.center_x = (self.center_x - point[0]) * factor + point[0]
self.center_y = (self.center_y - point[1]) * factor + point[1]

def _get_center_x(self) -> float:
""" Get the center x coordinate of the sprite. """
return self._position[0]
Expand Down
13 changes: 13 additions & 0 deletions arcade/sprite_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@ def update_animation(self, delta_time: float = 1/60):
for sprite in self.sprite_list:
sprite.update_animation(delta_time)

def _get_center(self) -> Tuple[float, float]:
""" Get the mean center coordinates of all sprites in the list. """
x = sum((sprite.center_x for sprite in self.sprite_list)) / len(self.sprite_list)
y = sum((sprite.center_y for sprite in self.sprite_list)) / len(self.sprite_list)
return x, y

center = property(_get_center)

def rescale(self, factor: float) -> None:
""" Rescale all sprites in the list relative to the spritelists center. """
for sprite in self.sprite_list:
sprite.rescale_relative_to_point(self.center, factor)

def move(self, change_x: float, change_y: float):
"""
Moves all Sprites in the list by the same amount.
Expand Down

0 comments on commit 1fa3820

Please sign in to comment.