Change size of window but center on monitor screen

Viewed 22

I want to start the program off with a smaller window say 100x100 and resize based off of what a user does with the small screen. In the example below it resizes to 800x800.

An issue I am running into is that upon resizing it will not be center on the monitor and depending on what size I resize to it will be off the monitor. Is there a way to resize the pygame window in a way that keeps it center on the monitor screen?

Most answers I can find are to make the game full screen, but I want to avoid that. I also found a workaround Pygame Display Position While Running where you quit and init again, but that seems non-ideal.

Here is code demonstrating the issue:

import pygame

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode([100, 100])
    screen.fill((255, 255, 255))
    pygame.display.update()
    menu = True
    size = [-1, -1]
    while menu:
        for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            menu = False
            size = [800, 800]
    screen = pygame.display.set_mode(size)
    screen.fill((255, 255, 255))
    game = True
    while game:
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                game = False
0 Answers
Related