pgzero icon indicating copy to clipboard operation
pgzero copied to clipboard

Allow assignment to Actor's width and height to scale the Actor

Open lordmauve opened this issue 10 years ago • 12 comments

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

lordmauve avatar Sep 18 '15 10:09 lordmauve

Working on this.

a96tudor avatar Jul 28 '18 09:07 a96tudor

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?

aoloe avatar Sep 18 '18 06:09 aoloe

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.

lordmauve avatar Sep 18 '18 08:09 lordmauve

thanks!

aoloe avatar Sep 18 '18 10:09 aoloe

any update?

aoloe avatar Nov 04 '18 17:11 aoloe

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?

martindemello avatar May 08 '19 07:05 martindemello

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.

ZhangEPSB avatar Jul 07 '19 01:07 ZhangEPSB

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()

aryoda avatar Aug 15 '19 15:08 aryoda

My pull request does use pygame.transform.scale(). It just uses factors of the current dimensions so that the image is not warped.

ZhangEPSB avatar Aug 15 '19 20:08 ZhangEPSB

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 :-)

aryoda avatar Aug 16 '19 12:08 aryoda

Any updates? This issue has existed for 7 years now and there's still no way to scale an actor.

IsaacShelton avatar Jun 11 '22 13:06 IsaacShelton

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()

IsaacShelton avatar Jun 11 '22 14:06 IsaacShelton