Optimize It, Package It
Ship Your Game to the World
A finished game still needs to run smoothly and reach players. This module covers FPS optimization, memory management, profiling, packaging with PyInstaller, and where to publish your finished build.
FPS Optimization
Most Pygame slowdowns come from wasteful drawing, not "Python is slow." Fix the biggest offenders first:
| Problem | Fix |
|---|---|
| Redrawing the entire screen every frame when only part changed | Use pygame.display.update(rect_list) to redraw only dirty rects |
| Un-converted images blitted every frame | Call .convert()/.convert_alpha() once at load time (Module 4) |
| Iterating every sprite manually with plain lists | Use pygame.sprite.Group — its update()/draw() are optimized in C |
| Recomputing the same value every frame (fonts, rotated images) | Cache the result once, reuse it |
Memory Management & Asset Optimization
Profiling: Finding the Real Bottleneck
Never guess where slowdown comes from — measure it. Python's built-in cProfile module shows exactly which functions consume the most time.
import cProfile
import pstats
def run_game():
# ... your normal main() call
pass
cProfile.run("run_game()", "profile_output")
stats = pstats.Stats("profile_output")
stats.sort_stats("cumulative").print_stats(15) # top 15 slowest functions
clock.get_fps(), rendered via Module 11's font code) is often enough for day-to-day development — reach for cProfile only when you need to pinpoint a specific bottleneck.Creating Executables with PyInstaller
Players without Python installed can't run a .py file. PyInstaller bundles your game and the Python interpreter itself into a single standalone executable.
pip install pyinstaller
pyinstaller --onefile --windowed --add-data "assets;assets" main.py
| Flag | What it does |
|---|---|
--onefile | Bundles everything into a single .exe/binary instead of a folder of files |
--windowed | Suppresses the console window (no black terminal box appears behind your game) |
--add-data "assets;assets" | Bundles your assets folder into the executable (use a colon : instead of ; on macOS/Linux) |
Publishing Your Game
Once packaged, itch.io is the most common first home for an indie Pygame project — free hosting, a simple upload flow, and a built-in audience of players actively looking for small games. Include a README describing controls, a few screenshots or a short gameplay GIF, and credit any third-party assets you used.
The FPS Impact of These Fixes
Applying just two of the fixes from Section 1 — calling convert_alpha() once at load time and batching sprites through a pygame.sprite.Group — typically produces a dramatic, measurable frame-rate jump.