Learning Goals 3 min
By the end of this lesson you will be able to:
- Recognise the three mouse-sensing blocks in the Sensing palette and name them: <mouse down?> (boolean), (mouse x), and (mouse y) (round reporters).
- Explain that (mouse x) and (mouse y) are live coordinates — they change every frame the mouse moves, and they use the same Stage range you already know:
x: −240..+240,y: −180..+180. - Combine <mouse down?> with forever + if <> then + go to x: () y: () to build a sprite that follows the mouse only while the button is held down.
Warm-Up — where is the mouse, anyway? 7 min
The Stage has its own number map. The middle is (0, 0), the right edge is x: 240, the top edge is y: 180. You've used those coordinates for sprites since Level 1. But the mouse pointer also has coordinates — and Scratch will tell you them, live, if you ask.
when flag clicked
forever
say (mouse x)
end
Imagine you hit the flag and then slide the mouse from the left edge of the Stage to the right edge. What numbers do you predict the cat will say?
Reveal the answer
The cat will say roughly -240 when the mouse is at the far left, climbing up through 0 in the middle, and finishing near 240 at the far right. The same range you use for sprite x-coordinates. If you drag the mouse off the Stage, the number keeps going (it clamps to the Stage area in some browsers and keeps reporting outside in others — try it).
Predict-this number two:
when flag clicked
forever
if <mouse down?> then
say [click!] for (0.2) seconds
end
end
Reveal the answer
Every time you click and hold a mouse button, "click!" appears for 0.2 seconds. If you just tap, you get one short flash. If you hold the button down, the bubble basically stays on the whole time — because the forever loop keeps re-asking the question and the answer keeps being true. Releasing the mouse stops the bubbles within 0.2 seconds.
Today's lesson is about those three blocks — <mouse down?>, (mouse x), and (mouse y) — and the fact that together they let you turn the mouse into a steering wheel, a paintbrush, or a trigger.
New Concept — three blocks, two shapes 15 min
Scroll down in the light-blue Sensing palette. Past the touching blocks and the distance-to block from last lesson, you'll find three mouse blocks sitting together.
The boolean: <mouse down?>
This one is hexagonal — same shape as touching [edge v]?. It reports true whenever any mouse button is being held down, and false the rest of the time.
<mouse down?>
It does not tell you which button (Scratch doesn't distinguish left/right/middle). It does not tell you where. It only tells you "is a button down right now, this instant?".
The reporters: (mouse x) and (mouse y)
These two are round — same shape as x position. They report the current mouse coordinates on the Stage.
(mouse x) :: sensing
(mouse y) :: sensing
Same coordinate system as sprites: x goes from about -240 (left edge) to 240 (right edge), y goes from -180 (bottom) to 180 (top). The numbers update continuously — there is no "click required". The instant you move the mouse, the reporter changes.
The classic combo: follow-the-mouse
Drop both reporters into the matching round slots of go to x: () y: () and put the whole thing in a forever loop:
when flag clicked
forever
go to x: (mouse x) y: (mouse y)
end
Gating it with <mouse down?>
Now wrap that follow-the-mouse block in an if-then with <mouse down?>. The sprite follows your pointer only while you're holding the button. Release, and it freezes. This is the heart of every drag-to-paint app:
when flag clicked
forever
if <mouse down?> then
go to x: (mouse x) y: (mouse y)
end
end
Worked Example — Aisyah's come-here pet 12 min
We're going to make the cat-pet come when called: while the button is held, the pet glues to the pointer; release, and the pet stays where you let go.
Step 1 — New project, keep the cat-pet
New Scratch project. Keep the default cat — this is our pet. (If you painted your own pet earlier, use that.) Roughly 60×60 pixels is plenty.
Step 2 — Position the pet
Drag the pet on the Stage to roughly x: 0, y: 0. We want it visible in the middle so we can see it move.
Step 3 — Drop the hat
From Events: when ⚑ clicked.
Step 4 — Add the forever loop
From Control: forever. Snap it under the hat.
Step 5 — Add the if-then
From Control: if <> then. Drop it inside the forever.
Step 6 — Drop <mouse down?> into the diamond
From Sensing: <mouse down?>. Drag it onto the diamond slot. It snaps in.
Step 7 — Add the go-to inside the if-then
From Motion: go to x: () y: (). Drop it inside the if-then C. The two round slots are empty.
Step 8 — Fill the round slots with (mouse x) and (mouse y)
From Sensing: drag (mouse x) into the left round slot, then drag (mouse y) into the right round slot. Both snap in.
Click the flag. Click on (or near) the pet and drag — the pet follows the pointer. Let go — the pet freezes. Move the mouse without holding — the pet ignores you. You just built a come-here pet with seven blocks, no special "make draggable" tickbox required.
The full assembled stack
when flag clicked
forever
if <mouse down?> then
go to x: (mouse x) y: (mouse y)
end
end
What you just built: the core of every paint program, every drag-and-drop puzzle, every "move the piece" board game on the Internet. The mouse is no longer just a clicker — it's a controller with coordinates.
Try It Yourself — three mouse drills 15 min
Goal: Make the cat follow the mouse all the time — not just while the button is held. Use go to x: (mouse x) y: (mouse y) inside forever, with no if-then at all.
when flag clicked
forever
go to x: (mouse x) y: (mouse y)
end
Think: Why do we need the forever? What would the script do without it?
Goal: Make a sprite that says the mouse coordinates as you move. When the button is not down, say (mouse x). When the button is down, say (mouse y) instead. Use one if <> then else (from L02-07) inside a forever.
when flag clicked
forever
if <mouse down?> then
say (mouse y)
else
say (mouse x)
end
end
Think: Without the if-else, both say blocks would run every frame and you'd only see whichever ran last. The if-else picks one path per frame.
Goal: "Follow with a delay." Make the sprite follow the mouse, but slowly — using glide (0.3) secs to x: (mouse x) y: (mouse y) instead of go-to. The sprite should trail behind the cursor like a tired puppy. Wrap in forever.
when flag clicked
forever
glide (0.3) secs to x: (mouse x) y: (mouse y)
end
Think: Compare to go to x: (mouse x) y: (mouse y) in a forever — that one is instant. The glide creates a smooth chase. Same blocks, different feel.
Mini-Challenge — Daniel's frozen draw 5 min
"Why won't the line follow?"
Daniel wants to draw with the mouse using the Pen extension. He drops the Pen extension and writes:
when flag clicked
erase all
pen down
go to x: (mouse x) y: (mouse y)
forever
if <mouse down?> then
pen down
else
pen up
end
end
Look closely at where the sprite actually goes during the forever loop.
Reveal one valid solution
The bug is that the sprite only goes to the mouse once — in the line before the forever. After that, the loop just toggles the pen on and off without ever moving the sprite again. So the pen draws a tiny dot at the spot where the mouse was at flag-click time, and nothing else.
The fix: move the go to x: (mouse x) y: (mouse y) inside the forever, so the sprite chases the mouse every frame:
when flag clicked
erase all
pen up
forever
go to x: (mouse x) y: (mouse y)
if <mouse down?> then
pen down
else
pen up
end
end
Now drag with the button held — line appears. Release — line stops. Press again somewhere else — new line. This is MS Paint in eight blocks. The pen-up/pen-down lives inside the loop, and so does the go-to. If you want a sprite to track the mouse continuously, the tracking block has to live inside the loop too.
Recap 3 min
You met three new Sensing blocks that turn the mouse into an input device. <mouse down?> is hexagonal — it reports true while any mouse button is held. (mouse x) and (mouse y) are round — they report the pointer's live Stage coordinates, in the same -240..+240 / -180..+180 range you use for sprites. Drop them into go to x: () y: () for follow-the-mouse, gate that with the boolean for click-to-drag, or save them into variables to remember a click position. Every mouse-based game and paint app rests on these three blocks.
- Boolean reporter
- A hexagonal block that reports true or false. <mouse down?> is one — true while a mouse button is held, false otherwise.
- Round reporter
- An oval block that reports a value (usually a number). (mouse x) and (mouse y) are round; they fit into any round slot.
- Live value
- A reporter whose answer changes on its own as the world changes. The mouse reporters are live — you don't read them, they are the current truth, every frame.
- Stage coordinates
- The Scratch coordinate system.
xfrom-240(left) to240(right).yfrom-180(bottom) to180(top). The mouse uses exactly this system. - Drag-to-paint
- The classic combo of <mouse down?> + go to x: (mouse x) y: (mouse y) inside a forever — the heart of every paint, draw, or drag-puzzle app.
Homework 2 min
Mouse-Brush Mini-App. Build a tiny paint program using only what you know.
- New project. Delete the cat. Add a small round sprite (a dot, a circle — paint your own or pick from the library) and add the Pen extension from the bottom-left button.
- Top of the script: when ⚑ clicked → erase all → pen up.
- Then a forever. Inside the loop: go to x: (mouse x) y: (mouse y), then an if <mouse down?> then else with pen down in the yes branch and pen up in the no branch.
- Add a second sprite — a tiny "C" button. Give it a when this sprite clicked → erase all script so clicking it wipes the canvas.
Save as HW-L2-16-Mouse-Brush.sb3. Click the flag and draw your name, a kuih, or a teh tarik glass with milky lines. Use the C button to clear.
Bring back next class:
- The
.sb3file. - Your answer to: "What happens if you swap (mouse x) and (mouse y) in the go-to block? Try it. Why does it feel so wrong?"
Heads up for next class: SCR-L02-17 does for the keyboard what today did for the mouse — meeting when [space v] key pressed (the hat block, fires once) and <key [space v] pressed?> (the boolean, true while held). Two ways, one keyboard.