Allow assignment to Actor's width and height to scale the Actor
Originally reported by: Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve)
Assigning to height and width could cause the actor to be scaled.
This might scale around the 'pos' position.
- Bitbucket: https://bitbucket.org/lordmauve/pgzero/issue/36
Working on this.
yes, that's the biggest stepping stone i've encountered until now, while thinking about the migration path from scratch to pygame zero...
is any action (testing, ...) needed for this patch being added to the master?
When I last looked, the patch had some issues causing the pos point to move. I'll look again at this at the Pycon UK sprints tomorrow.
thanks!
any update?
setting width and height is slightly complicated because they refer to the bounding box of the image. i've added support for setting .dimensions in #185, which refers to the dimensions of the image before rotation, and scales it around the anchor point. is there still a use case for scaling a rotated image to an exact bounding box size?
I submitted a pull request that may mitigate this issue. It doesn't let the user choose a pixel width and height, but it does allow them to scale the image (2x bigger/smaller). It may not be as specific, but it will prevent users from distorting the image when hard-coding width and height. Tested with colliderect and rotate. Hope it helps.
Scaling the image would be great, eg. via using pygame.transform.scale():
def resize_actor(actor, new_width, new_heigth):
# Resize and update the anchor point
# See: https://www.pygame.org/docs/ref/transform.html#pygame.transform.scale
actor._surf = pygame.transform.scale(actor._surf, (new_width, new_heigth))
actor._update_pos()
My pull request does use pygame.transform.scale(). It just uses factors of the current dimensions so that the image is not warped.
Minimizing image distortion by allowing only whole-number factors is a good thing.
My use case is flexibility, eg. for maze-alike games like pacman where your images and actors must be resized to the same tile size of the "game grid".
If I have to use only images with the correct pixel size
- I either have to edit each image if my images have the wrong image sizes
- or I have to find new images
Furthermore games should support different screen resolutions and therefore scaling of the game window and the images.
Just my opinion, still pygame zero is of great use and I love using it :-)
Any updates? This issue has existed for 7 years now and there's still no way to scale an actor.
For anyone visiting this
The only way I was able to get it to work was by using the images object to load the image and then scale the result.
background = pygame.transform.scale(images.background, (WIDTH, HEIGHT))
So then I was able to blit the image itself onto the screen.
screen.blit(background, (0, 0))
Example
import pygame
import pgzrun
WIDTH = 600
HEIGHT = 600
background = pygame.transform.scale(images.background, (WIDTH, HEIGHT))
def draw():
screen.blit(background, (0, 0))
pgzrun.go()