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

Fixed #36 - Allow assignment to Actor's width and height to scale the Actor #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 54 additions & 1 deletion pgzero/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def calculate_anchor(value, dim, total):
return float(value)


class InvalidScaleException(Exception):
"""The scale parameters where invalid."""
pass


# These are methods (of the same name) on pygame.Rect
SYMBOLIC_POSITIONS = set((
"topleft", "bottomleft", "topright", "bottomright",
Expand Down Expand Up @@ -85,6 +90,7 @@ def __init__(self, image, pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, **kwargs):
# Initialise it at (0, 0) for size (0, 0).
# We'll move it to the right place and resize it later

self._scale_x = self._scale_y = 1
self.image = image
self._init_position(pos, anchor, **kwargs)

Expand Down Expand Up @@ -170,14 +176,16 @@ def angle(self):
@angle.setter
def angle(self, angle):
self._angle = angle
self._adjust_scale_and_angle(self._scale_x, self._scale_y)
"""
self._surf = pygame.transform.rotate(self._orig_surf, angle)
p = self.pos
self.width, self.height = self._surf.get_size()
w, h = self._orig_surf.get_size()
ax, ay = self._untransformed_anchor
self._anchor = transform_anchor(ax, ay, w, h, angle)
self.pos = p

"""
@property
def pos(self):
px, py = self.topleft
Expand Down Expand Up @@ -216,6 +224,51 @@ def image(self):
def image(self, image):
self._image_name = image
self._orig_surf = self._surf = loaders.images.load(image)
self._adjust_scale_and_angle(self._scale_x, self._scale_y)
self._update_pos()

@property
def scale(self):
return self._scale_x, self._scale_y

@scale.setter
def scale(self, scale):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd quite like scale to accept a single int/float, as a shortcut for setting both values.

Actually I would expect that is the more common case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self._adjust_scale_and_angle(scale[0], scale[1])

@property
def scale_x(self):
return self._scale_x

@scale_x.setter
def scale_x(self, x):
self._adjust_scale_and_angle(x, 1.0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not expect setting scale_x to reset scale_y to 1.0.

actor.scale_x = 0.5
actor.scale_y = 0.5

should be equivalent to

actor.scale = 0.5

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


@property
def scale_y(self):
return self._scale_y

@scale_y.setter
def scale_y(self, y):
self._adjust_scale_and_angle(1.0, y)

@property
def size(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're taking out size, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sorry, my bad

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a96tudor just stumbled across this PR and looks like this is the only requested change that hasn't been addressed?

return self._surf.get_size()

def _adjust_scale_and_angle(self, x, y):
if x == 0 or y == 0:
raise InvalidScaleException('Invalid scale values. They should be not equal to 0.')

self._scale_x = x
self._scale_y = y

new_width = int(self._orig_surf.get_size()[0] * abs(x))
new_height = int(self._orig_surf.get_size()[1] * abs(y))

self._surf = pygame.transform.scale(self._orig_surf, (new_width, new_height))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should special-case the no-op case for performance for each of these - a check on whether scale_ == scale_y == 1 is much cheaper than doing the transform, which will copy a surface. Same with rotation and flip.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self._surf = pygame.transform.flip(self._surf, (x < 0), (y < 0))
self._surf = pygame.transform.rotate(self._surf, self._angle)

self._update_pos()

def _update_pos(self):
Expand Down
68 changes: 66 additions & 2 deletions test/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pygame

from pgzero.actor import calculate_anchor, Actor
from pgzero.loaders import set_root
from pgzero.actor import calculate_anchor, Actor, InvalidScaleException
from pgzero.loaders import set_root, images


TEST_MODULE = "pgzero.actor"
Expand Down Expand Up @@ -39,6 +39,12 @@ class ActorTest(unittest.TestCase):
def setUpClass(self):
set_root(__file__)

def assertImagesEqual(self, a, b):
adata, bdata = (pygame.image.tostring(i, 'RGB') for i in (a, b))

if adata != bdata:
raise AssertionError("Images differ")

def test_sensible_init_defaults(self):
a = Actor("alien")

Expand Down Expand Up @@ -107,3 +113,61 @@ def test_rotation(self):
for _ in range(360):
a.angle += 1.0
self.assertEqual(a.pos, (100.0, 100.0))

def test_scaling(self):
actor = Actor('alien')
originial_size = actor.size

# No scaling
actor.scale = (1, 1)
self.assertEqual(actor.size, originial_size)

# Scaling on x-axis only
actor.scale_x = 2
self.assertEqual(actor.size, (originial_size[0] * 2, originial_size[1]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great looking tests!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!


# Scaling on y-axis only
actor.scale_y = 2
self.assertEqual(actor.size, (originial_size[0], originial_size[1] * 2))

# Scaling down
actor.scale = (.5, .5)
self.assertEqual(actor.size, (originial_size[0]/2, originial_size[1]/2))

# Test scale getters
self.assertEqual(actor.scale, (actor.scale_x, actor.scale_y))

# Rotate and then scale
actor.angle = 90
actor.scale = (.5, .5)
self.assertEqual(actor.angle, 90)
self.assertEqual((actor.width, actor.height), (originial_size[1]/2, originial_size[0]/2))
self.assertEqual(actor.topleft, (-13, 13))

# Test rasing exception for invalid scale parameters
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in 'raising'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

with self.assertRaises(InvalidScaleException) as cm:
actor.scale = (0, -2)
self.assertEqual(cm.exception.args[0], 'Invalid scale values. They should be not equal to 0.')

# Test horizontal flip
actor.angle = 0
orig = images.load('alien')
exp = pygame.transform.flip(orig, True, False)
actor.scale = (-1, 1)
self.assertImagesEqual(exp, actor._surf)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tip is "one assert per test". This means that if a test fails, all of the other tests still run, which gives more information, when refactoring, about what you might have broken.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


# Test vertical flip
exp = pygame.transform.flip(orig, False, True)
actor.scale = (1, -1)
self.assertImagesEqual(exp, actor._surf)

# Test horizontal + vertical flip
exp = pygame.transform.flip(orig, True, True)
actor.scale = (-1, -1)
self.assertImagesEqual(exp, actor._surf)

# Test flip + scaling
exp = pygame.transform.scale(orig, (orig.get_size()[0]*3, orig.get_size()[1]*2))
exp = pygame.transform.flip(exp, True, True)
actor.scale = (-3, -2)
self.assertImagesEqual(exp, actor._surf)