2

I am using the easy-to-use Python library pgzero for programming games.

How can I center the game window? )It always opens at a random position...

import pgzrun

TITLE = "Hello World"

WIDTH  = 800
HEIGHT = 600

pgzrun.go()

Note: I am using the runtime helper lib pgzrun to make the game executable without an OS shell command... It implicitly imports the pgzero lib...

Edit: pgzero uses pygame internally, perhaps there is a way to center the window using the pygame API...

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
R Yoda
  • 8,358
  • 2
  • 50
  • 87

1 Answers1

2

You can use os.environ['SDL_VIDEO_CENTERED'] = '1'

This line of code tries to center your window the best it can.

Try this:

import pgzrun
import os

TITLE = "Hello World"

WIDTH  = 800
HEIGHT = 600

os.environ['SDL_VIDEO_CENTERED'] = '1'
pgzrun.go()
GOVIND DIXIT
  • 1,748
  • 10
  • 27
  • 1
    I have a problem using this command with my students that just download the latest version of pygames. `os.environ['SDL_VIDEO_CENTERED'] = '1'` Works for me I think because I running 1.9.6, but it doesn't work for any of my students that are running pygames 2.0.1. Is there a different syntax for this with the latest version of pygames? – Amy Brown Nov 04 '21 at 19:55