Learning LibraryGame Development LibraryKids

Sprites: the characters in your game

The moving things in a game — your ship, the enemies — are called sprites. Each one is an object that remembers where it is and knows how to draw itself.

The big idea

A sprite is a game object that holds its own position and draws itself when you ask.

See it in code

1Start simple

The simplest sprite is one on its own. We make a Ship at an (x, y) spot, and it draws itself — you never draw the triangle by hand:

python
from game import Stage, Ship

screen = Stage.create()
Stage.clear(screen)

ship = Ship(240, 520)
ship.draw(screen)
Run it — one sprite, drawing itself:
A single blue player ship drawing itself near the bottom center of a dark play area.

That's a sprite: an object that knows its spot and how to look. One line made it show up.

2A step further

Now add a second sprite. We put an Enemy up top and keep the Ship below. Each has its own spot, and each draws itself:

python
from game import Stage, Ship, Enemy

screen = Stage.create()
Stage.clear(screen)

ship = Ship(240, 520)
enemy = Enemy(240, 120)

ship.draw(screen)
enemy.draw(screen)
Run it — two sprites, each in its own place:
A purple enemy sprite near the top center and a blue player ship near the bottom center of a dark play area.

Two sprites, two spots. The ship and the enemy don't share anything — each takes care of itself.

3In our world

One more makes a crew. We keep the Ship near the bottom and add two Enemy sprites up top. Each has its own (x, y) spot, and each draws itself:

python
from game import Stage, Ship, Enemy

screen = Stage.create()
Stage.clear(screen)

ship = Ship(240, 520)
enemy1 = Enemy(160, 120)
enemy2 = Enemy(320, 120)

ship.draw(screen)
enemy1.draw(screen)
enemy2.draw(screen)
Run it — three sprites, each drawing itself:
A blue player ship near the bottom of a dark play area and two purple enemy sprites near the top.

Each sprite knows its own look and its own spot. ship is separate from enemy1 and enemy2 — moving one never touches the others. That's what makes them easy to work with.

The same idea, everywhere

Sprites are how every game keeps track of its cast. A coin, a cloud, a boss, a bullet — each is a sprite with its own position and picture. Make a bunch of them, and you've got a game world.

Try it yourself

Add a third enemy at (240, 60). Then move the ship by changing its start to Ship(100, 520) and run again — only the ship moves.

The common mistake

Forgetting to draw a sprite. Creating Enemy(160, 120) makes the enemy exist, but you won't see it until you call enemy1.draw(screen). Every sprite needs to be drawn to show up.

What it unlocks

Sprites are objects — see classes and objects and methods and attributes — and they move with frames and motion.