Scratch 3: Variables, Lists, and More Control

Version 1.21

Objectives

We'll learn how to make use of variables, lists, two more forms of repetition, and using broadcast messages for control.

Variables

More Control

A new kind of loop:

Example 1

Spelling — uses a variable to walk through a string and to count.

  1. Create a variable named position

  2. Build the script:

    A script for spelling

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.

Example 2A

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!

  1. For this script, we need two variables:
    • say what stores the sentence to be repeated
    • say how many stores the number of repetitions required
  2. First version of script, using a repeat block:

    A script for punishment

Try it!

Example 2B

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.

  1. Create a third variable:
    • count keeps track of how many repetitions she's done so far.
  2. Our strategy now is:
    1. Set count = 1
    2. repeat until count > say how many
      1. say count, say what
      2. add 1 to count

    Here's the revised script:

    An improved script for punishment

Lists

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.

Example 3A

  1. Create a list named notes and a variable named current index.
  2. 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.)

  3. Create two variables: current index and current note
  4. Drag out the following Sound blocks and test them individually:
    • play drum ...
    • play note ...
    • set instrument ...
  5. Write this script:

    A script for playing a musical tune

  6. Execute the script.

Example 3B

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)

  1. Make a list named durations and a variable current index.
  2. Put some numbers in 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.).
  3. Build this script:

    A script for playing a drum beat

  4. Test it. If it works, then let's repeat it 10 times as follows:

    A script for playing a drum beat repeatedly

Combining Melody and Rhythm

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.

Something Beautiful

Let's draw some flowers.

Example 4A

A script for drawing a rose, version 1

The sprite glides from a corner to the center of the stage, then disappears and lowers its pen.

The 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.

Example 4B

  1. Create three variables:
    • number of petals
    • petal length
    • petal width Check the boxes so the variables are displayed on stage.
  2. Modify the script as follows:

    A script for drawing a rose, version 2

  3. Now we can get quite a variety of designs with various parameter (i.e., variable) settings. Try it! For example:
    • number of petals = 21, petal width = 21, petal length = 84
    • number of petals = 21, petal width = 48, petal length = 84
    • number of petals = 21, petal width = 10, petal length = 110
    • number of petals = 65, petal width = 50, petal length = 61
    • number of petals = 112, petal width = 31, petal length = 160

Messages

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.

Example 5A

  1. Make a button:

    • Make a new sprite
    • Give it a background shape such as a rectangle, filled in. (Without a background behind the text, it will be hard to click on the button; we'll have to hit one of the letters precisely.)
    • Use the text tool to "draw" the word "Change".
  2. Make this script for the button sprite:

    A script for the change button

  3. Add two other sprites.

  4. 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.

Example 5B

  1. Add the following script for the second sprite:

    A script for a change receiver

  2. Leave the third sprite with no script.
  3. 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.

Example 5C

  1. Add a fourth sprite.
  2. Drag the script of the second sprite (the one that receives) to the fourth sprite. This is a bit tricky, so verify that the fourth sprite now has a copy of the second's script.
  3. Click the button.

Two of the sprites now respond to the broadcast. You can control any number of sprites by broadcasting messages.

Example 5D

  1. Change the script of the fourth sprite, so that instead of changing color, it changes its size:

    A script for a second change receiver

  2. Click on the button.

Now the fourth sprite responds, but in a different way. The point is:

Summary


Gregory Weber


  1. Revision log:
    • Version 1.2, 2010 April 17. More script snapshots added.
    • Version 1.1, 2010 April 14. Added some script snapshots. Didn't have time to do them all or to place a link to the script texts.
    • Version 1, 2010 April 14. Completed all examples.
    • Version 0.2, 2010 April 12. Draft. Moved input/output to Scratch II.
    • Version 0.1, 2010 April 10. Initial draft.