10. pygame Basics
Everything so far has been text in the console. pygame lets you open a window and draw shapes that move. This is how you’ll actually see your evolution sim.
First, get pygame ready
pygame is an extra toolbox that doesn’t come with Python. You install it once by typing this into a terminal (ask your grown-up to help the first time):
Then, at the top of your file, bring it in:
How screen positions work
The window is a grid of tiny dots called pixels. You describe a position with two numbers: x (how far across) and y (how far down).
⚠️ One surprise: y counts downwards! The very top of the window is y = 0, and y gets bigger as you go down. So a bigger y means lower on the screen.
Colours
A colour is three numbers: how much Red, Green, and Blue it has, each from 0 to 255. This is called RGB.
Notice these are written in CAPITALS because they’re constants — they don’t change.
The skeleton every pygame program needs
Almost every pygame program has the same shape. Here it is, fully explained. This opens a window that just sits there until you close it.
What is the “game loop”?
That big while is_running: loop is the heart of every game. It runs about 60 times every second. Each time round is called a frame. In every frame you:
- Check input — did the player press a key or close the window?
- Update — move things, change energy, etc.
- Draw — paint the new picture.
Do that 60 times a second and shapes appear to move smoothly. It’s like a flip-book!
Remember while loops from 4. Loops? This is exactly that. The loop keeps going while is_running is True. When the player clicks the X, we set is_running = False, and the loop stops.
Drawing shapes
You draw inside the game loop, after screen.fill(...) and before pygame.display.flip().
A circle (great for a creature!)
This draws a green circle, 20 pixels wide, in the middle of an 800×600 window.
A rectangle (great for food, or the world)
This draws a small orange square near the top-left.
Making something move
To move a shape, keep its position in variables and change them a little every frame. Because you redraw 60 times a second, it looks like smooth movement.
Here’s a complete, runnable program: a green creature-circle that drifts across the screen and bounces off the edges.
Run it and watch your creature bounce around the window! 🎉
Try changing speed_x and speed_y to make it faster, slower, or move in a different direction.
Connecting this to your Creature class
Here’s the exciting part. Remember the Creature class from 8. Classes & Objects? You can give each creature its own x, y, and colour, keep them all in a list, and draw the whole population every frame with a for loop:
And there it is — a window full of little creatures, each one an object, all wandering around. From here you can add food, energy loss, eating, and babies, and you’ve got the beginnings of a real evolution sim. 🌍
✅ Quick recap
- Install once with
pip install pygame, thenimport pygame. - Positions are
(x, y);ygets bigger going down. - Colours are
(red, green, blue), each 0–255. - Every pygame program has a game loop (a
whileloop) that runs ~60 times a second. - Each frame: check events → update →
screen.fill()→ draw →pygame.display.flip()→clock.tick(60). - Move things by changing their position variables a little each frame.
- Give your
Creatureclass adraw(self, screen)method and loop over a list to draw them all.