- Learnt28 min · friction, ask and answer, five pages, buttons as costumes
- Basic24 min · build the menu page
- Challenge 1–327 min · friction, the menu, all five pages
- Extrabonus · a Setting page
Review — where we got to 5 min
- The key changes a number. One move block does the moving.
- Speed × 0.9 every loop is friction. It makes the car roll and slow down.
- Five checkpoint posts, in order, make a lap honest.
Quick-fire
Your car keeps 90% of its speed each loop. What if you changed that to 80%?
Reveal the answer
It would slow down faster. That is more friction, like driving on sand. That one number is today’s first idea.
Today’s Topic 3 min
- Grass as friction, not as a wall
ask () and wait: letting the player type- A game with five pages
- Buttons as costumes, not as sprites
Learning outcome
By the end of this lesson you will be able to:
- Change how a car handles by changing one number.
- Ask the player a question and keep the answer.
- Build a menu from one sprite. Move between pages with broadcasts.
Learnt — friction and pages 28 min
Off the track, properly
Last week’s mission asked for a car that is slow on the grass. It must not be sent back to the start. You already have the block that does it: friction.
On the road you keep 90% of your speed each loop. On the grass, keep only 80%. That is all you change.
if <touching color [#3FA34D]?> then
set [speed v] to ((speed) * (0.8))
set [steering v] to ((steering) * (0.7))
else
set [speed v] to ((speed) * (0.9))
set [steering v] to ((steering) * (0.85))
end
Letting the player type
In Sensing there is a block you have not used yet:
ask [How many laps to win?] and wait
set [laps to win v] to (answer)
answer.There is only one answer. Ask a second question and the first reply is gone.
So copy it straight away. Put it into (laps to win), player name, or what you need it for.
A game has pages
Big games have more than one screen. Ours will have five:
- Menu: 1 player, 2 players, Setting, How to Play
- Game Mode: Time Race or Lap Race
- Race Track: which track
- Game Play: the race itself
- Play Again: who won, and what next
Each page is a broadcast. Send it, and the game moves on.
The problem with buttons
In Level 1’s Lesson 10 you made buttons the easy way: one sprite each. Four buttons, four sprites, four scripts. That was fine.
Five pages of buttons is fifteen sprites. That is fifteen scripts that are all nearly the same. You will get lost by the second page.
Buttons as costumes
Here is a better way. It is the same idea as the checkpoints last week:
- One sprite per page. Call it Menu.
- One costume per button on that sprite. Name it after what it says.
- A My Block that places one button and clones it.
- Hide the real sprite. Only the clones are ever seen.
define Button (x) (y) (name) (size)
go to x: (x) y: (y)
switch costume to (name)
set size to (size) %
create clone of [myself v]
when flag clicked
hide
Button (0) (120) [Title] (90) :: custom
Button (0) (40) [1 Player] (80) :: custom
Button (0) (-20) [2 Players] (80) :: custom
Button (0) (-80) [Setting] (80) :: custom
Button (0) (-140) [How to Play] (80) :: custom
A clone finds out who it is
Every clone runs the same script. So every clone would broadcast the same thing. It must check its own costume to know which button it is:
if <<touching [mouse-pointer v]?> and <mouse down?>> then
wait until <not <mouse down?>>
if <(costume [name v]) = [1 Player]> then
broadcast [Game Mode v]
end
if <(costume [name v]) = [Setting]> then
broadcast [Setting v]
end
end
Making it feel like a button
Make every button darker. Make the one under the mouse bright:
forever
if <touching [mouse-pointer v]?> then
clear graphic effects
else
set [brightness v] effect to (-15)
end
end
Two last fixes
The title lights up too. It is a costume on the same sprite. So it gets the same effect. But clicking it does nothing. That looks broken. Wrap the whole thing:
if <not <(costume [name v]) = [Title]>> then
end
Only the clicked button disappears. Each clone should go when any button is clicked:
when I receive [Game Mode v]
delete this clone
Basic — build the menu page 24 min
Step 1 — the friction fix
In your race, wrap the two friction lines in an if / else on touching color [grass]?. Use 0.8 and 0.7 in the grass half. Do it for both cars.
Step 2 — a new sprite called Menu
Paint your title as costume 1. Then paint one costume per button. Name each costume just what it says. Your code will check those names.
Step 3 — the Button block
Make a block with four inputs: x, y, name, size. Inside it put go to, switch costume, set size, create clone.
Step 4 — place them
Use when flag clicked and hide. Then use the Button block once per button.
Step 5 — the clone script
Show, go to front, then the forever loop. Put the hover effect first, then the click check.
Step 6 — the two fixes
Wrap everything in the “not Title” check. Add a when I receive [Game Mode v] → delete this clone for each message.
Nothing appears at all.
The clone script needs show. The real sprite is hidden. A clone copies the sprite, so it starts hidden too.
Every button does the same thing.
The costume check is missing. Or two of your costumes have the same name. Show costume [name v] on the Stage. Put the mouse over each one.
One click skips two pages.
You have no wait until <not <mouse down?>>. The press is still going when the next page appears.
The buttons stay on screen after I click.
A clone only deletes on the messages you give it. Add a when I receive [Game Mode v] stack for every message this page can send.
Grass that costs you
- More friction off the track, for both speed and steering.
- Both cars, each with their own numbers.
- Nothing resets and nothing jumps to a new place.
One sprite, one menu
- A menu page made from one sprite, with one costume per button.
- Buttons light up under the mouse. The title does not.
- Clicking any button clears the whole page.
- Each button sends a different message.
Teacher note
Some will have already built it with a sprite per button, and it will work. Do not undo it for them — ask them to add three more pages that way first. The refactor sells itself somewhere around the ninth sprite.
All five pages
This is the main mission.
- Menu, Game Mode, Race Track, Game Play, Play Again.
- Two game modes: Time Race and Lap Race.
- Two tracks to choose from.
- The choices go into variables. Then Game Play knows what to show.
Teacher reveal — pages are a state machine
A variable called page, holding one word, is worth adding even though nothing strictly needs it. Show it on the Stage while they click around. Suddenly the whole game is one word changing, and a page that fails to clear is obvious rather than mysterious.
This shape — one variable saying where you are, broadcasts moving you between states — is how menus, levels and game-over screens work in every language they will ever use.
A Setting page that works
Your menu has a Setting button. Make it do something.
- Ask how many laps win the race, and use that number.
- Ask for each player’s name. Show it on the Play Again page.
- Ask how fast the cars should be.
Summary 5 min
- Off the track means more friction. It slows you, but does not send you back.
ask () and waitfills one place calledanswer. The next question wipes it.- A game is a set of pages. A broadcast moves you between them.
- A page’s buttons are costumes on one sprite, cloned by a My Block.
- A clone checks its own costume. That tells it what to do.
- answer
- Where Scratch puts what the player typed. There is only one.
- Page
- One screen of a game. Only one is ever showing.
- Refactor
- Changing how code is written, but not what it does.
- Hover
- When the mouse is over something, but not clicking it.
Hand in
Share your project and send the link to your teacher. Next lesson is the last Car Race lesson. The pages and the racing join up.