Scratch 2: Control and Sensing

Version 1.11

Learning objectives

We continue our study of Scratch, learning about control. Control blocks let us make scripts which are more interesting than just sequences of actions.

We'll also look at things we can do with the Pen tools, and simple text input and output.

Control: Making Choices

Open the Control group of script blocks.

Example 1A

Try this:

A script for the cat to meow if it's close to the mouse

Example 1B

Let's make our sprite chase the (computer) mouse, if it's not close enough. This is almost the same script as before, only we are changing the if block to an if/else block and inserting a second action.

A script for the cat to meow or move closer to the mouse

Okay, now our sprite will do something, no matter what. Still, it does not seem to chase the mouse very hard. Let's make it more persistent.

More Control: Repetition

Example 1C

This is like Example 1B, except we are wrapping the if/else block in a forever control block.

A script for the cat to chase the mouse

Example 2

Use a beach ball sprite for this one.

A script for a bouncing ball

This is a bit boring, as the ball bounces back and forth along the same path all the time. Let's spice it up, by adding some randomness to the ball's direction. Add a second script for the same sprite (keep the first one there):

A script for some random variation of a bouncing ball

First, try out the second script all alone by clicking on the script. The effect is very subtle.

Now let's click on the green flag. With two scripts in the panel for the same sprite, both scripts will start running together. The first script manages the basic back-and-forth bouncing act. Only now, every 2.5 seconds, the second script will cause the sprite's direction to deviate a little.

Having two scripts run together is called concurrency. It's a powerful way of controlling behavior, but, in many cases, difficult to use because of complicated interactions between the scripts. (Our example is quite simple.)

Using the Pen

We can use our sprite to draw a picture using the "pen."

Open the Pen group:

Note, also, that in the Looks group we can control the visibility of our sprite with show and hide.

Try a few pen and motion commands to draw a line and turn a corner.

Example 3

Now, can we draw a rectangle?

This would do it:

A script to draw a square

But there's a much better way:

An improved script to draw a square

Okay, now let's get a little more rambunctious. Here's a little script that will, repeatedly, wait for a mouse click, draw a square at the mouse location, and then have the sprite retreat to a corner.

A script to draw squares repeatedly

Input and output

Our sprite can ask a question and read a typed-in response, and can "say" something based on the response.

Example 4

A script to greet by name

Summary

Previously, we learned about primitive actions, such as the motion commands, and sequences of actions.

Today, we've added to our control repertoire:

In addition, we've learned to make graphical output with the pen, and to use the ask and say blocks for text input and output.

Coming Up

Next time, we'll conclude our study of Scratch looking at variables, lists, and two more forms of repetition.


Gregory Weber


  1. Revision log:
    • Version 1.1, 2010 April 17. Added script snapshots.
    • Version 1.0, 2010 April 12. Added text input/output.
    • Version 0.9, 2010 April 10. Initial draft.