We'll learn how to make use of variables, lists, two more forms of repetition, and using broadcast messages for control.
answeris in fact a variable: a named location for storing different values.
answervariable is built in and associated with the
set (variable) to (value)
change (variable) by (amount)
A new kind of loop:
repeat until block lets us repeat an action until a condition becomes true. We can think of using this to achieve a goal: make the condition true. In fact, after the block finishes running (if it finishes), it is guaranteed that the condition is true. Of course, the action has to make some progress towards the goal. Otherwise, the
repeat until block will not finish.
Variables are typically used with this kind of block, to measure progress towards the goal.
Spelling — uses a variable to walk through a string and to count.
Create a variable named position
Build the script:
The goal of the loop is for the sprite to spell the word — that is, to say the letters of the word, from first to last. The variable
position stores the current position in the string, starting at 1 (the first letter), and changes it by adding 1 (next letter) after saying each letter.
For example, to spell "dog":
Note that after saying the last letter, at position 3, the script still adds 1 to
position, making 4, and that's what ends the loop.
Did you ever do something naughty, and a parent or teacher made your write "I will not do X" 100 times, or 5000 times, for example? Let Scratch do the work for you!
say whatstores the sentence to be repeated
say how manystores the number of repetitions required
First version of script, using a
How do we know that she's said it the required number of times? We'd have to count along as she does it, and that's no fun. Let's make the sprite do the counting for us.
countkeeps track of how many repetitions she's done so far.
Here's the revised script:
A list is a sequence of data, just as a simple script is a sequence of actions.
One thing we can do with a list is have our script perform an action on each item, or element, in the list.
For example, let's make a list of notes and play a tune by playing each of the notes in succession.
notesand a variable named
Click on the + sign in the notes display to add the following notes: 60, 62, 64, 65, 64, 62, 64, 62, 60.
We are using a MIDI player to make the sounds, so we use MIDI note numbers: 60 = middle C. (We're not using frequencies. The frequency of middle C is about 262.)
Write this script:
Execute the script.
But all of the notes have the same length. What about rhythm? Let's make a little drum piece. (You could start a new project here)
durationsand a variable
durations, for example, 1/2 1/4 1/4 1/2 1/4 1/4 1/2 1/2 1/2 1 (but enter the numbers as 0.5, 0.25, etc.).
Build this script:
Test it. If it works, then let's repeat it 10 times as follows:
We had melody (pitches) without rhythm at first, and now we have rhythm without melody. But how could we combine them, so we have notes with both pitch and rhythm? I won't go into the details, but just suggest the strategy: use two lists; store note pitches in one and note durations in the other; use the same index variable to walk through both lists at the same time.
Let's draw some flowers.
The sprite glides from a corner to the center of the stage, then disappears and lowers its pen.
repeat block draws 15 petals. For each petal, it uses 9 statements to draw a rectangle, starting at the center of the flower. The 10th statement in the
repeat block turns to put the sprite in position for the next petal.
Then the sprite reappears, raises its pen, and glides back to the corner.
Discuss how we'd need to change the script in order to ...
It's fairly awkward, isn't it? We can do better by adding some variables with visible controls.
Modify the script as follows:
A sprite can control another sprite by broadcasting a message that is received by the other sprite.
We're used to graphical user interfaces with menus, buttons, etc. Let's make a button that will control some other sprites by broadcasting a message.
Make a button:
Make this script for the button sprite:
Add two other sprites.
Click the button.
What happens? Not much! When you click on the button sprite, it sends a broadcast message that can be received by all the sprites (including itself). However, none of the sprites are set up to receive it yet.
Add the following script for the second sprite:
Click the button again and watch the second sprite change.
The second sprite responds, because it is scripted to do so. The third one does not.
Two of the sprites now respond to the broadcast. You can control any number of sprites by broadcasting messages.
Change the script of the fourth sprite, so that instead of changing color, it changes its size:
Click on the button.
Now the fourth sprite responds, but in a different way. The point is: