Operation Manual

Chapter 10: An Introduction to Scratch
So far in this book, youve learned a lot about how to use programs that other people have written on your Raspberry Pi. The
chief goal of the Raspberry Pi project is to get people writing their own programs, howeverand not just adults. The Raspberry
Pi Foundation is working to get the device adopted as an educational tool for all age ranges.
A key requirement for reaching that goal is ensuring that young children can experience the joy of creating their own software,
rather than just consuming other peoples code. The secret to this is Scratch.
Introducing Scratch
Created by the Lifelong Kindergarten group at the Massachusetts Institute of Technology Media Lab in 2006 as an offshoot of
the Squeak and Smalltalk languages, Scratch takes the core concepts of programming and makes them accessible to all. Long-
winded typingtiring and dull for younger childrenis replaced with a simple jigsaw-like drag-and-drop environment, which
nevertheless encourages programmatic thinking and introduces the core concepts used by all programming languages.
Officially considered a program for ages eight and above, but accessible to even younger programmers with a little help and
guidance, Scratch is deceptively powerful. Behind its colourful and mouse-driven user interface is a programming language that
includes impressive multimedia functionality. It should come as no surprise then that, of the more than two-and-a-half million
Scratch projects shared by users of the software on the official website, the majority are games.
Encouraging children to learn how to make their own games can be a great way of sneaking a little learning into their playtimes.
Scratchs friendly user interface and excellent handling of core concepts mean that children are unlikely to get frustrated by a
sudden steep learning curve. Better still, the concepts learned in Scratch offer an excellent foundation for progression to a more
flexible language like Python (see Chapter 11, Python Basics).
Even before moving on to another language, Scratch offers more than just a framework for games. It can be used to create
interactive presentations and cartoons, as well as interfaces with external sensors and motors through the use of add-on
hardware such as the PicoBoard and the LEGO WeDo robotics kit.
The recommended Debian distribution for the Raspberry Pi comes with the latest version of the Scratch development
environment pre-loaded, so if youve been following the recommendations throughout this book youre ready to start. If youre
using a different operating system, visit the official Scratch website at http://scratch.mit.edu to download the software
and you can find versions for Windows and Apples OS X in the same place.
Example 1: Hello World
When learning a new programming language, its traditional to start with a very basic program: one that displays a single line of
text. This is typically referred to as a Hello World program, and its the first step towards learning how to create your own
programs.
Unlike a traditional programming language, Scratch doesnt expect the user to memorise the names of instructions like print or
inkey$. Instead, almost everything is done by dragging and dropping blocks of code and arranging them into a logical pattern.
To begin, load Scratch by double-clicking its icon on the desktop or clicking on its entry in the Programming menu. After a few
seconds, the main Scratch interface will appear (see Figure 10-1). If its off-centre or small, press the Maximise buttonthe
middle of the three window control buttons on the right side of the top title barto fill the screen.
The Scratch interface is split into multiple panes. At the left is the block palette, which holds all the different code blocks you can
use to create a program. A list of objects in the program, known as sprites, appears at the bottom-right along with a control for
the stage on which the sprites appear. The stage itself is at the top-right of the window, which is where you will see the program
running. Finally, the middle of the window is where the program itself is constructed.
Figure 10-1: The main Scratch interface, shown running on the Raspberry Pi