How to transform current pygame surface to a larger one?

Viewed 110

Initially I am creating a game surface with dimension WIDTH and HEIGHT

pygame.init()
SCREEN1 = pygame.display.set_mode((WIDTH,HEIGHT))
sprites = pygame.sprite.Group()
sprites.add( ..... #add sprite
...
...

sprites.draw(SCREEN)
SCREEN.blit("test", (WIDTH-4, HEIGHT-2))
pygame.display.flip()
clock.tick(FPS)

How can I transform SCREEN1 to SCREEN2 with a scale of x2? I know there is pygame.transform.scale, I tried the following but it does not work. It only shows an empty window with new dimension.

pygame.init()
SCREEN1 = pygame.display.set_mode((WIDTH,HEIGHT))
sprites = pygame.sprite.Group()
sprites.add( ..... #add sprite
...
...

sprites.draw(SCREEN)
SCREEN.blit("test", (WIDTH-4, HEIGHT-2))

SCREEN2=pygame.display.set_mode((WIDTH*2,HEIGHT*2))
pygame.transform.scale(SCREEN, (WIDTH*2,HEIGHT*2), SCREEN2)

pygame.display.flip()
clock.tick(FPS)

May I know what is the proper way to do it? Thanks.

1 Answers

SCREEN is the display pygame.Surface which is associated to the window. When pygame.display.set_mode() is executed, then a new display surface is generated and the date in the previous display surface is lost. Create a copy of the surface (pygame.Surface.copy()) before you generate a new display surface:

old_screen = SCREEN.copy()

SCREEN2 = pygame.display.set_mode((WIDTH*2,HEIGHT*2))
pygame.transform.scale(old_screen, (WIDTH*2,HEIGHT*2), SCREEN2)
Related