Once a game has more than a few things moving, each one needs its picture, its position and its behavior kept together. pygame.sprite.Sprite is a class for exactly that, and pygame.sprite.Group manages many of them at once.
A sprite is a class that inherits Sprite and sets two attributes, image and rect:
Coins appear at the top, fall and remove themselves at the bottom. The loop never deals with one coin at a time: coins.update() moves all of them and coins.draw(screen) draws all of them.
The Sprite class
Three rules:
- Call
super().__init__()first in__init__. - Set
self.imageto a Surface (a loaded image, or one you drew). - Set
self.rectto its position, usuallyself.image.get_rect(...).
Put the sprite's behavior in update(). Group.update() passes along any arguments you give it, so group.update(dt) hands every sprite the delta time.
Group methods
| Method | Does |
|---|---|
group.add(*sprites) | Adds sprites (also Group(*sprites) when you make it) |
group.update(*args) | Calls update(*args) on every sprite |
group.draw(surface) | Blits each sprite's image at its rect |
group.remove(*sprites) | Takes sprites out of this group |
group.empty() | Removes all sprites |
len(group), for s in group | Counts and loops over the sprites |
sprite.kill() | Removes the sprite from every group it is in |
sprite.alive() | True if it is in at least one group |
A sprite can be in several groups at once. A common layout is one group with everything (for drawing) plus groups by role (enemies, bullets, coins) for collisions.
A player sprite
The player is a sprite too. Its update() reads the keyboard:
Press Play, click the game and walk with the arrow keys. The player faces the way it walks; the slimes patrol back and forth. Adding a third slime is one more Enemy(...). Sprite collisions makes them dangerous.
Other group types
| Class | For |
|---|---|
pygame.sprite.Group | Most uses |
pygame.sprite.GroupSingle | Holds at most one sprite, such as the player |
pygame.sprite.LayeredUpdates | Draws by layer, so you control what is in front |
Load an image once and share it when you make many sprites of the same kind; loading the file in every __init__, as the coin example does, is fine for a few sprites but slows a game that spawns hundreds.
Frequently Asked Questions
What is a sprite in pygame?
A game object made from a class that inherits pygame.sprite.Sprite. It has an image (what it looks like) and a rect (where it is), and usually an update() method with its behavior.
What does a sprite Group do?
It holds many sprites. group.update() calls update() on each, group.draw(screen) blits each one's image at its rect, and pygame.sprite.spritecollide() checks a sprite against all of them.
How do I remove a sprite?
Call sprite.kill(). It removes the sprite from every group it belongs to, so it stops being updated and drawn.
Why do I get AttributeError: object has no attribute image?
Group.draw() needs self.image and self.rect on every sprite. Set both in __init__, and call super().__init__() first.