How to Control a Script in Scratch on the Raspberry Pi

By Richard Wentk

When you’re experimenting with Scratch on your Raspberry Pi, sometimes you want a script to do something over and over. You can make a script do the same thing over and over by clicking it over and over. That works fine for a few repeats, but what if you want to repeat something hundreds of times?

You could use the duplicate right-click tool to make lots and lots of copies of your simple script and clip them together to make one big script.

That works for maybe ten repeats, but it’s a boring way to make a script do something hundreds of times.

Scratch has a better way. Click the Control button at the top of the block library. It has an orange edge. When you click, it you see a new set of blocks.

These are control blocks. They make your scripts smarter.

You can use control blocks do things like

  • Repeat some blocks forever.

  • Repeat some blocks as many times as you want, then carry on.

  • Start a script when you press a key.

  • Make a script wait for a while.

  • Make a script wait until something happens.

  • Repeat a script until something happens.

  • Check and test numbers, sprite positions, and other things.

  • Stop a script.

  • Stop all scripts.

How to use control blocks in Scratch

Control blocks go in three places:

  • At the start of a script

  • At the end of a script

  • Around other blocks

Start control blocks have a round top. You can’t clip a block on top of them. They have to go first because they wait for something to happen. The script can’t start until it happens!

For example, the when [space] key pressed block starts a script when you press the space key. You can pick a different key using the menu in the block.

End control blocks have a flat bottom. You can’t clip a block under them. They have to go at the end of a script because they tell the script to stop.

Around scripts have a space inside them. They look a bit like fat hairclips. To use them, drag them around the script you want to control.

You may have to split the script to pull out the blocks you want to control first and then glue it back together after you added the around block.

You can try out the repeat block. Drag it from the block list to the script area and clip it around the two blocks there already. The bottom end of the clip stretches to fit around the blocks.

The following figure shows the result. Click the block, and the sprite moves and turns. It repeats ten times unless you change the number by clicking it and typing in a new number.

image0.jpg

If it doesn’t turn, check which rotation button is lit.

How to stop scripts in Scratch

Click the number in the repeat block and type 100 and press Enter. Click the script again.

Now the sprite moves and turns over and over. It keeps moving for a long time.

Did you get bored? If you want to stop a script early, you can click the red button above the stage. The green flag next to it lights up when a script is running.

You can also click a script to stop it. While it’s running you can see a white border around it. When you stop it, the border disappears.