Search code examples
pythonpygamegame-engine

How do you scale a design resolution to other resolutions with Pygame?


So in every Pygame example I've seen, there seems to be a resolution the game is designed to be played on and that's it. No options for setting higher or lower resolutions. If you do change the resolution via display.set_mode, then the scale of the game textures/graphics get out of whack and the game becomes unplayable.

In all the examples I've seen, it looks like you would have to actually create different sized texture sets for each target resolution... Which seems ridiculous. That leads me to my question.

If I design a game based on a standard resolution of 480x270, is it possible to simply scale the output of that surface to 1080p (or 1440p, 4k, etc) while still utilizing the game assets built for 480x270? If so, can anyone post an example of how this can be accomplished? Any tips or pointers would also be appreciated.


Solution

  • You could make a dummy surface at the design resolution and scale it to the resolution the game is running at:

    window = pygame.set_mode([user_x, user_y])
    w = pygame.Surface([design_x, design_y])
    
    def draw():
        frame = pygame.transform.scale(w, (user_x, user_y))
        window.blit(frame, frame.get_rect())
        pygame.display.flip()
    

    Draw everything on the dummy surface and it will be scaled to the screen. This requires a fixed screen height to screen width ratio.