Pygame performance tips
Most slow Pygame games suffer from the same handful of problems. Measure first, then apply these fixes in order of impact.
On this page
- 1. Measure before you change anything
- 2. Convert every loaded image
- 3. Pre-render what doesn't change
- 4. Cache text and transforms
- 5. Don't load files in the loop
- 6. Skip what's off-screen
- 7. Reduce collision checks
- 8. Batch blits
- 9. Draw at a lower resolution
- 10. Mind Python-level loops
- What not to worry about
1. Measure before you change anything#
Show the frame rate while you play: pygame.display.set_caption(f"{clock.get_fps():.0f} FPS"). Then find where the time goes with Python's built-in profiler:
python -m cProfile -s cumtime main.pyPlay for a while, close the game, and read the top of the list. The functions with the largest cumulative time are where optimisation pays off. Guessing usually targets the wrong thing.
2. Convert every loaded image#
pygame.image.load("x.png").convert_alpha(), or .convert() for images without transparency. Unconverted images are converted pixel-by-pixel on every blit, which is often the single biggest slowdown in beginner games.
3. Pre-render what doesn't change#
Backgrounds, tile maps, UI frames and static text can be drawn once onto a Surface and blitted every frame as one image. The tile maps tutorial shows a level of hundreds of tiles drawn with a single blit.
4. Cache text and transforms#
font.render(), pygame.transform.rotate() and scale() create new Surfaces each call. Render a score only when it changes. Prepare flipped and scaled sprites at load time. For objects that rotate freely, pre-compute 36 or 72 rotations and pick the nearest.
5. Don't load files in the loop#
Loading images, sounds or fonts reads the disk. Do it once at start-up (or when a level loads) and keep the objects.
6. Skip what's off-screen#
Before drawing, test camera_rect.colliderect(obj.rect) and skip objects that aren't visible. With large worlds, also skip updating far-away objects, or update them less often.
7. Reduce collision checks#
Checking every object against every other grows quickly: 200 objects means nearly 20,000 pairs. Use groups so you only test relevant pairs (bullets against enemies, not bullets against bullets), and for large numbers divide the world into a grid and test only objects in nearby cells.
8. Batch blits#
surface.blits([(image, pos), ...]) blits a list in one call, avoiding Python overhead per sprite. pygame-ce's fblits() is faster still when you don't need the returned rectangles. Group.draw() already batches internally.
9. Draw at a lower resolution#
Pixel-art games can render to a small Surface (say 320 × 180) and scale up, either with pygame.SCALED or one pygame.transform.scale per frame. Fewer pixels to fill means faster drawing. See pixel art in Pygame.
10. Mind Python-level loops#
Per-pixel work in Python (get_at/set_at across a whole screen) is slow. Use pygame.PixelArray, pygame.surfarray with NumPy, or pygame.mask for bulk operations.
What not to worry about#
A few hundred pygame.draw calls or blits per frame are fine on modern computers. Micro-optimisations like caching method lookups rarely matter compared with the items above. Keep your code readable until profiling shows a real problem.