- Learnt27 min · swing, stop, kick, too hard
- Basic25 min · add the meter
- Challenge 1–327 min · swing, stop, judge
- Extrabonus · faster every goal
Review — where we got to 5 min
- The marker sits on the mouse. The arrow uses
point towards [mouse-pointer v]to face it. - At the click we save
(mouse x)and(mouse y)intoaim xandaim y. - Four comparisons, joined by three ands, decide GOAL or miss.
Quick-fire
A shot lands at x = 100 exactly — on the right post. Goal or miss?
Reveal the answer
Miss. <(aim x) < (100)> asks for less than 100, and 100 is not less than 100. The post itself is out.
Today’s Topic 3 min
- A variable that climbs to 100 and falls back to 0, for ever
- A meter that grows with it
- A key that stops it
- Shots that sail over the bar, or dribble short
Learning outcome
By the end of this lesson you will be able to:
- Make a number swing between two limits, using a second variable for its step.
- Stop a loop with
repeat until <key [space v] pressed?>. - Change what a shot does depending on a number.
Learnt — a meter you have to time 27 min
1 · A number that swings
We want (power) to climb to 100, fall back to 0, and climb again. One variable cannot remember which way it is going. So we use two.
| Variable | What it holds |
|---|---|
(power) | How hard the kick is, 0 to 100. |
(step) | How much power changes each time round. 2 going up, −2 going down. |
- Make
powerandstep, For all sprites. - In a
forever, addchange [power v] by (step). Drop the(step)reporter into the number slot. - Add
if <(power) > (99)> thenwithset [step v] to (-2)inside. That is the top. - Add
if <(power) < (1)> thenwithset [step v] to (2)inside. That is the bottom. - Tick both variables so their boxes show. Click the flag and watch.
when flag clicked
set [power v] to (0)
set [step v] to (2)
forever
change [power v] by (step)
if <(power) > (99)> then
set [step v] to (-2)
end
if <(power) < (1)> then
set [step v] to (2)
end
set size to (power) %
end
change [power v] by (step) never changes. Only (step) does.How to use this: Click the green flag and watch the meter climb to 100 and fall back to 0. Nothing to press.
2 · A meter that grows upwards
set size to (power) % turns the number into a picture. But Scratch grows a sprite around its centre. A bar with its centre in the middle grows up and down.
- Paint a new sprite. Name it
Power. - Draw a tall, thin triangle, point down. Put the point exactly on the little centre cross in the paint editor.
- Keep all of the triangle above the cross.
- Move the sprite to the side of the goal:
go to x: (200) y: (-150). - Run the swing script. The wedge grows up from its tip.
3 · SPACE stops it
Swap the inner loop for repeat until <key [space v] pressed?>. It swings until you press SPACE, then carries on below.
- From Control, drag a
repeat untilinside the forever. - Move the four swinging blocks into it.
- From Sensing, drop
<key [space v] pressed?>into its slot. - Under the
repeat until, addsay (join [Power: ] (power)) for (1) seconds.
when flag clicked
set [power v] to (0)
set [step v] to (2)
forever
repeat until <key [space v] pressed?>
change [power v] by (step)
if <(power) > (99)> then
set [step v] to (-2)
end
if <(power) < (1)> then
set [step v] to (2)
end
set size to (power) %
end
say (join [Power: ] (power)) for (1) seconds
end
How to use this: Click the green flag, click the Stage once, then press the space bar to stop the meter. It says the power, then swings again.
4 · The meter takes the kick
Last lesson the ball kicked on a click. Now the kick happens when SPACE stops the meter. So the meter saves the aim, then tells the ball to go.
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
broadcast [kick v] and wait
repeat until. Your right hand aims; your left hand times it.5 · Too hard, too soft
Two new rules, at the top of the ball’s kick. They move where the ball is going.
- On the ball, start a new script with
when I receive [kick v]. - Add
if <(power) > (85)> thenwithset [aim y v] to (150). That is up in the crowd. - Add
if <(power) < (30)> thenwithset [aim y v] to (-70). That is on the grass, short of the line. - Then the glide and the goal test from last lesson.
when I receive [kick v]
if <(power) > (85)> then
set [aim y v] to (150)
end
if <(power) < (30)> then
set [aim y v] to (-70)
end
glide (0.5) secs to x: (aim x) y: (aim y)
if <<<(aim x) > (-100)> and <(aim x) < (100)>> and <<(aim y) > (-25)> and <(aim y) < (85)>>> then
say [GOAL!] for (1) seconds
else
say [Miss!] for (1) seconds
end
go to x: (0) y: (-118)
How to use this: Click the green flag and watch. Three kicks are taken for you at the same spot: power 95, then 20, then 60. Nothing to press.
Basic — add the meter 25 min
Open last lesson’s project. The Aim and Arrow sprites stay exactly as they are.
Step 1 — the Power sprite
- Paint the wedge, tip on the centre cross.
- Make
powerandstep. - Build the swing from Learnt 1. Watch three swings.
Step 2 — the frame (optional, but worth it)
- Paint the outline sprite. Colour the top band red.
- Place it at the same x and y as Power.
- Drag Power in front if the frame covers it.
Step 3 — SPACE to kick
- Wrap the swing in
repeat untilSPACE. - Under it: save the aim, then broadcast
kickand wait.
when flag clicked
set [power v] to (0)
set [step v] to (2)
forever
repeat until <key [space v] pressed?>
change [power v] by (step)
if <(power) > (99)> then
set [step v] to (-2)
end
if <(power) < (1)> then
set [step v] to (2)
end
set size to (power) %
end
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
broadcast [kick v] and wait
end
Step 4 — the ball listens
- Delete the ball’s old
wait until <mouse down?>loop. - Keep a small flag script: goals to 0, back to the spot.
- Build the kick from Learnt 5, with the goals counter back in it.
when flag clicked
set [goals v] to (0)
go to x: (0) y: (-118)
when I receive [kick v]
if <(power) > (85)> then
set [aim y v] to (150)
end
if <(power) < (30)> then
set [aim y v] to (-70)
end
glide (0.5) secs to x: (aim x) y: (aim y)
if <<<(aim x) > (-100)> and <(aim x) < (100)>> and <<(aim y) > (-25)> and <(aim y) < (85)>>> then
change [goals v] by (1)
say [GOAL!] for (1) seconds
else
say [Miss!] for (1) seconds
end
go to x: (0) y: (-118)
Step 5 — play it
Ten kicks. Write down your goals. Then ten more, trying to beat it.
The meter grows down as well as up.
The wedge’s tip is not on the centre cross. Open the costume, select the whole drawing, and drag it until the tip sits on the cross.
Power climbs past 100 and never comes back.
The top test is = 100, or the step is not 2. Use <(power) > (99)>.
The meter sticks at 0.
step is 0. Check the flag script sets it to 2 before the loop.
One press of SPACE kicks twice.
You held SPACE through the whole kick, so the meter stopped again at once. Add wait until <not <key [space v] pressed?>> after the broadcast.
A click still kicks the ball.
The old wait until <mouse down?> loop is still on the ball, so two scripts are kicking it. Delete the old loop.
A meter that swings
- Power climbs to 100, falls to 0, and climbs again, for ever.
- The meter grows up from its tip, never down.
- The power box on the Stage never shows less than 0 or more than 100.
Stop it and shoot
- SPACE freezes the meter.
- The ball flies to where the mouse was at that moment.
- The meter waits until the ball is back on the spot.
- Then it swings again.
Teacher note
Ask what happens with a plain broadcast instead of broadcast and wait. Let them try it and press SPACE twice quickly. Two kicks at once, one ball, a mess. The fix is one word.
Too hard, too soft
- Power above 85 sails over the bar.
- Power below 30 stops short of the goal line.
- Anything in between flies where you aimed.
- Goals count, and start at 0 each flag.
Teacher reveal — two ifs before the glide
if <(power) > (85)> then
set [aim y v] to (150)
end
if <(power) < (30)> then
set [aim y v] to (-70)
end
aim y and nothing else. A pupil who adds new tests to the goal check instead has built something that works but reads badly — worth showing both side by side.A good follow-up: make the ball say Over the bar! or Too soft!. That needs the Ball to remember which rule fired — a third variable, or a say inside each if before the glide.
How to use this: Click the green flag and click the Stage once. Aim with the mouse, then press the space bar to stop the meter and shoot.
Faster every goal
A game should get harder as you get better. Make the meter swing faster after every goal.
How to use this: Click the green flag and click the Stage once. Aim with the mouse, press the space bar to shoot. Every goal makes the meter swing faster.
Teacher reveal — the meter, reading meter speed
when flag clicked
set [power v] to (0)
set [step v] to (meter speed)
forever
repeat until <key [space v] pressed?>
change [power v] by (step)
if <(power) > (99)> then
set [step v] to ((0) - (meter speed))
end
if <(power) < (1)> then
set [step v] to (meter speed)
end
set size to (power) %
end
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
broadcast [kick v] and wait
end
((0) - (meter speed)) is how Scratch writes “minus meter speed”. The Ball sets meter speed to 2 at the flag and adds 1 inside its GOAL half.Summary 3 min
- A number that swings needs two variables: the number, and its step.
- Two ifs flip the step at the ends. Test with
>and<, not=. - Scratch grows a sprite around its centre. Put the meter’s tip there.
repeat until <key [space v] pressed?>runs until a key stops it.broadcast [kick v] and waitkeeps the meter still until the kick is over.
- Power
- How hard the kick is, from 0 to 100.
- Step
- How much a number changes each time round a loop.
- Swing
- A number going up to a limit, then down to another, over and over.
- Centre
- The point a sprite turns and grows around. The cross in the paint editor.
Next lesson: the keeper stops standing still.