Scratch provides access to over 100 code blocks. These code blocks are organized into eight categories and are made available on the blocks palette. Each of these categories of code blocks is described in the following list:
Motion. Code blocks that control sprite placement, direction, rotation, and movement.
Looks. Code blocks that affect sprite and background appearance and provide the ability to display text.
Sound. Code blocks that control the playback and volume of musical notes and audio files.
Pen. Code blocks that can be used to draw using different colors and pen sizes.
Control. Code blocks that trigger script execution based on predefined events, repeatedly execute programming logic using loops, and perform conditional logic.
Sensing. Code blocks that can be used to determine the location of the mouse-pointer, its distance from other sprites, and whether a sprite is touching another sprite.
Numbers. Code blocks that perform logical comparisons, rounding, and other arithmetic operations.
Variables. Code blocks that can be used to store data used by applications when they execute.
You can view the code blocks belonging to a given category by clicking on one of the eight labeled button controls at the top of the blocks palette. Note that each category of code block is color coded, making it easy to distinguish between code blocks from different categories.
Each of these categories of code blocks is reviewed in the sections that follow. This review covers Scratch’s entire collection of code blocks, indicating which ones support monitors and providing a brief description of each block’s usage.
Motion blocks control a sprite’s placement on the stage. Motion blocks are colored blue. There are motion blocks that let you set the direction a sprite will move and then other blocks to move them. There are also motions blocks that report on a sprite’s location and direction. Table 3.1 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Moves a sprite forward or backwards a specified number of steps. | |
No | Rotates a sprite a specified number of degrees in a clockwise direction. | |
No | Rotates a sprite a specified number of degrees in a counterclockwise direction. | |
No | Points a sprite toward a specified direction (0 = up, 90 = right, –90 = left, 180 = down). | |
No | Points a sprite toward either the mouse-pointer or a specified sprite. | |
No | Moves a sprite to a specified coordination location on the stage. | |
No | Moves a sprite to the location of either the mouse-pointer or another sprite. | |
No | Moves a sprite to the specified coordinate position over a specified number of seconds. | |
No | Changes the position of a sprite on the X-axis by a specified number of pixels. | |
No | Changes a sprite’s location on the X-axis to a specified value. | |
No | Changes the position of a sprite on the Y-axis by a specified number of pixels. | |
No | Changes a sprite’s location on the Y-axis to a specified value. | |
No | Changes a sprite’s direction when it makes contact with one of the edges of the stage. | |
Yes | Retrieves a value representing a sprite’s coordinate on the X-axis (between –240 and 240). | |
Yes | Retrieves a value representing a sprite’s coordinate on the Y-axis (between –180 and 180). | |
Yes | Retrieves a value representing a sprite’s current direction (0 = up, 90 = right, –90 = left, 180 = down). |
You will learn more about motion blocks in Chapter 5, “Moving Things Around.”
Looks blocks modify sprite and background appearance and display text within popup bubbles. Looks blocks are colored purple. There are looks blocks that let you modify sprite costumes and colors. There are also blocks that let you modify a sprite’s size and control whether a sprite is visible on the stage. Table 3.2 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Changes a sprite’s costume, modifying its appearance. | |
No | Changes a sprite’s costume to the next costume in the sprite’s costume list, jumping back to the beginning of the list when the end of the list is reached. | |
Yes | Retrieves a numeric value representing a sprite’s current costume number. | |
No | Displays a text message in a speech bubble for a specified number of seconds. | |
No | Displays a text message in a speech bubble or removes the display of a speech bubble when no text is specified. | |
No | Displays a text message in a thought bubble for a specified number of seconds. | |
No | Displays a text message in a thought bubble or removes the display of a thought bubble when no text is specified. | |
No | Modifies a sprite’s appearance by applying and modifying a special effect (color, fisheye, whirl, pixelate, mosaic, brightness, or ghost) by a specified numeric value. | |
No | Applies a special effect (color, fisheye, whirl, pixelate, mosaic, brightness, or ghost) to a sprite by a specified numeric value. | |
No | Restores a sprite to its normal appearance, removing any special effects that may have been applied. | |
No | Modifies the size of a sprite by a specified numeric amount. | |
No | Sets a sprite’s size to a percentage of its original size. | |
Yes | Retrieves a percentage value representing a sprite’s current size when compared to its original size. | |
No | Tells Scratch to display a sprite. | |
No | Suppresses the display of a sprite on the stage, preventing it from interacting with other sprites. | |
No | Places a sprite on top of other sprites, placing it on the top layer and ensuring its display. | |
No | Moves a sprite back a specified number of layers, allowing other sprites to be displayed on top of it. | |
No | Alters the stage’s appearance by assigning it a different background. | |
No | Changes the stage’s background to the next background in the background list. | |
No | Retrieves a numeric value representing the background number of the stage’s current background. |
You will learn more about looks blocks in Chapter 10, “Changing the Way Sprites Look and Behave.”
Sound blocks play music and add sound effects to your Scratch application projects. Sound blocks are colored pink. There are sound blocks that let you play sounds and drum beats, select different types of instruments, control playback volume, and modify tempo. Table 3.3 outlines all of the code blocks that fit into this category.
You will learn more about sound blocks in Chapter 11, “Spicing Things Up with Sounds.”
Pen blocks draw any combination of shapes and lines using a virtual pen. Pen blocks are colored mint green. There are pen blocks that let you enable and disable drawing, set color and pen size, and apply shading. Table 3.4 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Erases or clears away anything drawn by the pen or stamped from the stage. | |
No | Places the pen in a down position, allowing drawing operations to occur as the pen is moved around the stage. | |
No | Disables drawing operations by lifting the pen. | |
No | Specifies the color to be used when drawing. | |
No | Changes the color used when drawing by a specified amount. | |
No | Specifies the color to be used when drawing based on a numeric range in which 0 is red (at the low end of the spectrum) and 100 equals blue (at the high end of the spectrum). | |
No | Modifies the shading used when drawing by a specified amount. | |
No | Specifies the shade to be used when drawing based on a numeric range in which 0 is the darkest possible shading and 100 represents the maximum possible amount of light. | |
No | Modifies the thickness of the pen based on a numeric increment. | |
No | Specifies the thickness or width of the pen used when drawing. | |
No | Draws or stamps the image of a sprite onto the stage. |
You will learn more about pen blocks in Chapter 12, “Drawing Lines and Shapes.”
Control blocks automate the execution of scripts, pause script execution, and send messages to other sprites, allowing sprites to synchronize their execution. There are also control blocks that let you set up loops to repeatedly execute collections of code blocks as well as control blocks that let you conditionally execute other code blocks based on whether or not a test condition evaluates as true. Control blocks are colored gold. Table 3.5 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Executes the script to which it has been attached whenever the IDE’s green flag button is pressed. | |
No | Executes the script to which it has been attached whenever a specified keyboard key is pressed. | |
No | Executes the script to which it has been attached whenever the user clicks on the sprite to which the script belongs. | |
No | Pauses script execution for a specified number of seconds, after which the script resumes its execution. | |
No | Repeatedly executes all of the code blocks embedded inside it. | |
No | Repeats the execution of all the code blocks embedded inside it a specified number of times. | |
No | Specifies a broadcast message to all sprites without pausing script execution. | |
No | Sends a broadcast message to all sprites to trigger a predefined action and then pauses script execution, waiting until all sprites have completed their assigned action before allowing the script in which the block resides to continue executing. | |
No | Executes the scripts to which it has been attached when a specified broadcast message is received. | |
No | Repeatedly executes all of the code blocks embedded within the control for as long as the specified condition evaluates as true. | |
No | Executes all of the code blocks embedded within the control if the specified condition evaluates as true. | |
No | Executes all of the code blocks embedded in the top half of the control (between the If an Else) if the specified condition evaluates as true and executes all of the code blocks embedded in the bottom half of the control (after Else) if the condition evaluates as being false. | |
No | Pauses script execution until a specified condition becomes true. | |
No | Repeats all of the code blocks embedded inside it for as long as a tested condition evaluates as true. | |
No | Halts a script’s execution. | |
No | Halts the execution of all scripts for all sprites in the application. |
You will learn more about control blocks in Chapter 9.
Sensing blocks determine the location of the mouse-pointer, its distance from other sprites, and whether a sprite is touching another sprite. Sensing blocks are colored sky blue. There are sensing blocks that can be used to interact with Scratch boards, allowing applications to detect when the sensor board’s buttons or slider are being pressed. Table 3.6 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Retrieves the location of the mouse-pointer on the X-axis. | |
No | Retrieves the location of the mouse-pointer on the Y-axis. | |
No | Retrieves a Boolean value of true or false, depending on whether a mouse button is pressed. | |
No | Retrieves a Boolean value of true or false, depending on whether a specified key is pressed. | |
No | Retrieves a Boolean value of true or false, depending on whether the sprite is touching a specified sprite, edge, or mouse-pointer as selected from the block’s pull-down menu. | |
No | Retrieves a Boolean value of true of false, depending on whether the sprite is touching a specified color. | |
No | Retrieves a Boolean value of true of false, depending on whether the first specified color inside the sprite is touching the second specified color on the background or on another sprite. | |
No | Retrieves a numeric value representing a sprite’s distance from another sprite or from the mouse-pointer. | |
No | Resets the timer back to its default value of zero. | |
Yes | Retrieves a numeric value representing the number of seconds that the timer has run. | |
No | Retrieves the property value (x position, y position, direction, customer #, and size of volume) for the background of a specified sprite. | |
Yes | Retrieves a numeric value, from 1 to 100, representing the volume of the computer’s microphone. | |
Yes | Retrieves a Boolean value of true or false when a sound value of 30 or greater is detected through the computer’s microphone. | |
Yes | Retrieves the value being reported by one of the sensors on a Scratch board. | |
Yes | Retrieves a Boolean value of true or false, depending on whether a specified sensor is being pressed. |
Note
A Scratch board is a special piece of hardware that you can purchase from the Scratch website and attach to your computer. Once it is attached, you can use a sensor board to collect and process environment- and user-provided input. You will learn how to programmatically interact with and control Scratch boards in Chapter 14, “Collecting External Input Using a Scratch Sensor Board.”
You will learn more about sensing blocks in Chapter 6, “Sensing Sprite Position and Controlling Environmental Settings.”
Numbers blocks perform arithmetic operations, generate random numbers, and compare numeric values to determine their relationship to one another. Numbers blocks are green. There are numbers blocks that can be used to round numeric values and to execute a host of mathematical functions like determining absolute value or square root of a number. Table 3.7 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Adds two numbers together and generates a result. | |
No | Subtracts one number from another and returns the result. | |
No | Multiplies two numbers together and generates a result. | |
No | Divides one number into another and returns the result. | |
No | Generates a random number within the specified range. | |
No | Returns a Boolean value of true or false, depending on whether one number is less than another. | |
No | Returns a Boolean value of true or false, depending on whether one number is equal to another. | |
No | Returns a Boolean value of true or false, depending on whether one number is greater than another. | |
No | Returns a Boolean value of true or false, depending on whether two separately evaluated conditions are both true. | |
No | Returns a Boolean value of true or false, depending on whether either of two separately evaluated conditions is true. | |
No | Reverses the Boolean value from true to false or false to true. | |
No | Retrieves the remainder portion of a division operation between two numbers. | |
No | Returns the nearest integer value for a specified number. | |
No | Returns the result of the selected function (abs, sqrt, sin, cos, tan, asin, acos, atan, Ln, log, E^, and 10^) when applied to the specified number. |
You will learn more about number blocks in Chapter 8, “Doing a Little Math.”
Variables blocks store and retrieve numeric values in computer memory. You will need to use variables to store data as your application executes. For example, if you create a game that challenges the player to try and guess a randomly generated number, you will need to use a variable to store and refer back to this number.
Variables can be used in conjunction with conditional programming logic to control the execution of other code blocks. Variables can also be used to control the repeated execution of code blocks embedded within code block loops. Variables blocks are colored orange. You can create and name custom variables blocks and assign them a starting value. You can also modify their values during script execution. Other code blocks can retrieve variable values and use them as input. Table 3.8 outlines all of the code blocks that fit into this category.
Block | Monitor | Description |
---|---|---|
No | Modifies the value assigned to a numeric value stored in a variable by the specified amount. | |
No | Assigns a value to a numeric variable. | |
Yes | Retrieves the value assigned to a variable. |
You will learn more about variables blocks in Chapter 7.
3.15.25.183