๐ The Game Loop

Press a key โ your hero moves. Let go โ it stops. Jump โ it comes back down. It feels alive. ๐ค But the computer is just a machine that does what it's toldโฆ so how does it know what to do?
The answer is one of the biggest secrets behind every game ever made. Let's uncover it.
The big idea: a game is a loop
A video game is really just a list of rules the computer follows super fast โ about 60 times every second. Each time around, it does the same three things:
- ๐ Listen โ which keys are you pressing?
- ๐ Move โ move everyone by the rules (gravity, speedโฆ).
- ๐จ Draw โ paint the new picture on the screen.
Then it loops back to the start and does it all over again. That cycle is called the game loop. Watch it spin:
LISTEN: Which keys are pressed right now?
Donkey Kong, Minecraft, every game you've ever played runs a loop like this. ๐ฎ
Your turn: become the rule-maker
Here's a real game loop, running live. The little red hero is you. The barrel rolls like in Donkey Kong.
On the right are the game's rulesโ gravity, jump power, speed. They're just numbers, and youget to change them. That's the whole secret: code is rules you can change.
Click the game, then use โ โ to move and โ / Space to jump. Try Slo-mo or Step once to watch the loop tick.
๐๏ธ The Rules
These are the game's rules. Drag them and watch what happens!
How hard the ground pulls you down
How hard you push off the ground
How fast you run left and right
โธ๐ง Peek inside: those sliders are โvariablesโ
Gravity, jump, and speed are variables โ think of each one as a little named boxthat holds a number. The game doesn't know โ0.8โ; it knows โlook in the box called gravity.โ When you drag a slider, you're just putting a new number in the box, and the loop reads it on the very next tick. Almost everything a game remembers โ your score, your health, where you're standing โ lives in a named box like this.
Discovery missions
Real scientists learn by experimenting. Try each mission in the lab above, tick it off, then peek at the secret to find out why it happened.
๐Drag Gravity down to 0, then jump. Where do you come back down?
๐ชจNow crank Gravity all the way UP and try to jump.
๐ฆMake Gravity tiny and Jump Power huge โ then sail over the barrel.
๐Jump and watch the vy number in the readout as you go up and come down.
๐ฃPress Pause, then tap โStep onceโ a bunch of times. Watch loop #.
๐ You leveled up! You now know:
- ๐ A game is a loop: Listen โ Move โ Draw, over and over, ~60 times a second.
- ๐๏ธ Code is really just rules โ and the rules are numbers you can change.
- ๐ Gravity gets added to your speed every single tick (that's why falling gets faster).
- โค๏ธ The โloop #โ counter is the game's heartbeat โ proof the loop is running right now.
๐ฌ Behind the scenes โ what's next?
Did you notice the x and y numbers in the lab? Those tell the game whereyour hero is. But what ARE they, really? And how does Minecraft keep track of where millions of blocks sit? That's our next adventure. ๐