How to structure a Pygame project
A single file is perfect for small games. When yours outgrows it, here's a simple layout for code, assets and settings that scales without becoming a maze.
On this page
Start with one file#
Every game on this site is a single file of 150 to 250 lines, and that's a good size: you can read it top to bottom and nothing is hidden. Keep the same order inside it as the projects do: imports, constants, classes for game objects, a game class, then the main loop. Split into several files when scrolling becomes the main way you navigate, typically somewhere past 400–500 lines.
A layout that scales#
star_catcher/
├── main.py # entry point: creates the window and runs the loop
├── settings.py # constants: sizes, colours, speeds, key bindings
├── game/
│ ├── __init__.py
│ ├── scenes.py # menu, playing, paused, game over
│ ├── player.py # Player sprite
│ ├── enemies.py # enemy sprites
│ └── assets.py # loads and caches images, sounds, fonts
├── assets/
│ ├── images/
│ ├── sounds/
│ └── fonts/
├── requirements.txt # pygame-ce==2.5.8 (pin what you tested)
├── README.md
└── .gitignore # .venv/, __pycache__/, build/, dist/Keep main.py short. Its only job is to start Pygame and hand control to the first scene. That also makes it easy to package with PyInstaller or pygbag, which both want a single entry file.
Settings in one place#
# settings.py
WIDTH, HEIGHT = 960, 540
FPS = 60
PLAYER_SPEED = 300
KEYS_LEFT = ("left", "a")Import what you need: from settings import WIDTH, HEIGHT. Tweaking feel (speeds, gravity, timings) becomes a one-file change. Player-changeable settings such as volume belong in a save file instead (saving game data).
Load assets once, from one module#
# game/assets.py
from pathlib import Path
import pygame
ROOT = Path(__file__).resolve().parent.parent / "assets"
_images = {}
def image(name):
if name not in _images:
_images[name] = pygame.image.load(ROOT / "images" / name).convert_alpha()
return _images[name]Building paths from the file's location means the game works no matter which folder it's started from, a common source of FileNotFoundError. Caching means each image is loaded from disk once.
Scenes instead of a giant loop#
Menus, levels and the game-over screen are easiest to manage as scene classes with the same three methods, handle(event), update(dt) and draw(surface), as shown in menus and game states. The main loop just forwards to the current scene.
Avoid circular imports#
If player.py imports enemies.py and enemies.py imports player.py, Python raises ImportError or gives you half-initialised modules. Pass objects as arguments (the scene gives the enemy a reference to the player) instead of importing game objects into each other.
Version control from day one#
Initialise a Git repository and commit whenever something works. Ignore generated folders (.venv/, __pycache__/, build/, dist/). Experiments become safe: if a change breaks the game, go back.
requirements.txt#
pygame-ce==2.5.8Pin the version you tested. Anyone, including future you, can recreate the environment with python -m pip install -r requirements.txt.