6

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 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 change the window mode using the pygame API...

R Yoda
  • 8,358
  • 2
  • 50
  • 87

2 Answers2

10

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 pygame

TITLE = "Hello World"

WIDTH  = 800
HEIGHT = 600

def 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 pygame

TITLE = "Hello World"

WIDTH  = 800
HEIGHT = 600

def 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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Great answer :-) I forgot to ask how to escape from the black screen I get now after running the code. What is the default mode (I would add a key handler then to end the "game" and seeing the normal desktop again)... – R Yoda Aug 16 '19 at 10:32
  • 1
    @RYoda you can use `on_key_down` to switch between fullscreen and window mode. I've extended the answer. – Rabbid76 Aug 16 '19 at 10:44
  • 2
    @RYoda Ctrl-Q will always quit in any Pygame Zero game (might be Cmd-Q on Mac). – Mauve Aug 18 '19 at 11:25
  • The first example above (without the key handling) is very flashy, but if you only set screen.surface the first time draw() is called, it works great! – michael Nov 09 '19 at 22:10
-2
import pgzrun
import pygame

TITLE = "Hello World"

WIDTH  = 800
HEIGHT = 600

def draw():
    screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)

pgzrun.go()
Jimit Vaghela
  • 768
  • 1
  • 8
  • 31
ezza
  • 1
  • 1
  • 1
    Welcome at SO! Could you please explain your answer a little bit more since it looks it contains exactly the same code of the already accepted answer by Rabbid76? THX! – R Yoda Jun 17 '21 at 20:36