1
\$\begingroup\$

How can I resize a Surface object in Pygame? Is there a way to bind a surface object into a Rect object, so that resizing the rect would also resize the surface (with Rect.inflate_ip method for an example)?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There's no way to do this automatically, so if you want some sort of automation for the surface rescaling, you'd need to write your own wrapper class around pygame.Rect, that owns a rectangle, and takes in a surface, and resizes the surface when a method of the wrapper is called.

The resizing function you are looking for is pygame.transform.scale. Pass it a surface and the new size you want, and it will return a new surface, or place the result into an already created surface (this is faster).

\$\endgroup\$
2
  • \$\begingroup\$ Is scale method not only for resizing of images at the toplevel? Is it ok to use it continuously (say by setting a timer) to resize many surfaces in the main loop? \$\endgroup\$
    – user78884
    Commented Nov 29, 2018 at 14:41
  • \$\begingroup\$ You can use it wherever, just note that it can be a bit slow, but that is unavoidable with a software renderer like pygame. \$\endgroup\$
    – user35344
    Commented Nov 29, 2018 at 15:33

You must log in to answer this question.