Search code examples
pythonpygamepygame-surface

Is there a way to resize properly game's screen?


Every time I try to resize the PyGame window, there's no resizing of the game, it keeps like before, and there's just more background.

Any idea about how to resize the game's output with the window (and keeping the ratio, too ), is there a method or do I just need to hand code it?


Solution

  • You have to code it yourself.

    Draw your game not directly on the display surface, but on an intermediate one that you can resize to the display surface's size.

    Here's a simple example:

    import pygame
    from pygame.locals import *
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((400, 300),HWSURFACE|DOUBLEBUF|RESIZABLE)
        game_screen = screen.copy()
        pic = pygame.surface.Surface((50, 50))
        pic.fill('dodgerblue')
    
        while True:
            for event in pygame.event.get():
                if event.type == QUIT: 
                    return
                elif event.type == VIDEORESIZE:
                    ratio = game_screen.get_rect().width / game_screen.get_rect().height
                    new_size = event.size[0], int(event.size[0] / ratio)
                    screen = pygame.display.set_mode(new_size, HWSURFACE|DOUBLEBUF|RESIZABLE)
    
            game_screen.fill('black')
            game_screen.blit(pic, (100, 100))
            screen.blit(pygame.transform.scale(game_screen, screen.get_rect().size), (0, 0))
            pygame.display.flip()
        
    main()