Pygame Display Position

I need x,y coords of the pygame window – either at start or on window move. The last one is nice to have.

I figured out how to center the pygame window at the bottom of the screen:

    pos_x = screen_width / 2 - window_width / 2
    pos_y = screen_height - window_height
    os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y)
    os.environ['SDL_VIDEO_CENTERED'] = '0'

Background: I have x,y coords which are screen related and I must convert the screen coords into window-local coords so that I can use them e.g. to display coords inside the pygame window or to discard coords which are outside the pygame window.

With my approach above, I knwo the initial position. But I can only use a single pygame window (because it’s always at the same position) and things go wrong if the user moves the window.

Leave a Comment