GoalRemember how forever and repeat loops, comments, costumes and coordinates work in an animation.
New words
loop · a set of instructions that runs again and againcomment · a note in a program that the computer does not runcoordinates · the x and y numbers that give a place on the stage
Explanation
A loop saves us from writing the same blocks again and again. A forever block repeats its blocks until someone stops the program, and nothing can be attached under it. A repeat block runs its blocks a set number of times and then the script carries on. When an algorithm says “repeat these steps three more times”, the repeat block gets the number 4, because the steps ran once already. Costumes let a sprite change its picture, and the stage uses x and y numbers to say where a sprite is. A comment is only a note for people. It never changes what the sprite does.
forever
repeat (n)
How many times?
never stops by itself
exactly n times
Blocks underneath?
no room for any
yes, they run afterwards
Good for
a scene that runs on and on
a job with a set count
Worked examples
A dolphin sprite starts at x: −100, y: −40. The script runs repeat (3) with “change x by (50)”, “change y by (20)” and “wait (0.5) seconds”. Each trip moves it 50 right and 20 up, so it passes (−50, −20) and (0, 0) and ends at (50, 20) after 1.5 seconds.
An algorithm says: 1 Start. 2 Turn 30 degrees. 3 Wait 1 second. 4 Repeat steps 2 and 3 five more times. 5 Stop. The steps run 1 + 5 = 6 times, so the script has repeat (6). The sprite turns 6 × 30 = 180 degrees, so it ends facing the opposite way and the script takes 6 seconds.
Block
Comment says
Sprite really
move (20) steps
move 5 steps
moves 20 steps
wait (3) seconds
short pause
waits 3 seconds
repeat (2)
run six times
runs two times
A comment is a note, not a command. Look at what each block really does, not at what the comment says.
Class activity
Animation flip book (with family): fold a small stack of paper and draw a ball that is a little higher on every page, five pages in all. Flip through the pages. Which page of your book is the “next costume”? Now write a script for it on paper that uses a repeat block and a wait. If you go on flipping forever, which block is that? Write one comment on your script and say who it helps.
Practice
1
A sprite starts at x: 0, y: 0 and runs the script. Where is it at the end?
At x: −80, y: 40 (4 × −20 and 4 × 10).
2
An algorithm says “Repeat steps 2 and 3 seven more times”. Which number goes in the repeat block?
8 (1 + 7).
3
A comment says “move 50 steps” but the block says “move (5) steps”. How far does the sprite move? What should you fix?
It moves 5 steps. Fix the comment, or the block if 50 is what you want.
4
A sprite has 3 costumes and starts on costume 2. Which costume does it wear after “next costume” has run 5 times?