- Learnt28 min · pen colour, pen size, and whose pen it is
- Basic25 min · five colour buttons
- Challenge 1–327 min · a colour you can see, brush keys, a brush that stops
- Extrabonus · a surprise colour
Review — Sketch Pad I 5 min
- The Pen blocks come from an extension.
pen downdraws,pen upstops,erase allwipes.- The pencil follows the mouse and draws while
<mouse down?>is true.
Quick-fire
Your pencil draws while the button is held. You click a button at the top of the Stage. What happens to the paper?
Reveal the answer
Clicking holds the button down, so the pencil draws a line up to the button. That is why last lesson’s Extra kept the top strip clean — and today every pencil does it.
Today’s Topic 3 min
set pen color to [#D400AB]andset pen size to (4)- Every sprite has its own pen
- A colour button that tells the pencil, with a message
- One variable that carries the colour
Learning outcome
By the end of this lesson you will be able to:
- Change the pen’s colour and size.
- Explain why a button cannot change the pencil’s pen by itself.
- Build a palette of colour buttons that all work the same way.
Learnt — colour, size, and whose pen 28 min
1 · Two blocks: a colour and a size
set pen color to [#D400AB]
set pen size to (8)
- Drag
set pen color to [#D400AB]into the code area. - Click its coloured box. Three sliders open: Color, Saturation and Brightness.
- Drag the Color slider to choose a colour.
- Or click the eyedropper at the bottom, then click any colour on the Stage to copy it.
How to use this: Click the green flag to start it. Nothing else to press — watch three lines appear.
2 · Paint a toolbar
The buttons need somewhere to live. Give the backdrop a strip across the top.
- Click the Stage, then the Backdrops tab.
- Pick the rectangle tool and a pale colour.
- Draw a strip across the very top, about 50 pixels deep. Its bottom edge is at y: 130.
3 · A colour button — and a surprise
Add the Ball sprite. Pick its pink costume, set its size to 60, and put it in the strip. Then give it this script:
when this sprite clicked
set pen color to [#D400AB]
How to use this: Click the green flag, then click the pink ball. Now hold the mouse button down on the stage and drag to draw.
4 · The ball tells the pencil
Only the pencil can change the pencil’s pen. So the ball sends a message, and the pencil does the work.
when this sprite clicked
broadcast [pink v]
when I receive [pink v]
set pen color to [#D400AB]
How to use this: Click the green flag, then click the pink ball. Now hold the mouse button down on the stage and drag to draw.
5 · Five buttons, one message
Five colours would need five messages and five scripts on the pencil. There is a shorter way. Each button puts its colour in a variable, then sends one message:
when this sprite clicked
set [colour v] to [#D400AB]
broadcast [new colour v]
when I receive [new colour v]
set pen color to (colour)
The colour code is a # and six letters or digits, like #D400AB. set pen color to (colour) understands it.
Basic — a palette 25 min
Open your sketch pad from last lesson.
Step 1 — the pencil’s new start
- Make a variable called
colour, for all sprites. - In the pencil’s green-flag script, after
erase all, addgo to [back v] layer. - Add
set [colour v] to [#4C4CFF]andset pen color to (colour). - Check the if says
<<mouse down?> and <(mouse y) < (130)>>.
Step 2 — the pencil listens
- Add
when I receive [new colour v]to the pencil. - Under it,
set pen color to (colour).
Step 3 — the first ball
- Add the Ball sprite. Size 60. Put it at the left of the strip.
- Give it
when this sprite clicked,set [colour v] to [#FF8000]andbroadcast [new colour v]. - Test it: click the ball, then draw. The line should be orange.
Step 4 — four more
- Right-click the ball and choose duplicate. Do it four times.
- Give each copy a different costume and drag it along the strip.
- Change each copy’s colour code: blue
#0077FF, pink#D400AB, green#00A010, purple#5A00CC.
when flag clicked
go to [back v] layer
erase all
set [colour v] to [#4C4CFF]
set pen color to (colour)
set pen size to (4)
forever
go to [mouse-pointer v]
if <<mouse down?> and <(mouse y) < (130)>> then
pen down
else
pen up
end
end
when I receive [new colour v]
set pen color to (colour)
How to use this: Click the green flag. Click a ball to pick a colour, then hold the mouse button down on the stage and drag to draw.
I click a ball and the pencil draws a line to it.
The (mouse y) < (130) test is missing, or your strip is deeper than 50 pixels. Check the strip’s bottom edge.
Clicking a ball does nothing.
The pencil is on top of the ball and catches the click. Add go to [back v] layer to the pencil.
Every ball gives the same colour.
You duplicated the first ball and did not change the code in its set [colour v] to [#FF8000] block.
The colour you are holding
You cannot see the pencil’s colour until you draw. Show it.
- A big ball sits at the right end of the strip.
- It changes to the colour of the ball you clicked.
- It starts blue, like the pencil.
How to use this: Click the green flag, then click any ball on the left. Watch the big ball on the right.
Teacher reveal
Each small ball also sets a held variable to its own number, 1 to 5. The big ball has all five costumes:
when I receive [new colour v]
switch costume to (held)
Three brushes
- Key 1 gives a thin brush, size 2.
- Key 2 gives a middle brush, size 8.
- Key 3 gives a fat brush, size 20.
- Changing the brush does not change the colour.
How to use this: Click the green flag, then click the stage once. Press 1, 2 or 3 to change the brush, then hold the mouse button down and drag to draw.
Teacher reveal
when [1 v] key pressed
set pen size to (2)
And the same for 2 and 3, all on the pencil — the pen size is the pencil’s too.
A brush that stops
Swap the three keys for two arrows, and show the size on the Stage.
- A variable called
brush, shown on the Stage. It starts at 4. - ↑ makes the brush 2 bigger. ↓ makes it 2 smaller.
- It never goes below 2 or above 30, however long you hold the key.
- The pen size always matches the number on the Stage.
How to use this: Click the green flag, then click the stage once. Hold ↑ or ↓ to change the brush, then hold the mouse button down and drag to draw.
Teacher reveal
when [up arrow v] key pressed
if <(brush) < (30)> then
change [brush v] by (2)
end
set pen size to (brush)
A common slip is to test after changing, which lets the brush reach 32 once. Test first, then change.
A surprise colour
Add a star to the end of the palette. It gives the pencil a colour nobody picked.
- Click the star and the pencil gets a random colour.
- Every click gives a different one.
- The five balls still work afterwards.
How to use this: Click the green flag, then click the star and draw. Click the star again and draw again.
Teacher reveal
when this sprite clicked
broadcast [surprise v]
when I receive [surprise v]
set pen (color v) to (pick random (0) to (100))
The first script is on the Star, the second on the Pencil — the same rule again.
Summary 5 min
set pen color to [#D400AB]andset pen size to (8)change the next line you draw.- Every sprite has its own pen. A button cannot change the pencil’s pen.
- The button sends a message. The pencil hears it and changes its own pen.
- A variable can carry the colour, so one message works for every button.
- Pen colour
- The colour of the next line a sprite draws.
- Colour code
- A colour written as # and six letters or digits.
- Swatch
- A small sample of a colour you click to choose it.
- Toolbar
- The strip of buttons along the top of an app.
Hand in
Save your sketch pad. Next lesson it gets tools: a rainbow brush, an eraser and a stamp.