Module 19 of 20 Phase: Architecture & Shipping

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.

⚡
10 Lessons
This module
⏱
~2.5 hrs
Estimated time
📊
Advanced
Difficulty
🐍
PyInstaller
Packaging tool
Section 1

FPS Optimization

Most Pygame slowdowns come from wasteful drawing, not "Python is slow." Fix the biggest offenders first:

ProblemFix
Redrawing the entire screen every frame when only part changedUse pygame.display.update(rect_list) to redraw only dirty rects
Un-converted images blitted every frameCall .convert()/.convert_alpha() once at load time (Module 4)
Iterating every sprite manually with plain listsUse 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
Section 2

Memory Management & Asset Optimization

🖼️
Compress Images
Resize sprite sheets to the exact dimensions used in-game — a 4K background scaled down every frame wastes both memory and CPU.
🔊
Audio Formats
OGG files are typically much smaller than uncompressed WAV for music, at negligible quality cost for game audio.
🗑️
Release Unused References
Remove dead sprites from Groups (Module 6) and clear particle lists (Module 16) — orphaned references prevent garbage collection.
📦
Lazy-Load Rarely Used Assets
A boss-only sprite sheet doesn't need to load before the main menu — load level-specific assets when that level starts, not at global startup.
Section 3

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.

profile_game.py
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
💡
Key insight: A simple on-screen FPS counter (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.
Section 4

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.

terminal
pip install pyinstaller

pyinstaller --onefile --windowed --add-data "assets;assets" main.py
FlagWhat it does
--onefileBundles everything into a single .exe/binary instead of a folder of files
--windowedSuppresses 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)
Section 5 · Publishing

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.

Section 6

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.

70 35 0 FPS Optimization pass 22 FPS Before 60 FPS After
Typical FPS improvement after applying convert_alpha() and sprite group batching

🧠 Quick Knowledge Check

1. What's usually the biggest cause of Pygame slowdown?
2. Why use pygame.sprite.Group instead of a plain Python list of sprites?
3. What does the --windowed flag do in a PyInstaller command?
4. What does Python's cProfile module help you do?
5. What's a common first platform to publish an indie Pygame project?
Finished Module 19?

Mark it complete to track your progress through Complete Pygame Mastery 2026.

🗒 Cheat Sheet 📝 Worksheet