📝 Worksheet← Lesson
BitWithBite
Pygame Mastery · Quick Reference

Sprites Cheat Sheet

Pygame Mastery
In one line: Pygame's Sprite class and Group system give every game object a standard image + rect + update/draw shape, so hundreds of objects can be managed with just a few lines of code.

Key Ideas

1The Sprite Class. Every pygame.sprite.Sprite subclass needs exactly two attributes: self.image (a Surface) and self.rect (a Rect for position and collisions).
2Sprite Groups. A Group holds many sprites. group.update() calls every sprite's update(); group.draw(screen) blits every sprite's image at its rect in one line.
3Sprite Layers. pygame.sprite.LayeredUpdates lets each sprite carry a _layer value so draw order is explicit (background behind player behind HUD) instead of insertion order.
4Sprite Sheets. Slice one packed image into individual frames with Surface.subsurface(rect) once at load time, then store the frames in a list for reuse.
5Animated Sprites & States. A state string (e.g. "idle", "walk") indexes into a dict of frame lists, so switching states automatically switches which frames play.
6super().__init__(). Always call this first in a Sprite subclass's __init__ so Pygame's internal bookkeeping wires the object up correctly for Groups.

Core API

class X(pygame.sprite.Sprite): self.image, self.rect
group.add(sprite) / group.update() / group.draw(screen)
pygame.sprite.LayeredUpdates().add(sprite, layer=n)
sheet.subsurface(pygame.Rect(x,y,w,h)).copy()
sprite.kill() # removes from every group it's in

Common Mistakes

Forgetting super().__init__() in a Sprite subclass
Always call it first — Groups silently fail to track the sprite otherwise
Slicing a sprite sheet inside update() every frame
Slice once at load time and store pre-cut Surfaces in a list
Using a plain Group when draw order matters
Use LayeredUpdates with explicit layer= values instead
sprite_sheet.png01234567
One subsurface() call per frame slices a packed sheet — frame 2 is highlighted as the sprite's current frame.