Programming in Scratch
Scratch is an incredibly easy to learn programming language that teaches you the concepts of programming without being too complicated to understand. It is great for making graphical programs such as games and animations and as we progress through the tutorials we will be teaching you how to utilize scratch to get the most out of your Raspberry Pi.
If you installed Raspian then you’ll find Scratch already installed to your desktop and within the Education menu from the bottom-left menu. Scratch can be installed on any platform, not just the Raspberry Pi and can be downloaded here.
Before we start let’s take a look at the Scratch interface (click to enlarge).
As you can see, it is split into several sections which we will look at in more detail. Although we will only briefly look at the different features of Scratch, the tutorials will help you understand it all a lot better.
Scratch programs are written as a flowchart which control images (Sprites) on the Canvas. We can select our code blocks from the sections on the left of the screen. Each set of blocks is split into eight categories which are colour coded.
The motion blocks make the Sprite move and rotate. There are many clever functions, such as bouncing off edges or moving towards other Sprites or the Mouse Pointer.
The control blocks work in the background and control how other blocks of code work. They include loops, conditional statements, pauses and messages. Messages are broadcast throughout the program and can be received by other blocks of code.
The sensing blocks allow your Scratch programs to be more interactive: you can detect when Sprites are touching objects or colours and allow the user to get involved with the use of the Mouse and Text Prompts.
When writing Scripts for your different Sprites, the intention is to create a flowchart that will run in order. The blocks of code can be dragged from the sections on the left onto the Script workspace. If you drag a block close to another block, a thick white line will appear which shows that these two blocks will snap together and therefore run one after another (starting from the top and running downwards).
Blocks that are curved on top (as shown above) show the start of a flowchart.
Sprites and Costumes
Sprites are the images that make up a Scratch program. They can be painted using the built in editor or imported from images. Each Sprite has its own set of code and sounds and can have multiple images attached to it.
The Sprite shown below has two costumes which are similar but when looped will show the cat running. This option for multiple costumes allows us to easily animate our Sprites to give more realistic animations.
We can select which Sprite to edit from the panel below the Canvas. We also have the option to create a new Sprite or import one from an image.
The Canvas is shown in the top left of the window and this is where our Sprites will be displayed and where our code will manipulate them.
There are three options above the Canvas to control how big it appears. We can have a small box, a large box or display the program in Full Screen. This will become useful when we want to display and test our programs.
Finally are the Menus: these can be found at the Top of the screen. Here we can create a new program, save our current one, open a previously saved on. We can also share our program and seek any help with the online documentation.
We hope that this brief introduction to Scratch has given you an idea or what we can do with it and has also made you feel confident with using the program. We will be learning more about the different aspects of Scratch as we progress through the tutorials so if there’s anything you are confused about now, rest assured you will become a Scratch guru by the end of our tutorials.