Scratch Blocks
An exploration of the blocks from which Scratch programs are built
An exploration of the blocks from which Scratch programs are built
As a block-based programming langauge, Scratch programs are built from snippets of code represented by blocks. These blocks have a quasi-puzzle piece shapes that helps indicate how they can fit together. Consider this example program that counts to ten:
Here we see a number of different blocks snapped tightly together to form a complete, simple program. This chapter will introduce the various blocks available in Scratch, as well as explain thier basic functionality and use.
Scratch blocks come in several shapes, which help identify how they can snap together and are intended to be used:
Scratch blocks are further organized into color-coded categories based on what the block does. These categories appear at the left of the block palette, and are:
Category | Color | Purpose |
---|---|---|
Motion | Rotate the sprite and move it around the stage | |
Looks | Alter the appearance of the sprite | |
Sound | Play and apply affects to pre-recorded sounds | |
Events | Trigger execution of block stacks | |
Control | Control program flow in block stacks | |
Sensing | Blocks that determine details from the user and the program state | |
Operators | Perform operations on data | |
Variables | Get and set variable and list values | |
My Blocks | Custom, user-created blocks |
Additional block categories can be supplied by Scratch Extensions. These are marked by a custom icon.
Motion blocks are used to rotate the sprite and to move it around the stage.
The motion blocks control the motion of the sprite across the stage. In working with these blocks, it is important to remember the coordinate system Scratch uses, which is based on Cartesian coordinates. These consist of two coordinates, the first is along the x-axis (horizontal), and the second along the y-axis (vertical). The boundary of the stage is from -240 to 240 on the x-axis, and -180 to 180 on the y-axis. This can be seen in the image below:
Sprites are allowed to move anywhere on the stage, but cannot move off it (if you want your sprite to disappear off-stage, use the hide block from the Looks palette). Attempting to change a coordinate to a value off-stage will instead set it to the maximum/minimum value.
In addition to specifying movement using coordinates, sprites can also be moved forward. Each sprite has a direction it is facing, measured as an angle between 0 and 360 degrees. The angle 0 is straight up, and positive rotations are clockwise. Rather than specifying positive and negative rotations on blocks, Scratch uses a clockwise and counterclockwise arrow to indicate a change in direction.
While Scratch adopts the familiar Cartesian coordinate system from mathematics, it does not adopt the unit circle (with the angle 0 aligning with the postive x-axis and positive rotations in a counter-clockwise direction). Instead it adopts clockface-based angles, with 0 degrees corresponding to 12-o’clock position, and positive rotation matching in the direction of clock hand movement.
The move () steps blocks moves a sprite in the direction it is currently facing. Each step corresponds to one unit on the stage.
The turn clockwise () degrees rotates the sprite in a clockwise direction by the specified number of degrees.
The turn counterclockwise () rotates the sprite in a counterclockwise direction by the specified number of degrees.
The go to () block moves the sprite to the specified location. Possible locations include:
The go to x: () y: () block moves a sprite to the coordinates (x, y) provided they are on the stage.
The glide () secs to () block moves the sprite to the specified location over the specified number of seconds. Possible locations include:
The glide to x: () y: () block moves a sprite to the coordinates (x, y) over the specified number of seconds, provided the coordinates are on the stage.
The change x by () block changes the sprites x-coordinate by the supplied value. For example, if the sprite was currently at x: 110, and the value of 10 was supplied to this block, the new value would be x: 120.
The set x to block changes the sprite’s x position to the provided value.
The change y by () block changes the sprites y-coordinate by the supplied value. For example, if the sprite was currently at y: 130, and the value of -10 was supplied to this block, the new value would be x: 120.
The set y to block changes the sprite’s y position to the provided value.
The x position block reports a sprite’s current x coordinate on the stage.
The y position block reports a sprite’s current y coordinate on the stage.
The direction block reports a sprite’s current direction, an angle between -180 and +180 degrees.
Looks blocks change the appearance of the sprite
Sound blocks play and apply effects to recorded sounds
Event blocks trigger the execution of block stacks
Control blocks determine when blocks are executed
Sensing blocks report aspects of sprite and program state, and provide for user interaction
Operator blocks transform and report data.
My blocks are custom, user-created blocks
Variables blocks allow data to be stored while the program is running