- Learnt28 min · three needs, one mood, a pet that paces
- Basic25 min · build all three
- Challenge 1–327 min · readable buttons, a pet that says what is wrong, a fair game
- Extrabonus · a pet that sleeps
Review — where we got to 5 min
Your pet has food, a bowl, a mood, an ending and a record. Two ideas come back today.
- The clock loop. It is a wait and a change block inside a forever.
- The mood script. It is an if that reads a variable and picks a costume.
Quick-fire
With one need, the whole game is “click the bowl before the number runs out”. What does a second need add?
Reveal the answer
A choice. Two things go down at different speeds. The player must decide which one to fix first. That choice is the game.
Today’s Topic 3 min
- Three needs, three buttons, and three clocks at three speeds.
- A mood that comes from the worst of the three.
- Why three ifs in a row are better than one if / else here.
- A pet that walks up and down when it is happy. It stops when it is not.
Learning outcome
By the end of this lesson you will be able to:
- Run a few variables at once. Each has its own clock.
- Choose a costume by checking a few variables, one by one.
- Make a sprite move because of a variable, not a key.
Learnt — three needs and one face 28 min
1 · The same clock, three times
You already have one clock. The other two are copies of it. They have different names and different waits.
- Make two more variables:
funandsleep. - Right-click your food clock and choose Duplicate. Twice.
- Change the variable in each copy using its dropdown.
- Give each one a different wait. Use 1.1, 1.7 and 2.3 seconds.
- Press the flag. Watch all three go down at once.
when flag clicked
set [fun v] to (10)
forever
wait (1.7) seconds
change [fun v] by (-1)
end
Each need gets its own button. Each button has three blocks.
when this sprite clicked
start sound [Boing v]
change [fun v] by (4)
How to use this: Click the green flag, then click the bowl, the ball or the bed on the stage — one for each of the pet’s three needs.
2 · One face, three questions
The pet has one face. Three things can be wrong with it. Start with the happy face. Then let any of the three spoil it.
when flag clicked
forever
switch costume to [dog2-a v]
if <(food) < (4)> then
switch costume to [dog2-c v]
end
if <(fun) < (4)> then
switch costume to [dog2-c v]
end
if <(sleep) < (4)> then
switch costume to [dog2-c v]
end
end
Why not an if / else?
- An if / else has two answers. Here there are three ways to be sad.
- Three if / elses inside each other are nine blocks deep. That is too hard to read.
- So set happy first, then test three times. It is flat and short. It is easy to add a fourth need.
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
3 · A happy pet moves
One more if makes the pet feel alive. A pet with enough fun walks up and down. A bored pet sits still.
- On the pet’s green-flag stack, add
set rotation style [left-right v]. - Add another green-flag stack. Put a forever loop in it.
- Inside the loop, put
if <(fun) > (4)> then. - Inside THAT, put two glide blocks. One goes to the right. One comes back.
- Play it, and stop clicking the ball. The walking stops on its own.
when flag clicked
forever
if <(fun) > (4)> then
glide (1.5) secs to x: (120) y: (-30)
glide (1.5) secs to x: (-120) y: (-30)
end
end
set rotation style, the pet walks back upside down.How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
How to use this: Click the green flag, then click the bowl, the ball or the bed on the stage — one for each of the pet’s three needs.
Basic — build all three 25 min
Add one need and test it. Then add the next. Never add two at once.
Step 1 — the second need
- Make a variable called
fun. - Duplicate the food clock. Change both dropdowns in the copy.
- Give it a different wait time.
- Add a Ball sprite. Put it beside the bowl. Give it the three-block clicked stack.
- Play for a minute with two needs.
Step 2 — the third
- Do the same again for
sleep. - Its button can be any sprite. Try a bed, a moon or a plain button.
- Give it the slowest clock of the three.
Step 3 — the mood
- Open your old mood script. It only knows about food.
- Change it so it sets the happy costume FIRST, inside the loop.
- Then add three ifs, one for each need.
- Test it. Let each need run out on its own.
Step 4 — the pacing
- Add
set rotation style [left-right v]to the green-flag stack. - Add the pacing stack.
- Play. Watch the pet stop when it gets bored.
Step 5 — tune the three speeds
- Play a whole game. Which need did you have to fix most?
- Slow that one down.
- Play again. Keep going until no one button fixes everything.
My second clock changes the food, not the fun.
The copy kept the old variable. You must change two dropdowns. One is in the set block. One is in the change block.
The pet is sad for ever, even after I feed everything.
The happy costume is set once at the top of the script, outside the forever loop. It must be the first block INSIDE the loop.
The pet walks back upside down.
set rotation style [left-right v] is missing. It goes on the green-flag stack, once.
Clicking a button feeds the wrong need.
You copied a button but only changed one dropdown. Open each button. Read its change block.
Three buttons anybody can read
Anyone who sits down at your game should know what the three buttons do. They should not need to ask. That includes you, a week from now.
- Each button looks like the thing it does.
- Each one makes its own sound.
- They are lined up in a place where nobody clicks them by mistake.
Say what is wrong
A sad face does not say which need is low. Make the pet ask for the thing it really wants.
- Each need has its own sentence.
- The pet asks for the lowest one. It does not ask for all three at once.
- It says nothing at all when everything is fine.
Teacher note — comparing two variables
Asking for the LOWEST need means comparing two variables against each other, which is new: every comparison so far has had a typed number on one side.
if <(food) < (fun)> then
say [I am hungry.] for (1.5) seconds
else
say [Play with me!] for (1.5) seconds
end
That handles two. Three is harder and a pupil who only does two has done the interesting part. Accept it, and let anybody who wants the third try — the honest answer needs nested ifs and it is a good stretch.
Make it a fair fight
Each of the three needs should matter. Change the speeds until no one button fixes everything.
- The three clocks all run at different speeds.
- In a whole game, you press each button about the same number of times.
- You can say which one you slowed down and why.
- You can last two minutes, but only if you think hard.
Teacher note — count the clicks
Stand behind a pair and tally which button gets pressed. A game where one button gets twenty presses and another gets two is not balanced, and the tally is a far better argument than an opinion. This is the same skill as last lesson’s speed tuning, on three numbers instead of one.
A pet that sleeps
Clicking the bed should put the pet to sleep for a while. It stops getting hungry. It stops walking. None of the other buttons work. But sleep goes up fast.
- You can see that it sleeps. Use a costume, a thought bubble, or both.
- Food and fun stop going down while the pet is asleep.
- The bowl and the ball do nothing while it sleeps.
- It wakes up on its own. Nobody clicks it.
Teacher notes — a switch, and everything that has to ask about it
The tidy answer is one variable used as a switch — the same trick the Space Race will use for its countdown next month.
when this sprite clicked
set [asleep v] to (1)
change [sleep v] by (6)
wait (5) seconds
set [asleep v] to (0)
And every clock has to ask about it:
forever
wait (1.1) seconds
if <(asleep) = (0)> then
change [food v] by (-1)
end
end
The interesting failure is forgetting one of them. A pet that sleeps but still gets hungry is a bug the pupils will find by playing, and finding it is worth more than being warned about it. Ask them afterwards how many places had to change: the answer is every script that touches a need, which is why a switch is a bigger decision than it looks.
Going further: next lesson the pet gets a name and the game gets a front page.
Summary 4 min
One need was boring. Three needs at three speeds make a game with choices in it.
- Each need is a variable, a clock and a button. They are three copies of one shape.
- Different speeds make the player choose.
- Set the happy costume first, then let three ifs spoil it.
- A variable can decide when a sprite moves, not just a key.
- Need
- A variable the player has to keep up. This game has three.
- Mood
- A costume picked by looking at every need, one by one.
- Rotation style
- If a sprite turns, flips, or stays upright when it changes direction.
- Balance
- Setting the speeds so every button matters the same.
Next lesson: you give the pet a name. It gets a front page, and an ending that says how you did.