Scratch applications are made up of sprites that interact with one another and the user. Sprites are controlled and animated by scripts. Sprites can have any number of scripts, each of which is designed to perform a specific task or action. Scripts are made up of one of more Scratch code blocks. In total, there are more than 100 different Scratch blocks, each of which is designed to fulfill a specific purpose. These blocks can be broadly classified into three categories, as outlined here:
Stack blocks
Hat blocks
Reporter blocks
The majority of code blocks provided by Scratch are stack blocks. Stack blocks are code blocks with a notch at the top or a bump at the bottom. The notches and bumps serve as visual indicators that identify how the blocks can be snapped together to create programming logic. Figure 3.2 shows an example of a typical stack block.
The notch on the top indicates that the code block can be attached to the underside of another code block. The bump at the bottom of the code block allows other code blocks to attach to its underside. Figure 3.3 shows an example of another stack block. This block will repeatedly execute any code blocks that you choose to embed inside it for as long as a tested condition evaluates as true.
Note
You will learn about the application of repetitive and conditional programming logic in Chapter 9.
Some stack blocks include an input area inside them that allows you to specify a value by typing in a number. For example, the stack block shown in Figure 3.4 lets you assign the color to be used when drawing by inserting a color-associated numeric value.
To modify the value assigned to a block like the one shown in Figure 3.4, click on the white area within the code block and type in a new value. Some code blocks let you configure them by selecting a value from a pull-down list, as demonstrated in Figure 3.5.
A hat block is a code block with a rounded or curved top and a bump at the bottom, visually indicating that it can be snapped on top of other stack blocks. Hat blocks provide the ability to create event-driven scripts. An event-driven script is one that automatically executes when a specified event occurs. An example of an event that can automatically trigger script execution is when the user clicks on the green flag button. When this event occurs, any scripts that begin with the hat block shown in Figure 3.6 are automatically executed.
Script execution can also be triggered when the user clicks on a sprite. This can be set up by adding the code block shown in Figure 3.7 to the beginning of the script.
Note
Every sprite in an application can potentially have its own scripts. You can automate the execution of any or all of the scripts using hat blocks. In addition to sprites, the stage can also have scripts.
A third type of Scratch code block is a reporter block. A reporter block is a code block that has either rounded or angled sides and is specifically designed as a mechanism for providing input for other code blocks to process. For example, the code block shown in Figure 3.8 is a typical reporter block.
As you can see, this reporter block has rounded sides. As such, it can only fit into code blocks like the one shown in Figure 3.9, whose input area displays a shape with rounded sides.
Figure 3.10 shows an example of a reporter block that has angled sides. This particular code block returns a value of true if the user has pressed the spacebar or a false if the spacebar has not been pressed. Because it has angled sides, it can only be embedded inside code blocks that contain an input area whose sides are also angled.
Note
Boolean is a term used to represent data that has one of two values, either true or false.
To take advantage of a reporter block like the one shown in Figure 3.10, you need to embed the reporter block into another code block that has been designed to work with it. For example, Figure 3.11 shows one such code block.
Figure 3.12 demonstrates how a reporter blocks looks after being embedded within another code block.
18.118.2.15