Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame_Zero window positioning

Tags:

python

pgzero

I am working with pygamezero using an editor called MU (this has the pgzero module built in). When the code is executed the top left of the game window spawns from the centre of the screen and, depending on the dimensions provided by the user for height and width and their screen resolution, portions of the window often appears "off screen". I have found a method - using pygame - that evokes full screen, but am wondering if there is a method to set starting x/y coords of the game screen, so that it is not full-screen, but the window spawn position can be controlled.

like image 268
Darryl Humphreys Avatar asked Feb 12 '26 11:02

Darryl Humphreys


2 Answers

To control the window position in Pygame Zero (pgzrun) make use of os.environ['SDL_VIDEO_WINDOW_POS'] as shown below. It must happen before the import pgzrun call, otherwise it won't work.

FYI If you're using Thonny--a teaching & learning IDE--you need to disable Pygame Zero mode from the Run menu to control where the window appears. If you don't disable the mode, Thonny will override your choice by implicitly importing (& thus executing) pgzrun.

The following code snippet will place your window at the top-left corner of the screen. You can also make the window go fullscreen in a different way but that doesn't require os.environ.

x = 0
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = f'{x},{y}'

import pgzrun
pgzrun.go()

Note: This question has also been (pretty much) answered elsewhere. Because this question and answer specifically relate to Pygame Zero I'm adding this here so anyone looking specifically for a pygame zero solution can find it.

like image 107
Eric D Avatar answered Feb 14 '26 00:02

Eric D


while, in MU editor, you can simply add a Python 3 environment variable SDL_VIDEO_WINDOW_POS=0,0 in the configuration settings. This makes the default Pygame Zero game window top-left corner to the screen top-left corner position (0,0) enter image description here

like image 28
Benny Chen Avatar answered Feb 14 '26 01:02

Benny Chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!