Menu

Pygame Sprite and Group Classes Explained

pygame.sprite.Sprite bundles an image, a Rect and an update() method into one game object, and pygame.sprite.Group updates and draws many of them in one line. Learn both, kill(), and how to structure a game with sprites.

This page includes runnable editors. Edit, run, and see output instantly.

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:

  1. Call super().__init__() first in __init__.
  2. Set self.image to a Surface (a loaded image, or one you drew).
  3. Set self.rect to its position, usually self.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

MethodDoes
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 groupCounts 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

ClassFor
pygame.sprite.GroupMost uses
pygame.sprite.GroupSingleHolds at most one sprite, such as the player
pygame.sprite.LayeredUpdatesDraws 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.

Coddy programming languages illustration

Learn to code with Coddy

Get started