Chapter 2

Scratch Blocks

An exploration of the blocks from which Scratch programs are built

Subsections of Scratch Blocks

Introduction

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:

A Scratch program to count to 10 A Scratch program to count to 10

Here we see a number of different blocks snapped tightly together to form a complete, simple program. This chapter will indroduce the various blocks available in Scratch, as well as explain thier basic functionality and use.

Block Shapes

Scratch blocks come in several shapes, which help identify how they can snap together and are intended to be used:

Name Shape Use
Hat Block Hat block shape Hat block shape First block in a stack of blocks, and identifies what event will start execution of the block stack
Stack block Stack block shape Stack block shape The primary shape of block in a block stack, typically executes some kind of instruction
Cap block Cap block shape Cap block shape Can only be used as the last block in a block stack as it ends the execution of the stack
C block C block shape C block shape Also used in stacks, but allows for one or more stacks of blocks to be snapped inside that will be conditionally or repeatedly executed
Reporter block Reporter block shape Reporter block shape Snaps into the input field of another block, providing a value for that block to use
Boolean block Boolean block shape Boolean block shape Snaps into a boolean input field of another block, and provides a value of true or false

Block Categories

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.

Chapter 4

Motion Blocks

Motion blocks are used to rotate the sprite and to move it around the stage.

Subsections of Motion Blocks

introduction

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:

Scratch Coordinate System Scratch Coordinate System

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.

Info

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.

move () steps

move () steps block move () steps block

The move () steps blocks moves a sprite in the direction it is currently facing. Each step corresponds to one unit on the stage.

Notes

  • The movement of this block is immediate. If you want the movement to be animated over time, use a glide block or repeat the move () steps alternated with a wait () seconds block. See the animated movement design pattern for details.
  • The direction of the sprite may not correspond with the artwork of the sprite. For example, Scratch cat is drawn facing to the left (90 degrees).
  • If the block would move the sprite off-screen in one of the two coordinates, it will stop moving in that direction while still moving in the other. This causes the sprite to appear to “slide” along the edge of the screen.

turn clockwise () degrees

turn clockwise () degrees block turn clockwise () degrees block

The turn clockwise () degrees rotates the sprite in a clockwise direction by the specified number of degrees.

Notes

  • In Scratch, the positive direction of rotation is clockwise
  • In Scratch, and angle of 0 is straight up
  • A Sprite’s appearance and direction may not correspond if it’s costume was not drawn facing upwards

turn counterclockwise () degrees

turn counterclockwise () degrees block turn counterclockwise () degrees block

The turn counterclockwise () rotates the sprite in a counterclockwise direction by the specified number of degrees.

Notes

  • In Scratch, the positive direction of rotation is clockwise
  • In Scratch, and angle of 0 is straight up
  • A Sprite’s appearance and direction may not correspond if it’s costume was not drawn facing upwards

go to ()

go to go to

The go to () block moves the sprite to the specified location. Possible locations include:

  • random position - randomly chosen coordinates on-stage
  • mouse pointer - the locationof the mouse cursor at the moment the block is executed
  • The position of other sprites in the project at the moment the block is executed

Notes

  • The movement is instantaneous.
  • The names of other sprites only appear in the list after they have been added to the project
  • When a sprite is chosen as the target, the position used is always that of the original sprite, not one of its clones

go to x: () y: ()

go to x: () y: () block go to x: () y: () block

The go to x: () y: () block moves a sprite to the coordinates (x, y) provided they are on the stage.

Notes

  • The movement is instantaneous. If you would like it to be animated over time, see the glide () secs to x: () y:() block.
  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to move a sprite to (6,300) would instead place it at (6, 180). The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

glide () secs to ()

go to go to

The glide () secs to () block moves the sprite to the specified location over the specified number of seconds. Possible locations include:

  • random position - randomly chosen coordinates on-stage
  • mouse pointer - the location of the mouse cursor at the moment this block is executed
  • The position of other sprites in the project at the moment this block is executed

Notes

  • The names of other sprites only appear in the list after they have been added to the project
  • When a sprite is chosen as the target, the position used is always that of the original sprite, not one of its clones

glide () secs to x: () y: ()

glide () secs to x: () y: () block glide () secs to x: () y: () block

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.

Notes

  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to move a sprite to (6,300) would instead place it at (6, 180). The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

change x by ()

change x by block change x by block

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.

Notes

  • The movement is instantaneous.
  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to change a sprite’s x position by 600 would instead set its x position to 240. The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

set x to ()

set x to () block set x to () block

The set x to block changes the sprite’s x position to the provided value.

Notes

  • The movement is instantaneous.
  • If you want to set both the x and y coordinates at the same time, use go to x: () y: () instead.
  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to set x to -300 would instead set it to -240. The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

change y by ()

change y by block change y by block

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.

Notes

  • The movement is instantaneous.
  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to change a sprite’s y position by -400 would instead set its y position to -180. The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

set y to ()

set x to () block set x to () block

The set y to block changes the sprite’s y position to the provided value.

Notes

  • The movement is instantaneous.
  • If you want to set both the x and y coordinates at the same time, use go to x: () y: () instead.
  • If the desired position has off-stage coordinates these will be clamped to the max/min allowable coordinate to keep the sprite on-stage. I.e. attempting to set y to 250 would instead set it to 180. The coordinate system for the stage is:

Scratch Coordinate System Scratch Coordinate System

x position

x position block x position block

The x position block reports a sprite’s current x coordinate on the stage.

Notes

  • The value can be displayed on the stage by checking the box next to the block in the code palette
  • The value displayed on the stage is always that of the original sprite
  • When used in a script run by a clone, it refers to the clone’s position

x position

y position block y position block

The y position block reports a sprite’s current y coordinate on the stage.

Notes

  • The value can be displayed on the stage by checking the box next to the block in the code palette
  • The value displayed on the stage is always that of the original sprite
  • When used in a script run by a clone, it refers to the clone’s position

direction

direction block direction block

The direction block reports a sprite’s current direction, an angle between -180 and +180 degrees.

Notes

  • Setting an angle larger or smaller than the bounds will wrap (i.e. 185 will become -175)
  • The value can be displayed on the stage by checking the box next to the block in the code palette
  • The value displayed on the stage is always that of the original sprite
  • When used in a script run by a clone, it refers to the clone’s position
Chapter 5

Looks Blocks

Looks blocks change the appearance of the sprite

Chapter 6

Sound Blocks

Sound blocks play and apply effects to recorded sounds

Chapter 7

Event Blocks

Event blocks trigger the execution of block stacks

Chapter 8

Control Blocks

Control blocks determine when blocks are executed

Chapter 9

Sensing Blocks

Sensing blocks report aspects of sprite and program state, and provide for user interaction

Chapter 10

Operator Blocks

Operator blocks transform and report data.

Subsections of Operator Blocks

Chapter 11

Motion Blocks

My blocks are custom, user-created blocks

Chapter 12

Variable Blocks

Variables blocks allow data to be stored while the program is running