- Learnt28 min · driving, a sensor, pads, exact colours
- Basic24 min · paint the floor, build the pads
- Challenge 1–327 min · the finish, slips, autopilot
- Extrabonus · a boost that wears off
Review — Treasure Hunt, finished 5 min
(distance to [mouse-pointer v])is a number. Small means close.<<> and <>>is true only when both sides are true.<<> or <>>is true when either side is true.<not <>>flips true and false.
Quick-fire
When is this true?
<<(distance to [mouse-pointer v]) < (50)> and <mouse down?>>
Reveal the answer
When the pointer is close and the mouse button is down. Close but not clicking is false. Clicking far away is false too.
Today’s Topic 3 min
- A robot you drive: turn, then move forward
- A sensor:
<touching color [#23262D]?> - A sensor is a question asked every frame, inside a forever loop
- Pads on the floor: green is faster, red is back to the start
Learning outcome
By the end of this lesson you will be able to:
- Use touching colour to find out what is under a sprite.
- Pick an exact colour with the eyedropper.
- Make the floor change what the robot does.
Learnt — a floor that talks 28 min
1 · Meet the robot and the course
We look at this robot from above. Its nose points right.
- The floor is pale.
- A thick dark line runs from the start to the finish.
- Green pads sit on the line. Red holes sit beside the corners.
- The chequered square at the end is the finish.
Real robots follow lines like this. Robots in warehouses and factories do it all day.
2 · Driving
You built this in the Car Race. The robot turns, then moves the way its nose points.
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (3) steps
end
end
3 · A sensor is a question
A sensor tells a robot something about the world. Our first sensor is aSensing block:
<touching color [#23262D]?>
A question asked once is not much use. The robot moves, so we keep asking.
- From Control, put an
if / elseblock inside the forever loop. - Drop
<touching color [#23262D]?>into its hexagon. - In the top half, put
say [On the line!]. - In the bottom half, put
say [Off the line]. - Drive. The bubble changes the moment you leave the line.
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (3) steps
end
if <touching color [#23262D]?> then
say [On the line!]
else
say [Off the line]
end
end
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot, and ← or → to turn it.
4 · Pads that give orders
Now the floor gives orders. We add a second script to the same robot. It asks two more questions.
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (speed) steps
end
end
when flag clicked
forever
if <touching color [#22C55E]?> then
set [speed v] to (6)
end
if <touching color [#EF4444]?> then
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
end
end
Two scripts run at the same time. One drives. One watches the floor. Neither waits for the other.
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot, and ← or → to turn it.
5 · The exact colour
Touching colour is fussy. Light green is not green. So we never type a colour. We pick it.
- Click the colour swatch inside the block.
- Click the eyedropper at the bottom of the colour menu.
- Click the pad on the Stage. The swatch turns that exact colour.
Basic — paint the floor, build the pads 24 min
Step 1 — the floor
- Click the Stage, then the Backdrops tab.
- Fill the backdrop with a pale colour.
- Pick the Line tool. Make the outline very thick, about 14.
- Draw a dark line from the bottom left to the top right, with a few bends.
- Paint a chequered square at the end of the line.
Step 2 — the pads
- With the Rectangle tool, paint two green squares on the line.
- Paint two red squares next to the line, on the inside of two bends.
Step 3 — the robot
- Paint a new sprite: a rounded rectangle with two wheels.
- Draw it with its nose pointing right.
- Make it small enough to sit on the line.
Step 4 — drive it
Build the driving script. Move it to the start of your line. Try all three keys.
Step 5 — the pads script
- Make a variable called
speed, for all sprites. - Change the move block to
move (speed) steps. - Build the pads script. Pick each colour with the eyedropper.
- Play. Green should speed you up. Red should send you home.
when flag clicked
forever
if <touching color [#22C55E]?> then
set [speed v] to (6)
end
if <touching color [#EF4444]?> then
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
end
end
The robot never speeds up on green.
The green in the block is not the green on the floor. Pick it again with the eyedropper, on the middle of the pad.
The robot goes back to the start at once, every time.
The robot starts touching red, or your start point is inside a red square. Move the start, or move the red square.
Speed is always 3, even after green.
Look for a second set [speed v] to (3) inside a forever loop. It puts speed back every frame.
The finish
Make the chequered square end the game.
- When the robot reaches the finish, it says “Finished!”.
- After that, the arrow keys do nothing.
- The flag starts a new game.
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot, and ← or → to turn it.
Teacher reveal
if <touching color [#FFFFFF]?> then
say [Finished!]
stop [other scripts in sprite v]
stop [this script v]
end
Inside the pads script’s forever loop. The finish is white and grey squares, and the question only needs the white. stop [all v] works too, but it clears the bubble, which is why the demo stops the driving script instead.
Count the slips
A good driver stays on the line. Count how often you leave it.
- A variable called
slipsstarts at 0. - Leaving the line adds exactly one.
- Staying off the line for a long time still counts as one slip.
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot, and ← or → to turn it.
Teacher reveal — two waits, not one if
when flag clicked
set [slips v] to (0)
forever
wait until <not <touching color [#23262D]?>>
change [slips v] by (1)
wait until <touching color [#23262D]?>
end
Most pupils first write if <not <touching color [#23262D]?>> then inside a forever. It counts thirty slips a second. The second wait until <> is the fix: the robot must get back on the line before the next slip can count.
Autopilot, with one sensor
Press space and the robot drives itself.
- It moves forward on its own while it is touching the line.
- It stops the moment no part of it touches the line.
- Then it says “Where did the line go?”.
How to use this: Click the green flag, then click the stage once and press the space bar. The robot drives itself. You can still drive it with ↑, ← and →.
Teacher reveal — and the question to ask
when [space v] key pressed
repeat until <not <touching color [#23262D]?>>
move (2) steps
end
say [Where did the line go?] for (2) seconds
Then ask: why did it not turn? Because “am I touching the line?” says nothing about which side the line went. To steer, a robot needs to know left from right. That is next lesson: two sensors.
A boost that wears off
Right now green makes you fast for the rest of the game. Make it last one second.
- A green pad sets speed to 6.
- About one second later, speed goes back to 3 by itself.
- Touching green again starts the second again.
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot over the green pad, and ← or → to turn it.
Teacher reveal
when flag clicked
set [boost v] to (0)
forever
if <touching color [#22C55E]?> then
set [boost v] to (30)
end
if <(boost) > (0)> then
set [speed v] to (6)
change [boost v] by (-1)
else
set [speed v] to (3)
end
end
This replaces the green half of the pads script. The driving script is unchanged — it still reads (speed), and does not care who sets it.
Summary 5 min
<touching color [#23262D]?>asks: is any part of me on this colour?- A sensor inside a forever loop is asked every frame, about 30 times a second.
- Two scripts can run at once: one drives, one watches the floor.
- Always pick a colour with the eyedropper. Paint pads in one flat colour.
- One sensor knows on or off. It does not know left from right.
- Sensor
- Something that tells a robot about the world around it.
- Frame
- One pass through the forever loop. About 30 every second.
- Eyedropper
- The tool that copies an exact colour from the Stage.
- Pad
- A patch of colour on the floor that gives the robot an order.
Hand in
Save your course and your robot. Next lesson the robot gets two sensor dots, and it learns to steer itself.