Change size of window but center on monitor screen

Question:

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

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
Asked By: Braeden Lisowski

||

Answers:

As @Jerry suggested in the comments you can use pygame.display.quit() right before you call pygame.display.set_mode((width, height)) or even simpler, you can simply use pygame.quit().

Here is a simple example :

import pygame as pg
import sys

def screen_resize(amount):
    # I dislike using the "global" keyword but I wanted to keep my example short
    global screen
    new_size = [screen.get_width() + amount, screen.get_height() + amount]

    # If the new size is too small or too big I simple exit the function
    if new_size[0] > MAX_SIZE: return
    if new_size[0] < MIN_SIZE: return

    # pg.quit() before resizing so that the new display is centered to your screen
    pg.quit()
    screen = pg.display.set_mode(new_size)

if __name__ == "__main__":
    MIN_SIZE, MAX_SIZE = 100, 500
    screen = pg.display.set_mode([MIN_SIZE, MIN_SIZE])

    while True:
        for evt in pg.event.get():
            if evt.type == pg.QUIT:
                pg.quit()
                sys.exit()
            
            # I resize the window when I press "p" or "m" ("p" for "plus", "m" for "minus")
            elif evt.type == pg.KEYDOWN:
                if evt.key == pg.K_p:
                    screen_resize(100)
                elif evt.key == pg.K_m:
                    screen_resize(-100)

I’m sure you can figure out a way to fit this principle into your project. If the answer isn’t clear, feel free to ask for clarifications and I will edit my answer.

Answered By: Anto

https://github.com/pygame/pygame/issues/3464

This is an issue with pygame and should be fixed in the next release.

Answered By: Braeden Lisowski
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.