How to make a Pygame Zero window full screen?

2024/7/3 23:24:22

I am using the easy-to-use Python library pgzero (which uses pygame internally) for programming games.

How can I make the game window full screen?

import pgzrunTITLE = "Hello World"WIDTH  = 800
HEIGHT = 600pgzrun.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 change the window mode using the pygame API...

Answer

You can access the pygame surface which represents the game screen by screen.surface and you can change the surface in draw() by pygame.display.set_mode(). e.g.:

import pgzrun
import pygameTITLE = "Hello World"WIDTH  = 800
HEIGHT = 600def draw():screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)pgzrun.go()

Or switch to fullscreen when the f key is pressed respectively return to window mode when the w key is pressed in the key down event (on_key_down):

import pgzrun
import pygameTITLE = "Hello World"WIDTH  = 800
HEIGHT = 600def on_key_down(key):if key == keys.F:screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)elif key == keys.W:screen.surface = pygame.display.set_mode((WIDTH, HEIGHT))pgzrun.go()
https://en.xdnf.cn/q/73243.html

Related Q&A

Check if one series is subset of another in Pandas

I have 2 columns from 2 different dataframes. I want to check if column 1 is a subset of column 2.I was using the following code:set(col1).issubset(set(col2))The issue with this is that if col1 has onl…

How to change the head size of the double head annotate in matplotlib?

Below figure shows the plot of which arrow head is very small...I tried below code, but it didnot work... it said " raise AttributeError(Unknown property %s % k) AttributeError: Unknown propert…

Passing a firefox profile to remote webdriver firefox instance not working

Im trying to start up a remote webdriver instance of Firefox and pass in a profile.profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.folderList","2") sel…

Tensorflow 2.3.0 does not detect GPU

The tensorflow does not detect the GPU card. I have following the procedures suggest at Nvidia website and tensorflow/install/gpu. How can I fix it? I am using the following packages and drives: NVIDI…

How to create dict from class without None fields?

I have the following dataclass:@dataclass class Image:content_type: strdata: bytes = bid: str = ""upload_date: datetime = Nonesize: int = 0def to_dict(self) -> Dict[str, Any]:result = {}if…

Is sys.exit equivalent to raise SystemExit?

According to the documentation on sys.exit and SystemExit, it seems thatdef sys.exit(return_value=None): # or return_value=0raise SystemExit(return_value)is that correct or does sys.exit do something …

Vertical overflow of table in live display should scroll the content

Im using a Live display to show the content of a Table which grows over time. Eventually there is a vertical overflow and in that case Id like the oldest (i.e. topmost) rows to vanish while the most re…

Reading KML Files Using Fastkml

Ive searched around quite a bit for this specific Python module, and havent been able to find a source which points me in the right direction.Im trying to read a KML file and display all of the feature…

Adding extra fields to django-registration form

I have a model called "Organization" that Ive setup as a User profile and I would like to have the fields from the "Organization" model show up on the registration page. How do I go…

Need to do a daily log rotation (0utc) using Python

Im an admitted noob to Python. Ive written a little logger that takes data from the serial port and writes it to a log file. Ive got a small procedure that opens the file for append, writes, then close…