- Learnt27 min · the aim, the arrow, the kick, the goal
- Basic25 min · build the penalty
- Challenge 1–327 min · aim, kick, judge
- Extrabonus · pick your corner
Review — the Road Crossing 5 min
- Every car in a lane is a clone of one sprite.
- A variable made For this sprite only gives each clone its own
(speed). - To ride a log, the player moves by the log’s speed while touching it.
Quick-fire
Ten clones all read (speed). They all move at the same speed. Why?
Reveal the answer
(speed) was made For all sprites. There is only one of it, so every clone reads the same number. Today’s variables are the opposite: we want one shared (aim x) that every sprite can read.
Today’s Topic 3 min
- An aim marker that follows the mouse
- An arrow that points towards it
- Two kinds of click: on a sprite, and on the Stage
- A kick that goes exactly where you clicked
- Four comparisons that decide GOAL or miss
Learning outcome
By the end of this lesson you will be able to:
- Make a sprite follow the mouse, and another face it.
- Save the mouse’s x and y at the moment of a click.
- Test whether a point is inside a box, using four comparisons and
and.
Learnt — aim, kick, judge 27 min
1 · The rules, before the blocks
- The ball starts on the penalty spot.
- You point at the goal with the mouse.
- You click. The ball flies to that spot.
- Inside the posts and under the bar is a goal. Anything else is a miss.
That is a whole game. Next lesson adds power, then a keeper who fights back.
2 · An aim marker on the mouse
You met this in the Shooting Game. A marker that sits on the mouse, for ever, in front of everything.
- Add a small sprite for the marker. A circle with a cross is plenty. Name it
Aim. - From Looks, add
go to [front v] layer. - From Motion, put
go to [mouse-pointer v]inside aforever. - Click the green flag and move the mouse. The marker sticks to it.
when flag clicked
set size to (70) %
go to [front v] layer
show
forever
go to [mouse-pointer v]
end
3 · An arrow that points towards it
A kicker looks where they are aiming. Our arrow does the same job. It stays over the spot and turns to face the marker.
- Add the Arrow1 sprite from the library. Name it
Arrow. - Put it just above the ball:
go to x: (0) y: (-85). - From Motion, drag
point towards [mouse-pointer v]into aforever. - Click the flag. Circle the mouse round the goal. The arrow turns with it.
when flag clicked
go to x: (0) y: (-85)
forever
point towards [mouse-pointer v]
end
How to use this: Click the green flag, then move the mouse over the goal. The marker follows it and the arrow turns to face it. Nothing to click yet.
4 · Two ways to hear a click
Scratch has two click hats in Events. Each click goes to only one of them.
when this sprite clicked
say [You clicked me!] for (1) seconds
when stage clicked
broadcast [stage clicked v]
The Stage cannot talk, so it sends a message. The keeper hears it and speaks for it:
when I receive [stage clicked v]
say [You clicked the grass.] for (1) seconds
How to use this: Click the green flag. Then click the goalkeeper, and then click the grass. Each click is heard by a different script.
5 · The kick — save the mouse first
The ball waits for the mouse button. Then it saves where the mouse is, and glides there.
- Make two variables,
aim xandaim y, For all sprites. - On the ball, add
wait until <mouse down?>inside aforever. - Then
set [aim x v] to (mouse x)andset [aim y v] to (mouse y). The round reporters are in Sensing. - Then
glide (0.5) secs to x: (aim x) y: (aim y). - Wait a second, and send the ball back to the spot.
when flag clicked
go to x: (0) y: (-118)
forever
wait until <mouse down?>
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
glide (0.5) secs to x: (aim x) y: (aim y)
wait (1) seconds
go to x: (0) y: (-118)
end
(0, -118) is the white dash on the Soccer backdrop.How to use this: Click the green flag, then point at the goal and click. The ball flies to where you clicked, waits, and comes back.
6 · Inside the goal? Four tests
A shot is a goal when it is inside a box. The box has four edges, so there are four tests.
| Edge | A goal must be… | The test |
|---|---|---|
| Left post | to the right of it | <(aim x) > (-100)> |
| Right post | to the left of it | <(aim x) < (100)> |
| Goal line | above it | <(aim y) > (-25)> |
| Bar | below it | <(aim y) < (85)> |
All four must be true. <<> and <>> from Operators joins two answers. So we build it in two halves, then join the halves.
<<(aim x) > (-100)> and <(aim x) < (100)>>
<<(aim y) > (-25)> and <(aim y) < (85)>>
- Drag out one
<<> and <>>. Put a<() > (50)>in its left side and a<() < (50)>in its right. - Drop
(aim x)into both. Type-100and100. - Build the second half the same way with
(aim y). - Drag a third
<<> and <>>. Drop one half into each side. - Put the whole thing in an
if / elseafter the glide.
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
This demo takes eight shots for you at random spots. Watch the numbers, then the verdict.
when flag clicked
repeat (8)
go to x: (0) y: (-118)
set [aim x v] to (pick random (-180) to (180))
set [aim y v] to (pick random (-60) to (150))
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
end
go to x: (0) y: (-118)
(pick random (-180) to (180)) plays the part of your hand.How to use this: Click the green flag and watch. Eight shots are taken for you at random spots. Nothing to press.
Basic — build the penalty 25 min
New project. Choose the Soccer backdrop. Three sprites: Ball, Arrow, Aim.
Step 1 — the ball on the spot
- Add Soccer Ball from the library. Name it
Ball. - Set its size to 60.
- Add
go to x: (0) y: (-118)under a green flag. Check it sits on the white dash.
Step 2 — the aim
- Paint or pick a small marker. Name it
Aim. - Build its stack from Learnt 2. Test it.
Step 3 — the arrow
- Add Arrow1. Name it
Arrowand set its size to 70. - Build its stack from Learnt 3. Test it.
Step 4 — two variables
- Make
aim xandaim y, For all sprites. - Make
goalsandshotstoo. Leave their boxes showing on the Stage.
Step 5 — the kick and the verdict
- Build the Ball’s kick from Learnt 5.
- Replace the wait with the if / else from Learnt 6.
- Add
change [shots v] by (1)after the glide, andchange [goals v] by (1)inside the GOAL half.
Step 6 — reset at the flag
- Click the Stage in the sprite list.
- Give it
when flag clicked,set [goals v] to (0)andset [shots v] to (0).
Step 7 — check against this
when flag clicked
go to x: (0) y: (-118)
forever
wait until <mouse down?>
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
glide (0.5) secs to x: (aim x) y: (aim y)
change [shots v] by (1)
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)
end
Every shot says GOAL!, even wide ones.
One of the four tests points the wrong way. Read each one aloud: “aim x is greater than minus one hundred”. The left post wants greater than; the right post wants less than.
Every shot says Miss!, even in the middle.
Check the minus signs. 100 where -100 should be makes a goal impossible. Also check you used (aim y) in the second half, not (aim x) twice.
The ball lands somewhere I did not click.
The glide is reading (mouse x) instead of (aim x). The mouse moved while you waited.
The arrow flies off with the mouse.
It has go to [mouse-pointer v] instead of point towards [mouse-pointer v]. The first moves it; the second only turns it.
Holding the button kicks again and again.
That is what the loop asks for. Add wait until <not <mouse down?>> at the end, as the Shooting Game did.
Aim and point
- A marker follows the mouse everywhere, in front of everything.
- An arrow sits just above the ball.
- The arrow always faces the marker.
- The arrow never leaves its place.
The kick
- One click, one kick.
- The ball glides from the spot to where you clicked.
- It lands there even if you move the mouse straight away.
- It goes back to the spot, ready for the next one.
Teacher note
Ask a pupil to glide to (mouse x) first and flick the mouse during the flight. The ball wanders. Then ask why saving it fixes that. The idea — keep a copy of a changing value — comes back every lesson in this arc.
Goal or miss
- Inside the posts and under the bar says GOAL!.
- Wide, high or short says Miss!.
goalsandshotscount on the Stage, and start at 0 each flag.
Teacher reveal — the four edges
The eight-shot test is the point. Most mistakes hide at one edge only, so a pupil who tests the middle three times will never find them.
<<<(aim x) > (-100)> and <(aim x) < (100)>> and <<(aim y) > (-25)> and <(aim y) < (85)>>>
How to use this: Click the green flag, then point at the goal and click to shoot. Watch goals and shots count up.
Pick your corner
A shot near a post is harder to save. Make it worth more: 3 points for a corner, 1 for the middle.
How to use this: Click the green flag, then click to shoot. A goal near a post scores 3 points; the middle scores 1.
Teacher reveal — one way to build it
when flag clicked
forever
go to x: (0) y: (-118)
wait until <mouse down?>
set [aim x v] to (mouse x)
set [aim y v] to (mouse y)
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
if <<(aim x) < (-70)> or <(aim x) > (70)>> then
change [points v] by (3)
say [Corner! +3] for (1) seconds
else
change [points v] by (1)
say [Goal! +1] for (1) seconds
end
else
say [Miss!] for (1) seconds
end
end
points to 0 at the flag.Summary 3 min
go to [mouse-pointer v]moves a sprite onto the mouse.point towards [mouse-pointer v]only turns it.when this sprite clickedandwhen stage clickednever both run for one click.<mouse down?>hears a click anywhere.- Save the mouse at the moment of the click. The mouse will not wait.
- Four edges, four comparisons, three ands.
- Aim
- The spot you are pointing at. We keep it in aim x and aim y.
- Comparison
- A question with a yes or no answer, like “is aim x less than 100?”.
- and
- Says yes only when both sides say yes.
- Region
- An area of the Stage, like the inside of the goal.
Next lesson: a power meter. Kick it too hard and the ball sails over the bar.