Handling screen sizes and scaling in Pygame
Players have small laptops, huge monitors and everything in between. Here are three ways to make one game look right on all of them.
On this page
Pick a logical resolution#
Design your game for one fixed size, the logical resolution, such as 960 × 540 or 320 × 180 for pixel art. All game logic, positions and UI use these coordinates. Scaling to the player's screen happens at the very end. Choosing 16:9 suits most monitors.
Option 1: the SCALED flag (easiest)#
screen = pygame.display.set_mode((320, 180), pygame.SCALED)Pygame creates a window as large as fits the desktop at an integer multiple where possible, scales your 320 × 180 surface up, and converts mouse positions back to logical coordinates automatically. Add pygame.RESIZABLE to let players resize, and pygame.display.toggle_fullscreen() to switch to fullscreen. The display reference has a working example.
Option 2: scale it yourself with letterboxing#
When you need full control, draw on a fixed-size Surface and scale it into the real window, keeping the aspect ratio and filling the rest with black bars:
GAME_W, GAME_H = 960, 540
window = pygame.display.set_mode((GAME_W, GAME_H), pygame.RESIZABLE)
game = pygame.Surface((GAME_W, GAME_H))
def present():
win_w, win_h = window.get_size()
scale = min(win_w / GAME_W, win_h / GAME_H)
size = (int(GAME_W * scale), int(GAME_H * scale))
frame = pygame.transform.smoothscale(game, size) # use scale() for pixel art
window.fill("black")
window.blit(frame, frame.get_rect(center=(win_w // 2, win_h // 2)))
pygame.display.flip()Mouse positions now need converting back: subtract the bar offset and divide by scale.
Option 3: truly responsive layouts#
Strategy games and tools sometimes show more of the world on bigger screens instead of scaling. Handle pygame.VIDEORESIZE (or window size events), recompute layout from the new size, and keep UI anchored to edges (rect.bottomright = (w - 10, h - 10)). This is the most work, so choose it only if your game benefits.
Fullscreen#
pygame.display.toggle_fullscreen()switches the current window.pygame.FULLSCREEN | pygame.SCALEDgives a scaled fullscreen at the desktop resolution without changing the monitor's mode, which avoids black-screen flicker on many systems.pygame.display.get_desktop_sizes()lists monitor resolutions.
Always let players leave fullscreen (a key such as F11 or an option in the menu).
High-DPI displays on Windows#
With display scaling at 150%, Windows may enlarge your window and blur it. Telling Windows your game handles DPI itself prevents that. A common approach, run before pygame.init():
import sys
if sys.platform == "win32":
import ctypes
try:
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except (AttributeError, OSError):
passThen your window has real pixel sizes, so pair it with SCALED or your own scaling to keep the game a comfortable size.