- Learnt28 min · remembering choices, setting up the race, countdown, winning
- Basic24 min · join the menu to the game
- Challenge 1–327 min · remember, obey, and declare a winner
- Extrabonus · Setting and How to Play
Review — where we got to 5 min
- Off the track is more friction — 0.8 instead of 0.9.
ask () and waitfills one place calledanswer.- A page’s buttons are costumes on one sprite, cloned by a My Block.
- A broadcast moves you from one page to the next.
Quick-fire
You click 2 Players and the game moves to the next page. How does the race find out there are two players?
Reveal the answer
It does not. A broadcast carries no information — it just says “go here”. Fixing that is the first job today.
Today’s Topic 3 min
- Remembering what the player chose
- Sensible defaults
- A race that sets itself up from the choices
- A 3-2-1 countdown
- Two ways to win, judged in one place
Learning outcome
By the end of this lesson you will be able to:
- Carry a player’s choice across pages with a variable.
- Show or hide a sprite depending on what was chosen.
- Write two different win conditions that end the same way.
Learnt — joining it up 28 min
A broadcast carries nothing
This is the idea of the lesson, and it is easy to walk past. When you broadcast [Game Mode v], the message says where to go. It does not say who sent it or why.
So a button that only broadcasts has thrown your choice away. It has to remember first:
if <(costume [name v]) = [2 Players]> then
set [players v] to (2)
broadcast [Game Mode v]
end
The five things to remember
- players — 1 or 2
- game mode — Time Race or Lap Race
- track — Track 1 or Track 2
- laps to win — how many
- race time — how long
Defaults, before anything else
What if the player never visits a page? Set every one of them to something sensible when the flag is clicked:
when flag clicked
set [players v] to (1)
set [game mode v] to [Lap Race]
set [track v] to [Track 1]
set [laps to win v] to (3)
set [race time v] to (60)
The race reads, it does not decide
When Game Play arrives, nothing has to be worked out. Every sprite just looks at the variables.
The Stage picks the backdrop:
when I receive [Game Play v]
if <(track) = [Track 1]> then
switch backdrop to [Track 1 v]
else
switch backdrop to [Track 2 v]
end
The blue car only turns up if it was asked for:
when I receive [Game Play v]
hide
if <(players) = (2)> then
go to x: (30) y: (70)
point in direction (90)
show
end
Three… two… one…
A new sprite with four costumes: 3, 2, 1 and GO!.
Each one does exactly the same thing — appear small, grow, hold. Four times. So make a block:
define Pop
set size to (40) %
show
repeat (12)
change size by (8)
end
wait (0.4) seconds
when I receive [Game Play v]
switch costume to [3 v]
Pop :: custom
switch costume to [2 v]
Pop :: custom
switch costume to [1 v]
Pop :: custom
switch costume to [GO! v]
Pop :: custom
hide
broadcast [Game Start v]
Game Start, not the flag — so nobody can jump the lights.Two ways to win
Your player chose a game mode. Now honour it:
- Lap Race — wait until somebody reaches the lap target.
- Time Race — count the clock down, then see who did the most laps.
when I receive [Game Start v]
if <(game mode) = [Lap Race]> then
wait until <<(P1 lap) = (laps to win)> or <(P2 lap) = (laps to win)>>
else
repeat until <(race time) = (0)>
wait (1) seconds
change [race time v] by (-1)
end
end
Then one comparison decides it, whichever mode was played:
if <(P1 lap) > (P2 lap)> then
set [winner v] to [Red]
else
if <(P2 lap) > (P1 lap)> then
set [winner v] to [Blue]
else
set [winner v] to [Nobody]
end
end
broadcast [Play Again v]
Basic — join the menu to the game 24 min
Step 1 — the five variables and their defaults
Make them, and add the defaults stack to any sprite. It runs once, at the flag, before the menu.
Step 2 — every button remembers
Go through each page and add a set above each broadcast.
Step 3 — the Stage picks the track
Both track backdrops in one project, and one if / else on (track).
Step 4 — the cars line up
On when I receive [Game Play v]: hide, then go to the right start position for the chosen track, reset speed, steering and laps, then show. Blue does all of it inside if <(players) = (2)> then.
Step 5 — the countdown
New sprite, four costumes, the Pop block, and broadcast [Game Start v] at the end. Change the cars’ driving loop to start on Game Start.
Step 6 — the judge
The two win conditions, the winner comparison, then Play Again.
Step 7 — play it all the way through
Menu → mode → track → race → winner → menu. Twice, with different choices.
The blue car is there even in 1 player.
Its show is outside the if <(players) = (2)> then, or another script shows it later. Search the sprite for every show.
The cars drive off during the countdown.
Their driving loop still starts on when flag clicked or on Game Play. It must start on Game Start.
The second race is on the first track.
Play Again goes back to Game Mode, so the track is re-chosen — but the Stage only switches backdrop on Game Play. Check that stack is still there and still runs.
The clock keeps counting into minus numbers.
Your repeat until <> is checking <() = (0)> but the number goes past 0 in one step. Use <() < (1)> instead, or make sure you only ever subtract 1.
Remember the choices
- Five variables, with sensible defaults set at the flag.
- Every button sets its variable before it broadcasts.
- Show them on the Stage while you click through, so you can see it working.
The race obeys
- The chosen track appears.
- One player means one car — the blue car is not on screen at all.
- The cars start on the line and wait for the countdown.
Teacher note
This is the first time the class has written code whose behaviour is decided somewhere else entirely. Ask what would have to change to add a third track. The answer — one more backdrop, one more button, one more line — is the reward for the last two lessons.
Somebody wins
- Lap Race ends when a car reaches the lap target.
- Time Race ends when the clock runs out, and the leader wins.
- A draw says so.
- The Play Again page names the winner and offers Play Again or Back to Menu.
Teacher reveal — where the judge should live
The deck puts the win check on the Stage, and that is a good instinct: it belongs to nobody, so it is easy to find. A sprite works just as well — what matters is that there is one of it.
The usual mistake is a win check inside each car. Two checks means two winners, or two broadcasts, or a race that ends twice. If a pupil’s result page flickers, that is why.
Setting, and How to Play
Your menu has two buttons that still do nothing. The deck’s last mission is to finish them.
- Setting — ask for the number of laps, the race time and each player’s name, and use them.
- How to Play — a page that explains the controls, with a way back.
Summary 5 min
- A broadcast says where to go. A variable says what was asked for. You need both.
- Give every setting a default before the player sees anything.
- The race reads the variables. It does not decide anything itself.
- A My Block is worth making even with no inputs — the countdown proves it.
- Two win conditions can end the same way, so the rest of the game stays simple.
- Default
- What a setting is before anybody changes it.
- State
- Everything the game currently knows: page, choices, scores.
- Game mode
- A different way to win the same game.
- Draw
- Nobody wins. Worth handling — it happens more than you think.
Hand in
Share your finished Car Race and send the link to your teacher. That is the second arc done — next lesson starts the Shooting Game.