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

Conversation

a96tudor
Copy link

Hey!

Here is my first attempt at solving this issue. Looking forward for you feeedback!

Thanks,
Tudor.

Copy link
Owner

@lordmauve lordmauve left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Does _calc_anchor() also need to change? The resizing should happen from the anchor point. You might need to add a demo game to show how it works.

Also this will need a docs update but that can be added later.

pgzero/actor.py Outdated

@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

pgzero/actor.py Outdated
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._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

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.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


# 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!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants