A quick guide to Scratch on the Raspberry Pi
David Briddock shows how to create games and animations using Scratch
Would you like to delve into the world of animation and game creation? Do you want to bring your imaginative ideas to life without learning a software development language?
With Scratch, from MIT’s innovative Media Lab (media.mit.edu), you can construct all kinds of multimedia projects without writing a single line of code. Its fully visual interface is aimed at anyone old enough to use a keyboard and mouse. In fact, you hardly need to use the keyboard at all.
Scratch does away with the traditional editor and symbolic language approach. In its place there’s a collection of graphical, snap-together programming blocks. There are blocks with different shapes that lock together in specific ways, blocks that perform distinct operations, and blocks with entry fields and drop-down lists for specific data values.
Scratch blocks transform the program creation process into a visually attractive, immediately engaging and fun experience. Blocks are able to perform many types of operations, but their primary use is to control ‘sprites’ (animated graphical objects) and sounds. With Scratch you can load sprites and sounds from the many examples provided or create something new using your own imagination. Despite the simplicity of this block-building concept, it’s possible to create intricate and sophisticated programs, full of colourful scenes and audible effects.
Do you have Scratch?
Time to boot up your Raspberry Pi. You’ll find Scratch 1.4 is preinstalled on the latest ‘official’ operating system, the ‘Wheezy’ release of Debian. There should be either an icon on the desktop or an item in the LXDE desktop menu.
If your Raspberry Pi doesn’t have Scratch, it can be easily added. The ‘Getting Scratch’ boxout explains how it’s installed. What if you don’t have a Raspberry Pi yet? Not to worry, Scratch will also run on PCs. Just check out the ‘Scratch On Your PC’ boxout.
All the information in this article is based on Scratch version 1.4.
The easiest way to dive into Scratch is to start with a ready-made program. This way we’ll have something that works immediately, so we’ll do that, then spend a little time discovering how this particular example is put together, before making some changes of our own.
Scratch has a collection of sample projects and resources that are pre-installed as part of the main application. We’re going to select one called ‘Aquarium’ which, as you might expect, creates an animated fish tank populated by fish and other creatures.
Let’s get started. Click on the Scratch desktop icon or the LXDE menu option to launch the Scratch Studio. To load our chosen sample project follow these steps:
Select the ‘File’ > ‘Open...’ menu
Click ‘Examples’ on the left
Select ‘Animation’ at the top
Select ‘Aquarium’ from the list
Click the ‘OK’ button.
Now the Aquarium project is loaded, we’ll examine the Scratch studio application.
The Scratch Studio has everything we need to design and build animations, games and other fun projects. With the Aquarium project loaded, the Studio should look similar to the Figure 1: aquarium project image.
The studio is divided into three distinct areas or panels. On the left there’s a ‘Blocks Panel’, which contains the collection of blocks used to build programs. These blocks are grouped into various categories, such as motion, control, sensing, sound and so on.
In the centre there’s a ‘Builder Panel’. Here is where we’ll drag and drop our blocks, snapping them together in various combinations. This panel lets us create and edit images, or record and import sounds.
On the right is the ‘Staging Area’. At the top is a visual representation of the Scratch stage - in this case an aquarium complete with plants, bubbles, fish and other creatures. Underneath there’s a collection of ‘sprites’. Each sprite can have more than one image representation. To the left there’s a special ‘Stage’ sprite, which acts as the stage background.
Running the program
At the top of the ‘Staging Area’ there’s a green flag and a red circle. These buttons start and stop the program execution. Press the green flag now to watch the aquarium come to life.
With the three controls just above the green flag button we can alter the size of the ‘Staging Area’, or expand the stage itself to full-screen mode. In full-screen mode it’s much easier to view an animation or play a game.
Spend a little time studying the Aquarium animation in action. Notice how the bubbles appear to rise upwards. Watch how the creatures move around the tank.
The stage animation
So how does all this animation magic happen? Let’s start to peel back the covers and discover what’s involved.
First, click on the special ‘Stage’ sprite icon at the bottom of the ‘Staging Area’. Now look in the central ‘Builder Panel’. In this example the stage sprite has a very simple set of blocks. What do these blocks do?
Let’s read the blocks in sequence from top to bottom. At the top is a ‘green flag’ block. Next there’s a block to create a ‘forever’ (or endless) loop. Inside this loop there’s another block that will switch between different backgrounds, with a pause of one second. In other words, after clicking the green flag, all the backgrounds will be displayed in turn, with a one-second pause between each one.
Where are the backgrounds? In the central ‘Builder Panel’ click on the ‘Backgrounds’ tab. Now we can see there are three separate background images. The number, size and position of bubbles is different for each one.
As a sprite can have any number of images, let’s create a new background. Go to the third background image in the list and click on its ‘Copy’ button. A new fourth background will appear. Click on the fourth background’s ‘Edit’ button and the image appears in a little drawing application. We can have some fun changing the bubble positions and sizes.
After editing, click the ‘OK’ button to save all the changes, then use the green flag button to view the results. If it doesn’t look right, we can either edit it again or delete this new image completely using the round ‘X’ button.
Now it’s time to investigate the other sprites. Select each one in turn and study blocks in the ‘Script’ tab of the central ‘Builder Panel’. It’s these blocks that move the sprite around the aquarium.
Take the octopus sprite as an example. There are two separate sets of blocks. The upper set has a forever loop, which moves the octopus one step at a time across the stage. Inside this loop there’s also a check to see if the octopus has reached the side of the tank. When it does, the direction of movement is reversed - a change of 180º.
The lower set looks very similar. The difference here is that this time the change of direction is determined by the value of a randomly generated number. This adds a sense of realism to the octopus’s movements. Change the random number range to a bigger or smaller value and click the green flag to see what happens.
We can also have a peek at the ‘Costumes’ tab. This is the place to create the sprite’s visual appearance. A sprite can have more than one costume. For example, the jellyfish has two costumes. Try editing the second costume by painting it in a different colour. Click the green flag to see how it looks.
While the animation looks good, it’s very quiet, so we’ll add some sound. Any sprite can be associated with one or more sounds. Let’s add a sound effect to the special ‘Stage’ sprite.
Select the ‘Stage’ icon from the ‘Staging Area’, then go to the central ‘Builder Panel’ and select the last tab named ‘Sounds’. Click the ‘Import’ button to pop up a sound selector window. We could add any sound. However, if we take a look in the ‘Effects’ folder we’ll see a sound called ‘Bubbles’ - which seems quite appropriate. Once selected it appears as a new item in the ‘Script’ panel. Listen to the sound with the play button.
To play our chosen sound over the animation we’ll need to add a new block set, which will be activated when the green flag is clicked.
At the top of the left-hand ‘Blocks Panel’ select the ‘Control’ group. Underneath there’s a ‘when clicked’ block with a green flag on it. Drag and drop this block onto the central ‘Builder Panel’. We’ll also need a ‘forever’ loop, which is also in the ‘Control’ group. Drag it into the ‘Builder Panel’ and snap it onto the ‘when clicked’ block.
Now for the sound. Select the ‘Sound’ group in the ‘Blocks Panel’, then drag the ‘play sound’ block over to the ‘Builder Panel’. It needs to go into the ready-made slot inside the ‘forever’ loop block. The modified stage script can be seen in the Figure 2: sound blocks image.
And that’s it. Click on the ‘green flag’ button and listen to those bubbles. Why not experiment with different sample sounds or even record your own sound.
Now we’ve covered the basics why not load, start and modify some of the other sample projects. There are plenty to choose from, including some classic games like PacMan and Pong.
As we’ve seen, Scratch makes it easy to create multimedia software and have plenty of fun at the same time. For more inspiration visit MIT Media Lab’s large and dynamic educational community at scratched.media.mit.edu. They frequently update this website with new projects and helpful videos.
So, if you’re itching to do some Raspberry Pi programming, but find Python a little daunting, why not ease your itch with a little Scratch?