PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it

“Once I get on a puzzle, I can’t get off.”
Richard P. Feynman


Scale and zoom

Scale surface

Related Stack Overflow questions:

pygame.transform.scale() does not scale the input Surface itself. It creates a new surface and does a scaled “blit” to the new surface. The new surface is returned by the return value:

pygame.transform.scale() does:

  1. Creates a new surface (newSurface) with size (width, height).
  2. Scale and copy Surface to newSurface.
  3. Return newSurface.

The destination surface is a pygame.Surface where the scaled surface is copied to. That is quicker, because the memory for the Surface has not to be allocated.

It DestSurface is set, then pygame.transform.scale() does:

  1. Scale and copy Surface to the DestSurface.
  2. Return DestSurface.

For that reason, the size of DestSurface has to be (width, height), the format of DestSurface has the same as the format of Surface.

A possible use case is if you have to continuously scale something to a fixed size in the main application loop (e.g. if the surface is dynamically generated). In the following surf is assumed to be a surface object:

while True:
    # [...]

    scaledSurf = pygame.transform.scale(surf, (100, 100)) 
    window.blit(scaledSurf, (x, y)

The code can be improved by using the DestSurface parameter:

scaledSurf = pygame.Surface((100, 100))

while True:
    # [...]

    pygame.transform.scale(surf, (100, 100), scaledSurf) 
    window.blit(scaledSurf, (x, y)

That is probably a rare case. In general you should try to scale the surfaces at the initialization, rather than continuously in the application loop and to use the scaled surfaces in the loop.

Do not try to use the parameter compulsively and do not “construct” a use case for the DestSurface parameter. Do it the other way around. Write your application and make it run. Then investigate whether the DestSurface parameter can be an improvement for your specific use case.

Transform scale and zoom surface

Related Stack Overflow questions:

Define a zoom factor and calculate the size of the of the zoom area. e.g. If the zoom factor is 2, the area that needs to be zoomed on the window is half the width and height of the window:

zoom = 2

wnd_w, wnd_h = window.get_size()
zoom_size = (round(wnd_w/zoom), round(wnd_h/zoom))

Define the rectangular zoom area. The center point of the area is the position where to zoom to. (e.g the cursor position):

zoom_area = pygame.Rect(0, 0, *zoom_size)
zoom_area.center = (pos_x, pos_y)

Create a new pygame.Surface with the size of the zoom area and copy the region of the window to the surface, by using ,blit, where the area parameter is set to the zoom region:

zoom_surf = pygame.Surface(zoom_area.size)
zoom_surf.blit(screen, (0, 0), zoom_area)

Scale zoom_surf by either pygame.transform.scale() or pygame.transform.smoothscale():

zoom_surf = pygame.transform.scale(zoom_surf, (wnd_w, wnd_h))

Now zoom_surf has the same size as the window. .blit the surface to the window:

window.blit(zoom_surf, (0, 0))

Scale inner surface (surface with outline)

Scale and zoom window

See Display, display position, resize, coordinate system and scroll - Scale and zoom window

Zoom collision and hitboxes

Spin effect through scaling

Related Stack Overflow questions: