Click to Make Things Happen 👆¶
Drawings that do something when you click them
When a Shape Is Clicked 🎈¶
when ... is clicked says what to do each time someone clicks a shape. Click the balloon:
Canvas paper having width 400, length 300
draw circle balloon at 200, 150 having radius 40, fill "tomato"
when balloon is clicked {
grow balloon by 2 in 1 second
shrink balloon by 2 in 1 second
}
Everything inside the { } happens when the balloon is clicked, one line after another. Click it again, and it happens again!
Nothing Happens Until You Click 🖼️¶
The lines inside when ... is clicked aren't part of the picture. The picture is drawn first, and then it waits for you.
That's why the balloon above starts small and red. Its lines only run when somebody clicks it.
Changes Happen in Their Turn ⏱️¶
Inside when, a change without a time happens straight away, when its turn comes:
Canvas paper having width 400, length 300
draw square box at 150, 100 having size 100, fill "skyblue"
when box is clicked {
box.fill = "gold"
turn box by 90 in 1 second
box.fill = "skyblue"
}
The box turns gold, turns round, then goes back to blue. Outside when, a change without a time belongs to the picture, but inside it, every line waits for its turn.
Clicking Again 👆👆¶
Every click runs the lines inside when, even if the last click's lines are still going. Both go on at the same time.
Try clicking the box above twice, quickly. The second turn adds to the first, so the box turns twice as far. Changes with by add up. A change to a value, like box.fill = "gold", starts from wherever the shape has got to and ends on its value.
Stopping Things ✋¶
Give something that takes time a name with animation, like you name a shape. Then play starts it and stop stops it. Click the fan to switch it on, and click it again to switch it off:
Canvas paper having width 400, length 300
draw rectangle fan at 150, 140 having width 100, length 20, fill "skyblue"
animation spin {
forever { turn fan by 360 in 2 seconds }
}
when fan is clicked {
if spin is playing {
stop spin
}
otherwise {
play spin
}
}
animation spin { }makes an animation calledspin. Making it doesn't start it, just likecircle sunmakes a circle without drawing it.play spinstarts it.if spin is playing { }checks whether it's going. If it isn't, the lines inotherwise { }happen instead.stop spinstops it right where it is.
Without the check, each click would start another spin, and the newest would be the only one stop could reach!
play animation spin { } makes an animation and starts it in one go, just like draw circle sun makes a circle and draws it.
More Than One when 📋¶
A shape can have more than one when ... is clicked. A click runs them one after another, in the order you wrote them:
Canvas paper having width 400, length 300
draw circle ball at 100, 150 having radius 30, fill "purple"
when ball is clicked {
move ball by 200, 0 in 1 second
}
when ball is clicked {
move ball by -200, 0 in 1 second
}
The ball goes across, then comes back.
Two different shapes don't wait for each other either: clicking one while the other is moving starts it straight away.
A Button That Switches a Light On 💡¶
A shape you make but don't draw is hidden. show makes it appear, so a click can bring something in:
Canvas paper having width 400, length 300
draw rectangle button at 30, 230 having width 100, length 40, fill "green" {
text at 18, 26 having content "Click me", size 16, color "white"
}
circle lamp at 250, 120 having radius 50, fill "gold"
when button is clicked {
show lamp in 1 second
wait 1 second
hide lamp in 1 second
}
The words are a part of the button, so clicking them clicks the button too. A hidden shape can't be clicked: you can't click what nobody can see!
Clicking Shapes With Parts 🙂¶
Clicking any part of a shape clicks the whole shape. A part can have its own when too, using its full name:
Canvas paper having width 400, length 300
draw circle face at 200, 150 having radius 80, fill "gold" {
circle eye1 at -30, -20 having radius 10, fill "black"
circle eye2 at 30, -20 having radius 10, fill "black"
}
when face is clicked {
turn face by 360 in 2 seconds
}
when face.eye1 is clicked {
face.eye1.fill = "blue"
}
Click the face to make it spin. Click the left eye and it turns blue, and the face spins too, because the eye is part of the face.
Clicking a Recipe 🍪¶
A shape made from a recipe changes through its settings, in a click too. Click the face to make it bigger:
Canvas paper having width 400, length 300
shape face having size 60, hair "short" {
circle head having radius size / 2, fill "gold"
circle eye1 at -size / 6, -size / 10 having radius size / 15, fill "black"
circle eye2 at size / 6, -size / 10 having radius size / 15, fill "black"
when hair is "short" {
rectangle fringe at -size / 3, -size / 2 having width size * 2 / 3, length size / 8, fill "brown"
}
when hair is "curly" {
repeat 5 times {
circle at -size / 3 + size / 6 * (count - 1), -size / 2 having radius size / 10, fill "brown"
}
}
}
draw face sam at 200, 150
draw circle button at 40, 40 having radius 20, fill "tomato"
when sam is clicked {
sam.size = sam.size + 20
}
when button is clicked {
sam.hair = "curly"
}
Each click on the face makes it 20 bigger, and the recipe makes it again: eyes, hair and all. Click the red button and the face gets curly hair, because the recipe's when hair is "curly" part is chosen now.
A when for Each Shape 🎈🎈🎈¶
Put when inside repeat, and every shape the loop draws gets its own:
Canvas paper having width 400, length 300
repeat 3 times {
draw circle balloon at 100 * count, 200 having radius 30, fill "tomato"
when balloon is clicked {
move balloon by 0, -50 * count in 1 second
}
}
Each balloon floats up when you click it, and each one goes a different height, because count is 1, 2 or 3 for that balloon.
A New Shape With Every Click 🌷¶
A draw inside when draws a new shape each time you click. Click the grass again and again to plant a row of flowers:
Canvas paper having width 400, length 300
draw rectangle grass at 0, 220 having width 400, length 80, fill "green"
flowers = 0
when grass is clicked {
flowers = flowers + 1
draw circle flower at 40 * flowers, 200 having radius 12, fill "pink" {
circle at 0, 0 having radius 4, fill "gold"
}
grow flower by 1.5 in 0.5 seconds
}
flowers = flowers + 1 counts the clicks, so each flower goes a little further along. (There's more about counting like this in the next lesson.)
Each click makes its own flower. The name means the flower that click drew, so grow flower grows the new one and leaves the others as they are.
Its parts can use the click's numbers too. Here each new flower has one more petal than the last:
Canvas paper having width 400, length 300
draw rectangle grass at 0, 220 having width 400, length 80, fill "green"
flowers = 0
when grass is clicked {
flowers = flowers + 1
draw circle flower at 60 * flowers, 150 having radius 8, fill "gold" {
repeat flowers + 2 times {
circle at 18 * count - 9 * (flowers + 3), -25 having radius 7, fill "pink"
}
}
}
A shape you made outside the when can't be drawn inside it, because a draw there makes a new shape every time. Use show for that instead, as with the lamp above.
A Click That Works Only Sometimes 🔓¶
Here the lamp only works once the button has been clicked. The button sets a variable, and the lamp checks it with if:
Canvas paper having width 400, length 300
draw rectangle button at 30, 230 having width 100, length 40, fill "gray"
draw circle lamp at 250, 120 having radius 50, fill "lightgray"
ready = false
when button is clicked {
button.fill = "green"
ready = true
}
when lamp is clicked {
if ready {
lamp.fill = "gold" in 0.5 seconds
lamp.fill = "lightgray" in 0.5 seconds
}
}
Click the lamp first: nothing happens. Click the button, and now the lamp lights up when you click it. You'll learn more about variables and if in the next lesson.
Watch Out: Where when Goes 👀¶
when ... is clicked can go below a shape, using the part's full name (when face.eye1 is clicked), or inside the shape's { }, using the part's own name (when eye1 is clicked). It can't go inside another when.
Challenge Time! 🏆¶
- 🌟 Challenge 1: Make a traffic light where clicking it goes red, then amber, then green.
- 🌟 Challenge 2: Make a ball that bounces up and down once each time it's clicked.
- 🌟 Challenge 3: Make a button that makes the sun come up, and another that makes it go down.
- 🌟 Challenge 4: Make a face whose eyes each turn a different colour when you click them.
- 🌟 Challenge 5: Make a ball that starts bouncing when you click it, and stops when you click it again.
What's Next? 🚀¶
Your drawings can now listen to clicks. Next, they learn to remember things between clicks: Remembering Things. ✨