“Once I get on a puzzle, I can’t get off.”
Richard P. Feynman
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:
newSurface
) with size (width, height)
.Surface
to newSurface
.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:
Surface
to the DestSurface
.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.
Related Stack Overflow questions:
How do I scale a PyGame image (Surface) with respect to its center?
How to change an image size in Pygame?
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))
See Display, display position, resize, coordinate system and scroll - Scale and zoom window
How to zoom in and out of an image pygame and using the mousposition as the center of the zoom
Related Stack Overflow questions: