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
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:
from game import Stage, Ship
screen = Stage.create()
Stage.clear(screen)
ship = Ship(240, 520)
ship.draw(screen)
That's a sprite: an object that knows its spot and how to look. One line made it show up.
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:
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)
Two sprites, two spots. The ship and the enemy don't share anything — each takes care of itself.
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:
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)
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.
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.