pygame.display

Open and configure the game window, show each finished frame, switch to fullscreen and scale low-resolution games cleanly.

3 min readUpdated
On this page

Overview#

pygame.display controls the window. set_mode() creates it and returns the display Surface, the image that becomes visible each time you call flip() or update(). Most programs call set_mode() once and keep the returned Surface in a variable called screen.

Syntax#

Python
screen = pygame.display.set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
pygame.display.flip()
pygame.display.update(rectangle=None)        # or a list of rectangles
pygame.display.set_caption(title)

Parameters of set_mode#

ParameterMeaning
size(width, height) in pixels. (0, 0) uses the current desktop resolution.
flagsOptions combined with |, see below.
depthColour depth in bits. Leave it at 0 and Pygame picks the best value.
displayWhich monitor to open on (0 is the primary one).
vsync1 asks for vertical sync to avoid tearing. Support depends on the platform and flags; with SCALED or OPENGL it is most reliable.

Common flags:

FlagEffect
pygame.FULLSCREENFullscreen at the given size.
pygame.RESIZABLEThe player can resize the window; you receive VIDEORESIZE and window events.
pygame.SCALEDYour game draws at size but the window is scaled up to fit the desktop. Mouse positions are converted back to game coordinates. Ideal for pixel art.
pygame.NOFRAMEA window without borders or title bar.
pygame.OPENGLCreate an OpenGL context (for use with PyOpenGL).
pygame.HIDDEN / pygame.SHOWNStart hidden or visible.

Return values#

set_mode() returns the display Surface. flip() and update() return None. Calling set_mode() again replaces the window and invalidates the old Surface.

Other useful functions#

FunctionWhat it does
get_surface()Returns the current display Surface from anywhere in your code.
set_caption(title)Sets the window title.
set_icon(surface)Sets the window icon; call before set_mode() on some platforms.
toggle_fullscreen()Switches between windowed and fullscreen.
get_window_size()Actual window size, which can differ from the Surface size with SCALED.
get_desktop_sizes()Sizes of all connected monitors.
Info()Object with details such as current_w, current_h and bitsize.
get_driver()Name of the video driver in use.

flip() versus update()#

flip() shows the whole display Surface. update() with no argument does the same; update(rect) or update(list_of_rects) refreshes only those areas, which can save time in software rendering when little changes. For most games, call flip() once per frame. Never call both in the same frame.

Practical example#

display_example.pyDownload
"""pygame.display: window size, caption, SCALED for crisp pixel art, and fullscreen toggling."""
import pygame

pygame.init()
print("Desktop sizes:", pygame.display.get_desktop_sizes())

# A 320x180 game drawn at that resolution but shown in a larger window.
# SCALED lets Pygame stretch the image to the window and keeps mouse coordinates correct.
screen = pygame.display.set_mode((320, 180), pygame.SCALED | pygame.RESIZABLE)
pygame.display.set_caption("Display demo - F toggles fullscreen")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 20)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
            pygame.display.toggle_fullscreen()

    screen.fill((30, 40, 70))
    w, h = screen.get_size()
    pygame.draw.rect(screen, (244, 197, 49), (10, 10, w - 20, h - 20), 2)
    info = pygame.display.Info()
    screen.blit(font.render(f"Surface {w}x{h}  window {pygame.display.get_window_size()}", True, "white"), (16, 16))
    screen.blit(font.render(f"Driver: {pygame.display.get_driver()}  bits: {info.bitsize}", True, "white"), (16, 36))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

This opens a 320 × 180 game surface in a scaled window. Press F to toggle fullscreen. Drawing coordinates stay 320 × 180 whatever the window size.

Common errors#

  • pygame.error: video system not initialized: you called a display function before pygame.init() or after pygame.quit().
  • pygame.error: No video mode has been set: convert() or convert_alpha() ran before set_mode().
  • Black or frozen window: you never call flip(), or you never process events. See black screen and window not responding.
  • Blurry window on Windows high-DPI displays: Windows may scale old-style apps. Using SCALED, or marking the process as DPI-aware, helps; see screen sizes and scaling.

pygame-ce notes#

pygame-ce also offers pygame.Window, an object-oriented API for creating and managing one or more windows. It is not available in the original pygame, so code that uses it requires pygame-ce. set_mode() works the same in both.

Official documentation#

↑ ↓ to moveEnter to openEsc to close