Using Scratch to explore turtle geometry

Making Procedures in Scratch 1.4

Scratch 1.4 does not support procedures or methods. However, the use of the broadcast message in Listing 3 provides a workaround. One reason to use a procedure is to consolidate commonly used code into a single instance, which is exactly what the broadcast allowed me to do with the script.

The current script runs the draw octagon broadcast four times. Without the broadcast message, you would have to duplicate the blocks for the octagon pattern four times. Repeating the entire pattern six times would be a lot of extra blocks and would create an unwieldy script. As it stands now, you can happily repeat the pattern 2, 4, 10, or more times with only one code edit, which avoids a lot of duplication.

The broadcast message approach to procedures does have some limitations. Broadcasts can't accept inputs and the when I receive **() stack has to finish before it can be called again, which is why the script in Listing 3 requires the broadcast () and wait block.

Adding Slider Controls to Create a User Interface

When you create a variable, Scratch enables a stage monitor that displays the variable's value on the screen. If you look in the Data palette, you'll see the oval reporter blocks with check boxes next to them. If the block is checked, the stage monitor displays.

For the next step in the exercise, you can show the pattern # stage monitor by checking the box next to the block name. You can uncheck the size block if you still have it displayed.

Right-click on pattern **# stage monitor and choose slider from the menu options. This will change the view of the stage monitor and will include a slider, as seen in Figure 3. This simple user interface allows you do some experimenting without editing your scripts directly. Use the slider to set the pattern # and press the o key to draw it.

Figure 3: A slider interface to change the pattern # variable. The pattern in the figure is created by changing pattern # and two other values. Can you find two other values.

You can control the values that can be entered via the slider control by right-clicking on the slider and choosing the set min max option. In the dialog box that appears, enter the minimum and maximum values that you want someone to be able to enter via the slider. I chose 1 and 50 for my min and max values.

You will need to remove the set (pattern **#) to (4) block in the initialization part of the when (o) key pressed stack. Otherwise, it will undo the value you set with the slider.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content