- 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 means more friction: 0.8, not 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 know there are two players?
Reveal the answer
It does not. A broadcast carries nothing else. 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 uses the choices to set itself up
- 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, based on what the player chose.
- Write two ways to win that end the same way.
Learnt — joining it up 28 min
A broadcast carries nothing
This is the big idea of the lesson. It is easy to miss. 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 loses your choice. 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? Give each choice a default. A default is the value it starts with. Set them when you click the flag:
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 starts, there is nothing to work 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 shows up if the player asked for it:
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…
Make a new sprite with four costumes: 3, 2, 1 and GO!.
Each one does the same thing. It appears small, grows, and stays. That is 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 start too early.Two ways to win
Your player chose a game mode. Now make the game follow it:
- Lap Race: wait until someone 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 check decides who won. It works for both modes:
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. 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
Put both track backdrops in one project. Add one if / else on (track).
Step 4 — the cars line up
On when I receive [Game Play v], hide first. Go to the right start place 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
Make a new sprite with four costumes and the Pop block. Put broadcast [Game Start v] at the end. Change the cars’ driving loop to start on Game Start.
Step 6 — the judge
Add the two ways to win, the winner check, then Play Again.
Step 7 — play it all the way through
Menu → mode → track → race → winner → menu. Do it 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. Look in 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 you choose the track again. 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 <> checks <() = (0)>. But the number jumps past 0 in one step. Use <() < (1)> instead. Or only ever take away 1.
Remember the choices
- Five variables. Each one gets a default at the flag.
- Every button sets its variable before it broadcasts.
- Show them on the Stage while you click through. Then 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. The car with the most laps wins.
- A draw says so.
- The Play Again page names the winner. It has Play Again and Back to Menu buttons.
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 last mission is to finish them.
- Setting: ask how many laps, how long the race is, and each player’s name. Then use them.
- How to Play: a page that explains the keys, 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 can be useful even with no inputs. The countdown shows this.
- Two ways to win can end the same way. Then the rest of the game stays simple.
- Default
- What a setting is before anybody changes it.
- State
- Everything the game knows right now: page, choices, scores.
- Game mode
- A different way to win the same game.
- Draw
- Nobody wins. Plan for it. It happens more than you think.
Hand in
Share your finished Car Race and send the link to your teacher. The Car Race is done. Next lesson starts the Shooting Game.