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.
Open the Control group of script blocks.
when (green flag) clicked is used to start a script by clicking on the green flag in the upper right corner of the Scratch window.
wait block makes the script pause (so we can get ourselves into position)
if block lets the script perform an action provided a condition is true. The "action" can actually be a sequence of actions. If the condition is not true, it will do nothing.
if/else block lets the script choose between two actions. If a condition is true, it will perform the first action; otherwise, the second.
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.
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.
repeat block lets us repeat an action a set number of times. (Say "hello" 4 times")
forever block makes the script repeat an action forever. Okay, really, not forever, but until you make the script quit — for example, by clicking on the red stop sign beside the green flag. Or you could turn your computer off.
These kinds of control block are called loops because, after executing an action, they go back and do it again.
This is like Example 1B, except we are wrapping the
if/else block in a
forever control block.
Use a beach ball sprite for this one.
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):
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.)
We can use our sprite to draw a picture using the "pen."
Open the Pen group:
clearerases the picture
pen downlowers the pen, so that moving it will draw a line
pen upraises the pen, so that we can move without drawing
stampcauses the pen to leave an imprint of the sprite at the current location.
Note, also, that in the
Looks group we can control the visibility of our sprite with
Try a few pen and motion commands to draw a line and turn a corner.
Now, can we draw a rectangle?
This would do it:
But there's a much better way:
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.
Our sprite can ask a question and read a typed-in response, and can "say" something based on the response.
answeris something associated with the
answercan also be dragged into a suitable hole in a block of a script.
joinputs two strings (sequences of characters) together.
Previously, we learned about primitive actions, such as the motion commands, and sequences of actions.
Today, we've added to our control repertoire:
forever, or for a fixed number of times (
In addition, we've learned to make graphical output with the pen, and to use the
say blocks for text input and output.
Next time, we'll conclude our study of Scratch looking at variables, lists, and two more forms of repetition.