Chapter 9
IN THIS CHAPTER
Starting Scratch
Understanding the Scratch screen layout
Positioning and resizing your sprite
Making your sprite move
Changing your sprite’s appearance
Adding sounds and music
The Raspberry Pi was created partly to inspire the next generation of programmers, and Scratch is the perfect place to start. With it, you can make your own cartoons and games and discover some of the concepts that professional programmers use every day.
Scratch is designed to be approachable for people of all ages. The visual interface makes it easy to see what you can do at any time, without having to remember any strange codes, and you can rapidly achieve great results. Scratch comes with a library of images and sounds, so it takes only a few minutes to write your first Scratch program.
In this chapter, we introduce you to Scratch so that you can start to experiment with it. In Chapter 10, we show you how to use Scratch to make a simple arcade game.
Programming is the art and science of creating programs. You can create programs in lots of different ways, and Scratch is just one of them. In Chapter 11, you read about Python, another one.
Scratch and Python are both programming languages, different ways of writing instructions for the computer. Different programming languages are best suited for different tasks. Scratch is ideal for making games, for example, but it’s not much use if you want to create a word processor. Using Python to create games takes longer, but it is more powerful than Scratch and gives you much more flexibility in the type of things you can get the computer to do.
There are two versions of Scratch installed in Raspbian:
https://scratch.mit.edu
), Scratch 2 is the version you are familiar with. Scratch 2 adds some features that are not available in the original Scratch, including the ability for sprites (which are, roughly speaking, game characters) to create copies of themselves, and a feature to make your own blocks (or instructions). (Both are described at the end of this chapter.) Because Scratch 2 requires the Flash software to work, it only runs on the Raspberry Pi 2 and Raspberry Pi 3. Some programs, especially action games, run significantly slower in this version of Scratch, because of all the layers of software required to run Flash on the Raspberry Pi. Simple programs will run fine, but if you want to make games, you will soon notice a performance difference.Both versions of Scratch have been enhanced with features for controlling electronics projects with your Raspberry Pi, as you will see in Chapter 16.
If you're a newcomer to Scratch, we recommend you start by using the original Scratch, because your programs will run much more quickly. If you're already familiar with Scratch from the online version, or you're building programs that could use the additional features of Scratch 2, you might prefer to choose that version instead.
Because of the speed issues associated with Scratch 2, and the fact that it doesn’t run on the Model B+ or earlier Pi models, we're going to use the original Scratch in our examples. We will, however, introduce you to both versions in this chapter so you can find your way around Scratch 2 as you build your own projects.
You access Scratch from the desktop environment.
To start Scratch, select your chosen version from the Applications menu in the top left of the screen. You can find Scratch and Scratch 2 in the Programming
folder.
Scratch divides the screen into four main areas, as you can see in Figures 9-1 and 9-2. The Stage is where you can see your game or animation take shape. There’s a cat on it already, so you can get started straightaway by making it do things, as you see in a minute. In Scratch, the Stage is in the top right, and in Scratch 2, it's in the top left.
Underneath the Stage is your Sprite List. You can think of sprites as the characters in your game. They’re images that you can make do things, such as move around or change their appearance. For now, there’s just the cat, which has the name Sprite1 in Scratch and Cat1 in Scratch 2.
You create a Scratch program by snapping together blocks, which are short instructions. You find the blocks in the Blocks Palette, which is on the left in Scratch and in the middle in Scratch 2. In both cases, it displays the Motion blocks by default. They include instructions to move ten steps, rotate, go to a particular grid reference, and point in a particular direction.
The Scripts Area is where the magic happens! You assemble your program in this space by dragging blocks into it from the Blocks Palette. The Scripts Area is in the middle in the original Scratch and on the right in Scratch 2. In the original Scratch, you can use two buttons in the top right (refer to Figure 9-1) to toggle the Stage between full size and small. In Scratch 2, click the Edit menu to find the option for Small Stage Layout. When the Stage is small, the Scripts Area is bigger, so you might find that useful when you’re writing scripts later in this chapter.
You can drag and drop your sprite (the cat) around the Stage to position it where you would like it to be at the start of your program.
You can also resize the sprite. Two buttons above the Stage (refer to Figures 9-1 and 9-2) are used to enlarge or shrink a sprite. Click one of them, and the mouse pointer changes to arrows pointing outward (for enlarging) or inward (for shrinking). Click your sprite on the Stage repeatedly to change its size.
When you’ve finished resizing, click something that isn’t a sprite to return the mouse pointer to normal and stop resizing.
Experimenting with Scratch is easy. To try out different blocks, just click them in the Blocks Palette. For example, try clicking the Move 10 Steps block, and you should see the cat move to the right. You can also turn the sprite 15 degrees in either direction by clicking the appropriate blocks.
Not all of the blocks will work at the moment because some of them need to be combined with other blocks. There’s no harm in experimenting, however. If you click something that doesn’t work, you might get an error message, but you won’t cause any harm to Scratch or your Raspberry Pi.
Next, we talk you through the different Motion blocks you can use.
You can use two different methods to position and move sprites. The first is to make your sprite “walk,” and to change its direction when you want it to walk the other way. The other is using grid coordinates, which we cover in the next section.
Here are the five blocks you use to move your sprite in this way (see Figure 9-3):
10
Steps: This makes your sprite walk in the direction it is facing. If your sprite has been rotated, the steps taken could move it in a diagonal line across the Stage. You can click the number in this block and then type another number to increase or decrease the number of steps taken, but bigger numbers spoil the illusion of animation. It stops looking like the sprite is walking across the screen when the number of steps taken is too big.15
Degrees: This block rotates your sprite. As with the number of steps, you can edit the number to change the degree by which your sprite is rotated. It walks in the direction it is facing when you use the Move 10 Steps block.Point in Direction 90
: Whatever direction your sprite is facing, this block points it in the direction you want it to face. Use this block as is to reset your sprite to face right. You can change the number in this block to change the direction you want your sprite to face, and the numbers are measured in degrees from the position of facing up. (See Figure 9-4.) It helps to think of it like the hands of a clock: When the hand is pointing right, it’s 90 degrees from the 12 o’clock position; when it’s pointing down, it’s 180 degrees from the top. To point left, you use –90. When you click the arrow in the right of the number box, it gives you a menu from which you can select the four main directions — but you can enter any number.
You might be wondering whether you can enter 270 to point left, and the answer is that it works, but it can cause errors in your programs. If you turn your cat to direction 270 and then ask Scratch which way the cat is facing, it tells you –90. To avoid any inconsistencies like this, keep direction numbers in the range from –179 to 180.
The second way you can move and position your sprite is to use grid coordinates. That makes it easy to position your sprite at an exact place on the screen, irrespective of where it is now.
Every point on the Stage has two coordinates: an X position (indicating where it is horizontally) and a Y position (indicating where it is vertically). The X positions are numbered from –240 at the far left to 240 at the far right. The Y positions are numbered from –180 at the bottom edge of the Stage to 180 at the top edge. That means the Stage is a total of 480 units wide and 360 units tall. The center point of the screen, where your cat begins its day, is where X equals 0 and Y equals 0. Figure 9-5 provides a quick visual reference of how the coordinates work.
When you move the mouse over the Stage, the grid reference of your mouse pointer is shown just underneath the Stage on the right.
Seven Motion blocks use the X and Y coordinates (see Figure 9-6):
0
y:0
: You can use this block to position your sprite at a specific point on the Stage. By default, it returns a sprite to the center of the screen (x=0, y=0). Edit the numbers for X and Y to position your sprite somewhere else.1
secs to x:0
y:0
: When you use the Go To block, your sprite just jumps to its new position. The Glide block makes your sprite float there smoothly instead. You can change the number of seconds the glide takes, including using decimals for part of a second (for example, 0.5 for half a second).10
: This moves your sprite ten units to the right. You can change the number of units and use a negative number if you want to move left instead. Note that this doesn’t affect your sprite’s vertical position and is independent of which way around your sprite is facing.0
: This changes the horizontal position of your sprite on the Stage, without affecting its vertical position. The value 0
returns it to the center of the screen horizontally, and you can edit the number to position it left or right of that. Use a negative number for the left half of the screen and a positive number for the right half.10
: This moves your sprite ten units up the Stage, without affecting its horizontal position, and irrespective of which direction it is facing. You can change the number of units and use a negative number to move the sprite down the screen instead.0
: This changes the vertical position of your sprite on the Stage without affecting its horizontal position, and without regard to which way it faces. Use a positive value for the top half of the Stage and a negative value for the lower half.It can be hard to keep track of where your sprite is and in which direction it’s facing, but you can show the values for its X position, Y position, and direction on the Stage. Select the boxes at the bottom of the Blocks Palette to do this, as shown in Figure 9-7. They clutter the screen a bit, but they can be essential tools for testing when you’re creating a game.
As well as moving your sprite around the screen, you can change what it looks like.
One way to think of sprites is as the characters in a game (although they can be used for lots of other objects, too, such as obstacles). Each sprite can have a number of costumes, which are different pictures of it. If the costumes look fairly similar, you can create the illusion of animation by switching between them. Your cat sprite comes with two costumes, and when you switch between them, it looks like the cat is running. You can think of a costume as being one image in an animation sequence (an animation frame).
You can see the costumes for your sprite by clicking the Costumes tab at the top of the Scripts Area, as shown in Figure 9-8. If you want to modify the cat’s appearance, you can click the button to edit one of the costumes in Scratch or simply click the sprite in Scratch 2 and use the editing canvas to its right. If you want to create a new animation frame, you can click the Copy button beside a costume in Scratch, or in Scratch 2 you can right-click the costume and choose duplicate from the menu that opens. You can then edit the bits you want to change.
In the Blocks Palette, there are two blocks you can use to switch between costumes. (See Figure 9-9.) Click the Looks button above the Blocks Palette to show them:
Scratch includes four blocks you can use to show a speech bubble or a thought bubble onscreen. (Refer to Figure 9-9.) To see them, and to see the other blocks that change a sprite’s appearance, click the Looks button above the Blocks Palette. The speech and thought bubbles are great for giving a message to the player or viewer. You can edit the word in the block (Hello! or Hmm …) to change the text in the bubble. Figure 9-10 shows the speech bubbles (top row) and thought bubbles (bottom row) in action.
If you use one of the options with a length of time in it, the sprite pauses for that length of time and the bubble disappears when it has elapsed.
If you use a block without a length of time, you can make the bubble disappear again by using the Say or Think block again but editing the text so that the text box in the block is empty.
You can apply several graphic effects to your sprite using Looks blocks. In Figure 9-10, we’ve used eight sprites to demonstrate them on the Stage. The Color effect changes the sprite’s Color Palette, turning orange to green in the case of the cat. The Fisheye effect works like a fish-eye lens, making the central parts of the sprite appear bigger. Whirl distorts the sprite by twisting its features around its middle. Pixelate makes the sprite blocky. Mosaic shrinks the sprite and repeats it within the space it usually occupies. The Brightness and Ghost effects can sometimes look similar, but the Brightness effect increases the intensity of the colors (turning the cat’s black outline silver while softening the orange), and the Ghost effect fades out all colors evenly.
Here are the three blocks you use to control graphic effects:
25
: You can select the effect you want to change (by default, it’s the color effect) and enter the amount of it you want to add. You can use negative numbers to reduce the extent to which the effect is applied to your sprite. The color effect has 200 different levels (from 0 to 200), and the other effects typically look best with levels in the range from –100 to 100. Experiment!0
: Use this block to set a chosen effect to a specific level. Choosing 0
turns the effect off again. You can use any of the seven effects with this block.Earlier in this chapter, we show you how to change the starting size of your sprite on the Stage. You can use blocks to issue instructions to change its size, too, so you could make it grow larger as the game progresses, for example.
There are two blocks you can use to resize your sprite:
10
: This block enables you to change the size of your sprite by a certain number of units, relative to its current size. As usual, you can edit the number. If you want to decrease the sprite’s size, use a negative number.100%
: This block sets the size to a percentage of its original size, so with the default value of 100 percent, it effectively resets any resizing you’ve done.Sometimes, you might not want your sprite to be seen on the Stage. If a spaceship is blown up in your game, for example, you want it to disappear from view. These two blocks give you control over whether a sprite is visible:
As well as changing a sprite’s appearance, you can give it some sound effects. Scratch comes with sounds, including slurps, sneezes, and screams; ducks, geese, and owls; and pops, whoops, and zoops. You can find effects for most occasions, and many of them are natural partners for one of the sprites that Scratch provides.
To add a sound to your sprite, you have to do one task first: Import the sound to your sprite. Here’s how you’d do that:
Click the Sounds tab above the Scripts Area, as shown in Figure 9-11, and then click the Import button.
In the original Scratch, it's labelled Import, but in Scratch 2, the button looks like a tiny speaker: Don't confuse it with the large speaker used to represent any existing sound effect on the sprite.
(Optional) After you’ve imported a sound, you can preview it. Click the speaker beside it in Scratch, or the play button in the Sounds Area in Scratch 2. In both versions of Scratch, click the X button to delete a sound from your project.
If you a delete a sound in this way, it remains on your SD card so that you can import it again later.
After a sound has been imported, you use one of the Sound blocks to play a sound. To see all available Sound blocks, click the Sound button at the top of the Blocks Palette first.
The Play Sound block enables you to choose which sound you’d like to play from those you have imported. The Play Sound Until Done block stops the program from running any blocks joined underneath this one until the sound has finished playing.
There are also blocks that let you use virtual drums and pitched instruments to create music using Scratch. Notes are numbered: C is 60, C# is 61, D is 62, and so on. There’s a block called Play Note 60
for 0.5
Beats that plays a note with a particular number for a certain duration. When you click the menu in this block to specify which note to play, a piano opens that you can use to select the note.
You can use a block called Set Instrument to 1
to change the instrument, but if you want to use this, you need to install the different instruments first if you're using the original Scratch. To do so, click the Terminal icon on the task bar at the top of the desktop and then issue these commands in the Terminal window (explained in greater depth in Chapter 5):
sudo apt-get update
/usr/share/scratch/timidityinstall.sh
Finally, in order to finish installing the instruments, you need to reboot your Raspberry Pi. Save your work in any open applications first, and then open the Applications menu, click Shutdown, and choose Reboot.
Clicking blocks in the Blocks Palette is one way to issue commands to Scratch, but if that’s all you’re doing, you’re not really programming. The fact is, if you have to click each block every time you want to run it, you’re doing all the hard work of remembering the instructions, and the computer can work only as fast as you can click the blocks.
A program is a reusable set of instructions that can be carried out (or run) whenever you want. To start to create a program, you drag blocks from the Blocks Palette and drop them in the Scripts Area in the middle of the screen. Most blocks mentioned so far in this chapter have a notch on the top and a lug on the bottom, so they fit together like jigsaw pieces. You don’t have to align them perfectly: Scratch snaps them together for you if they’re close enough when you release the mouse button.
You put your blocks in the order you want Scratch to run them, starting at the top and working your way down. It’s a bit like making a to-do list for the computer.
A group of blocks in the Scripts Area is called a script, and you can run the script by clicking anywhere on it. Its border is highlighted, and you’ll see the cat move around the Stage as you’ve instructed it to.
You can have multiple different scripts in the Scripts Area, so you could have one to make the cat walk left and another to make it walk right, for example. When you add multiple sprites (see Chapter 10), each sprite has its own Scripts Area and scripts there to control it.
The moonwalk is the dance, popularized by Michael Jackson, that makes you look like you’re walking forward while you’re actually moving backward. Figure 9-12 shows a script to make your cat moonwalk across the Stage. The first two lines in the script reset the cat to the middle of the screen, facing right. The cat tells you that it loves to moonwalk and then lets out a little whoop like Michael Jackson’s, which it continues for the duration of the dance. The costume switch changes the position of the cat’s legs, and it then glides 150 units to the left. Close the speech bubble by using the Say block with nothing in it, and then switch back to the other costume, which makes the cat’s legs move back to their default position. Give it a go!
As you put your script together, you might find that some movements happen so fast that you can hardly see what’s going on.
If you click the Control button at the top of the Blocks Palette, you can find a set of yellow blocks that are used to govern when particular things happen. You can read more about these in Chapter 10, but for now it’s worth knowing that a block here enables you to wait for a certain number of seconds. Drag this into your script where necessary to introduce a delay so that you can see each of your blocks in action. The length of the delay is 1 second by default, but you can change it to whatever you want, including parts of a second (for example, 0.5 for half a second).
Remember to save your work so that you can come back to it later. You can find the option to save on the File menu, at the top of the screen, or you can click the Floppy Disk icon in the top left in the original Scratch.
In the original Scratch, when the Save Project dialog box opens (see Figure 9-13), you see buttons on the left to choose from various places you could save your file, although you might not have permission to use all of them. (See Chapter 5 for more on permissions.) We recommend that you use the Scratch
folder inside your pi
directory. On the right, you can add your name and some project notes to remind you later what the project was about. You can see and edit the project notes associated with a file by using the File menu when you’re working on a program.
When you save in Scratch 2, you see a file browser that enables you to choose where to save. Again, we recommend saving in your Scratch
folder inside your pi
directory.
The two versions of Scratch on your Raspberry Pi share the same core blocks, but there are a few differences in Scratch 2 to be aware of:
Despite these differences, you should find that programs written for the original Scratch will work in Scratch 2, so you can use tutorials and books based on them. For more guidance on Scratch 2, see our recommended resources at the end of Chapter 10.
18.223.172.252