LEGO Education Computer Science & AI
Build a live action video game, then use if-then conditionals, a timer, and a win sequence to make the game your own.
An if-then statement just means: if something happens, then something else is going to happen. In computer science, these are called conditionals. For example: if the player touches this coin, then the score goes up.
You use conditionals in your daily life too. If you’re hungry, then you eat. If it’s raining outside, then you probably grab an umbrella.
Video games run on conditionals. If a character jumps up to hit a block, then they get coins, points, or a mushroom. If a character touches a heart symbol, then they get an extra life. If a character makes a correct move, then they get a point.
How would you design your own video game? Think about the games you like to play:
What features do you enjoy? Character design, boss fights, difficult games, sports games, or making a high score in an old school video game?
Today you’re making a live action video game. That means the game doesn’t take place on a screen. It takes place in real life, right in front of you. Look at it once it’s built: how do you think it’s going to work, and what does success look like in this game?
You are building a track with a starting plate at one end and a flag at the other, a player cart on the single motor that rides the track, a color sensor that watches the track, and a controller: a big wheel on the double motor.
Your flag is on your right, the color sensor is looking at you, and the motor is on the outside of the track. When you slide the cart along the track, it passes right in front of the color sensor.

If the screen says Preparing and your color sensor flashes rainbow lights, it’s updating. You didn’t do anything wrong. Don’t turn it off; it only takes a moment.
If you don’t connect all three devices, or a battery is dead on one, this code won’t work correctly. That’s the first thing to check.
The double motor’s wheel is your controller. Turning it should make something happen. Look at the code and predict: what do you think it does?
| Block | What it does |
|---|---|
| when left motor turned ↻? | An event. It runs when you turn the controller wheel clockwise. |
| run ↺ for 3 rotations | The single motor drives the player cart along the track. |
| if ⬢ then | A conditional with an empty space. |
| play sound win until done | Inside the if: plays the win sound. |
Off to the side is a block that isn’t attached yet: detecting red?
If you turn the wheel too many times, the cart goes past the end of the track and there’s no way to get back. Slide it back onto the track. If your character falls off, put it back on.
Make the controller drive your character either way, depending on which way you turn the wheel.
| Block | What it does |
|---|---|
| when left motor turned ↻? | Turn the wheel clockwise… |
| run ↺ for 1 rotations | …and the cart moves one way. |
| if detecting red? then play sound win | Checks for red. |
| when left motor turned ↺? | Turn the wheel counterclockwise… |
| run ↻ for 1 rotations | …and the cart moves the other way. |
| if detecting red? then play sound win | Checks for red. |
Now the code checks for red in two places. You can fix that with its own event.
On a smooth table, the track can turn as your character drives by. Attach one of those extra rubber pieces under the gray part of the track for more grip.
You can have more than one “when ▶ clicked” running at the same time.
Make sure your character is standing up. If the sensor still misses it, it may be too far back. Mr. Spratt moved his color sensor forward one hole, closer to the track.
Right now you win automatically just by driving in front of the color sensor. Make the player do more. A wait until block is another type of condition that must be met before anything happens in your code.
The controller drives your character both ways. Red passing the sensor plays the dog, then blue passing the sensor plays win and ends the game. If you’re too slow, the alarm plays, then the lose sound, and the game stops. Too hard? Give yourself more time on the timer.
This is a simple game. You have access to all the standard blocks now, so there are lots of ways to make it even better. Make this game your own.
Come up with a theme for your game, like an ocean or a forest, and add things onto your build so your player gets more into the idea of the game. You could build obstacles too.
Keep the track clear so the cart can pass right in front of the color sensor, and keep your characters standing up so the sensor can see them.
Program some new rules into your game:
Every new rule is a conditional. Write it as “if ___, then ___” first, then find the blocks that match.
Someone else can play your game, follow your rules, and either win or lose, and you can explain every rule as an if-then.
Share it: Tell Mr. Spratt all about how you changed the game. What features did you put in? What’s one rule you added?
You built with all four characters. Add more wait until blocks so the player has to get three or even four characters past the color sensor, in order, before the timer runs out. Give each character its own sound.
Then hand the controller to someone else. Can they figure out your sequence?
A conditional is an if-then statement: if something happens, then something else happens. Video games are full of them, like “if the player touches a coin, then the score goes up.”
Your child built a live action video game from building instructions starting on page 80: a track with a player cart, a color sensor, and a wheel controller. At code.legoeducation.com (PIN 1311), they completed an if-then so the game plays a sound when it sees red, made the controller drive both ways, and used the standard blocks to add a forever loop, a timer, and a win sequence where the player must bring a red, then a blue character past the sensor before time runs out.
There’s no single right answer for the theme or new rules. Here is the program Mr. Spratt built before turning the game over to students:
| Block | What it does |
|---|---|
| when left motor turned ↻?: run ↺ for 1 rotations | Controller moves the cart one way. |
| when left motor turned ↺?: run ↻ for 1 rotations | Controller moves the cart the other way. |
| when ▶ clicked | Starts the win sequence. |
| wait until detecting red? | Waits for the red character. |
| play sound dog until done | Red made it. |
| wait until detecting blue? | Waits for the blue character. |
| play sound win until done | Blue made it. You win! |
| stop all | Game over. |
| when ▶ clicked | Starts the timer at the same time. |
| wait 15 seconds, play sound alarm until done | Halfway warning. |
| wait 10 seconds, play sound lose until done | Time’s up. |
| stop all | Game over. |
