Making Things Move π¬¶
Shapes that move, turn, grow and change colour while you watch
Moving Slowly π ¶
Add in and a number of seconds, and a change happens slowly instead of all at once. Press βΆ Play to watch:
Canvas paper having width 400, length 300
draw rectangle sky at 0, 0 having width 400, length 300, fill "skyblue"
draw circle sun at 200, 260 having radius 40, fill "gold"
draw oval hill at 200, 300 having width 500, length 160, fill "green"
move sun to 200, 80 in 3 seconds
move sun to 200, 80 says where the sun goes. in 3 seconds says how long it takes to get there. The sun was drawn before the hill, so it rises from behind it.
The Picture, Then the Animation πΌοΈ¶
Your code runs from the top, one line after another, and each line waits for the one before it to finish:
- The picture is everything before the first line with a time. It is drawn first, all at once.
- The animation starts with the first line with a time. From there, every line happens when its turn comes.
So the sun above starts where it was drawn, at 200, 260, and the animation moves it from there.
Lines with a time happen in the order you write them, each one waiting for the one before to finish:
Canvas paper having width 400, length 300
draw rectangle sky at 0, 0 having width 400, length 300, fill "skyblue"
draw circle sun at 200, 260 having radius 40, fill "gold"
draw oval hill at 200, 300 having width 500, length 160, fill "green"
move sun to 200, 80 in 3 seconds
move sun to 200, 260 in 3 seconds
sky.fill = "navy" in 4 seconds
The sun goes up, then down, and only then does the sky get dark.
Ways to Move βοΈ¶
| Line | What happens |
|---|---|
move ball to 300, 100 in 2 seconds |
goes to that place |
move ball by 50, 0 in 1 second |
goes 50 across and 0 down from where it is |
turn box to 90 in 1 second |
turns until its angle is 90 |
turn box by 360 in 2 seconds |
turns all the way round once more |
grow balloon to 2 in 3 seconds |
grows until it is twice as big as it was drawn |
grow balloon by 2 in 3 seconds |
gets twice as big as it is now |
shrink balloon by 2 in 1 second |
gets half as big as it is now |
Canvas paper having width 400, length 300
draw square box at 60, 110 having size 80, fill "gold"
draw circle balloon at 300, 150 having radius 30, fill "red"
turn box by 360 in 2 seconds
grow balloon by 2 in 2 seconds
shrink balloon by 2 in 1 second
Without in, a line like move ball by 50, 0 happens at once: in the picture if it comes before the first line with a time, or the moment its turn comes after it.
Changing Anything Slowly π¨¶
Any change with = can have a time too. Colours mix gradually, and words fade out and back in:
Canvas paper having width 400, length 200
draw circle ball at 100, 100 having radius 40, fill "yellow"
draw text label at 200, 110 having content "Hello", size 32
ball.fill = "purple" in 3 seconds
ball.radius = 70 in 2 seconds
label.content = "Goodbye" in 2 seconds
Appearing Slowly ⨶
Put a time at the end of a draw, and the shape fades in instead of being there from the start:
Canvas paper having width 400, length 300
draw rectangle sky at 0, 0 having width 400, length 300, fill "midnightblue"
draw circle moon at 300, 80 having radius 40, fill "lightyellow" in 2 seconds
draw circle at 80, 60 having radius 4, fill "white" in 1 second
draw circle at 160, 120 having radius 4, fill "white" in 1 second
draw circle at 60, 200 having radius 4, fill "white" in 1 second
The shape is part of the picture from the start, just invisible, so other lines can use it straight away, like moon.center.
Showing and Hiding π¶
hide makes a shape invisible, and show brings it back. With a time, it fades:
Canvas paper having width 400, length 300
draw rectangle sky at 0, 0 having width 400, length 300, fill "skyblue"
draw circle sun at 120, 100 having radius 40, fill "gold"
circle moon at 280, 100 having radius 30, fill "lightyellow"
hide sun in 2 seconds
sky.fill = "midnightblue" in 2 seconds
show moon in 2 seconds
The moon is made but never drawn, so it isn't in the picture. show moon puts it on the drawing. After that it has been drawn, so draw moon would be drawing it twice: use show and hide from then on.
Showing a shape that is already showing, or hiding one that is already hidden, does nothing. That makes blinking easy:
Canvas paper having width 400, length 200
draw circle light at 200, 100 having radius 40, fill "red"
forever {
hide light
wait 0.5 seconds
show light
wait 0.5 seconds
}
Waiting β³¶
wait pauses before the next line:
Canvas paper having width 400, length 200
draw circle frog at 60, 150 having radius 25, fill "green"
move frog by 120, 0 in 1 second
wait 2 seconds
move frog by 120, 0 in 1 second
Repeating with Time π¶
A repeat with time inside it plays each time round, one after another. count works just as before:
Canvas paper having width 400, length 300
draw rectangle at 0, 250 having width 400, length 50, fill "forestgreen"
draw circle ball at 40, 230 having radius 20, fill "tomato"
repeat 4 times {
move ball by 40, -30 * count in 0.5 seconds
move ball by 40, 30 * count in 0.5 seconds
}
Each hop is higher than the one before, because count goes 1, 2, 3, 4.
At the Same Time π€Ή¶
Lines with a time happen one after another. To make things happen at the same time, put them in together { }:
Canvas paper having width 400, length 300
draw circle ball at 50, 150 having radius 20, fill "tomato"
draw square box at 300, 50 having size 50, fill "gold"
together {
move ball by 250, 0 in 2 seconds
turn box by 180 in 1 second
ball.fill = "purple" in 2 seconds
}
move ball by 0, 100 in 1 second
Every line inside starts at the same moment. together ends when the slowest one finishes, and then the next line carries on. A repeat or forever inside together is one thing: it runs its own lines one after another, while the others run alongside it. A forever never finishes, so together doesn't wait for it: it keeps going on its own.
Forever βΎοΈ¶
forever repeats and never stops:
Canvas paper having width 400, length 300
draw rectangle at 192, 150 having width 16, length 150, fill "sienna"
draw circle hub at 200, 150 having radius 12, fill "brown" {
rectangle at -6, -90 having width 12, length 180, fill "white"
rectangle at -6, -90 having width 12, length 180, fill "white", angle 90
circle at 0, 0 having radius 8, fill "brown"
}
forever {
turn hub by 360 in 4 seconds
}
The blades are parts of the hub, so the whole windmill turns together.
To spin the windmill while something else happens, start the forever first, or put both in together:
Canvas paper having width 400, length 300
draw circle sun at 60, 260 having radius 30, fill "gold"
draw circle hub at 250, 150 having radius 10, fill "brown" {
rectangle at -5, -70 having width 10, length 140, fill "white"
rectangle at -5, -70 having width 10, length 140, fill "white", angle 90
}
together {
move sun to 60, 60 in 3 seconds
forever {
turn hub by 360 in 4 seconds
}
}
A traffic light changes again and again:
Canvas paper having width 400, length 300
draw rectangle at 170, 40 having width 60, length 170, fill "black"
draw circle red at 200, 75 having radius 22, fill "darkred"
draw circle amber at 200, 125 having radius 22, fill "saddlebrown"
draw circle green at 200, 175 having radius 22, fill "darkgreen"
forever {
red.fill = "red"
wait 2 seconds
red.fill = "darkred"
green.fill = "lime"
wait 2 seconds
green.fill = "darkgreen"
amber.fill = "orange"
wait 1 second
amber.fill = "saddlebrown"
}
Inside forever, changes without a time happen at their moment, in order.
forever never ends, so the lines after it don't wait for it. It keeps going on its own, and the next line starts straight away. Here the ball keeps turning while it moves:
draw circle ball at 50, 50 having radius 10
forever {
turn ball by 10 in 1 second
}
move ball by 100, 0 in 1 second
Changes Without a Time β±οΈ¶
A change without a time waits for its turn like any other line:
Canvas paper having width 400, length 300
draw circle sun at 200, 260 having radius 40, fill "gold"
move sun to 200, 80 in 2 seconds
sun.fill = "orange"
move sun to 200, 260 in 2 seconds
The sun rises, turns orange at the top, and then sets. To make the colour change slowly instead of all at once, give it a time too: sun.fill = "orange" in 0.5 seconds.
Following Another Shape πͺ¶
kite.center is where the kite is right now. A shape drawn there stays there, even when the kite flies off. Put @ in front, and it follows the kite instead:
Canvas paper having width 400, length 300
draw circle kite at 300, 60 having radius 15, fill "tomato"
draw line string from 60, 280 to @kite.center
forever {
move kite by -150, 30 in 2 seconds
move kite by 150, -30 in 2 seconds
}
@ means "follow". Wherever the kite goes, the end of the string goes too.
You can add to what you follow. This shadow stays below the bee, 10 to the right:
Canvas paper having width 400, length 300
draw circle bee at 50, 100 having radius 12, fill "gold"
draw oval shadow at @bee.x + 10, 260 having width 20, length 6, fill "gray"
forever {
move bee by 300, 0 in 3 seconds
move bee by -300, 0 in 3 seconds
}
Only bee.x is followed. The + 10 and the 260 stay as they are.
Any property can be followed, not just places: fill @sun.fill keeps a shape the same colour as the sun, even while the sun changes colour.
Giving a shape's place a value of its own, like move shadow by 0, 10 or shadow.at = 100, 100, stops it following. shadow.at = @cloud.center makes it follow the cloud instead.
Recipes That Change πͺ¶
A shape made from a recipe changes through its settings, and a setting can change slowly too. Watch this face grow and blush:
Canvas paper having width 400, length 300
shape face having size 60, skin "gold" {
circle head having radius size / 2, fill skin
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"
}
draw face sam at 200, 150
sam.size = 160 in 2 seconds
sam.skin = "pink" in 2 seconds
The recipe makes the face again as size grows, so the eyes grow and move with the head. A number setting changes smoothly and a colour slowly mixes into the new one. Words that aren't colours, like hair "curly", change when the time is up.
A setting can follow, too. This face is always twice as big as the sun:
Canvas paper having width 400, length 300
draw circle sun at 60, 60 having radius 20, fill "gold"
shape face having size 60 {
circle head having radius size / 2, fill "pink"
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"
}
draw face sam at 250, 170 having size @sun.radius * 4
forever {
sun.radius = 40 in 2 seconds
sun.radius = 20 in 2 seconds
}
size @sun.radius * 4 follows the sun's radius, so the face grows and shrinks with the sun.
Challenge Time! π¶
- π Challenge 1: Make a balloon that grows, then floats up off the top of the drawing.
- π Challenge 2: Make a day that turns into night: the sun sets, the sky gets dark, then the moon rises.
- π Challenge 3: Make a fan with three blades that spins for ever.
- π Challenge 4: Make a word that changes three times, waiting a second between each change.
What's Next? π¶
You can now make drawings that move: moving, turning, growing and changing slowly with in seconds, showing and hiding, waiting, doing things at the same time, repeating, going on for ever, and following another shape with @.
Next, make your drawings listen: in Click to Make Things Happen, shapes do things when you click them. π