Wikijunior:Raspberry Pi/Introduction to Scratch

A simple Scratch 3 program which plays a "chirp" sound once after pressing the Go Go button.

Scratch is a visual programming language developed by the Massachusetts Institute of Technology (MIT) to introduce programming concepts to children.

Installing Scratch edit

Scratch is preinstalled on Raspberry Pi OS (and its predecessor Raspbian) by default.

Online Scratch 3 editor edit

There is an online editor on the website that can be used for the tutorials which should work on most modern web browsers such as Google Chrome (and its open-source counterpart Chromium), Microsoft Edge, Apple Safari, Opera and Mozilla Firefox.

Offline Scratch 3 editor edit

Alternatively, you can also download a copy of the offline editor from the official website. Scratch 3 officially supports these operating systems:

You can also install Scratch 3 on Windows 10 and Windows 11 via the Winget command in Windows PowerShell: winget install -e --id MITMediaLab.Scratch.3

Alternative Linux distributions edit

If you use an alternative Linux distribution to Raspberry Pi OS, you may need to install Scratch using the package manager via the Terminal:

  •   Debian and   Ubuntu: sudo apt install scratch
  •   Fedora: sudo dnf install scratch
  •   Arch: sudo pacman -Syu scratch

Try some simple programs edit

A simple "Hello World" program edit

 
A "Hello World" program in Scratch 3.

Creating a "Hello World" program in Scratch 3 is very simple:

  1. From the Events category, select and drag the yellow "when   clicked" block.
  2. From the Looks category, select and drag the purple "say 'Hello!'" block.
  3. Change the text in the purple block from "Hello!" to "Hello World".
  4. Click the Go button (with the green flag icon  ) to start the program.

This is equivalent to writing print("Hello World") in the Python programming language.

Moving a sprite edit

 
A Scratch program which moves the sprite 10 steps each time the Go   button is pressed.

Moving a sprite is essential for making games and other animations. To move a sprite in Scratch, follow these steps:

  1. Add a sprite to your project.
  2. From the Events category, select and drag the yellow "when   clicked" block.
  3. From the Motion category, select and drag the "move 10 steps" block.
  4. Click the Go button (with the green flag icon  ) to start the program.
  5. Watch as your sprite moves 10 steps to the right.

You can change the number in the "move 10 steps" block to make your sprite move faster or slower. You can also drag other motion blocks, such as "turn 10 degrees" and "go to x: 100 y: 100", to make your sprite move in different ways.

Making a sprite jump edit

This is a more advanced Scratch concept, but it's still relatively easy to learn. To make a sprite jump in Scratch, follow these steps:

  1. Add a sprite to your project.
  2. From the Control category, drag a "when   clicked" block category to the Scripting Area.
  3. From the Control category, drag a "wait 1 second" block to the Scripting Area and place it inside the "when green flag clicked" block.
  4. From the Motion category, drag a "change y by 10" block to the Scripting Area and place it inside the "wait 1 second" block.
  5. From the Motion category, drag a "change y by -10" block to the Scripting Area and place it below the "wait 1 second" block.
  6. Click the Go button (with the green flag icon  ) to start the program.
  7. Watch as your sprite jumps into the air and then falls back down.

You can change the number in the "change y by 10" and "change y by -10" blocks to make your sprite jump higher or lower. You can also add more "wait 1 second" blocks to make your sprite stay in the air for longer.

Further reading edit

There are several books on Scratch.

The ScratchJr book is aimed at a younger audience from the age of 5+ and has its own accompanying app which is available for Apple iPad (App Store) and Android tablets (Google Play and the Amazon appstore).

Make Your Own Scratch Games! is aimed at the age of 10+.

TitleAuthorYearISBN
Make Your Own Scratch Games!Anna Anthropy2019ISBN 978-1593279363
The Official ScratchJr Book: Help Your Kids Learn to CodeMarina Umaschi Bers, Mitchel Resnick2015ISBN 978-1593276713

Wikibooks has a book on Scratch though it is incomplete.