Let's write our first program in Scratch!
Let’s write a program that makes two sprites dance around to some music. Note: Please make sure to periodically save your project using the “Save Now” button in the toolbar to avoid losing your work!
1. Adding a Sprite
First, we need to add a sprite to our project. In the sprites pane, hover over the cat button, then select “Choose a Sprite”. Here, you can select any sprite you’d like to be the second character in our dance scene. Once the sprite is created, you can drag it anywhere you’d like.
2. Adding a Background
Next, let’s add a background. Next to the sprites pane, there is a toolbar used for choosing backgrounds. Select the “Choose a Backdrop” button and choose a fun background for your project.
3. Adding a Block
Let’s experiment a bit with the blocks. Navigate to the “Motion” section of the Block Palette. There are many blocks here that can be used to make our sprites move. When we use a block, it is called running the code.
Drag a “move 10 steps” block to your script area, and click it to make the sprite move. The sprite that will move is the sprite that is selected in your sprite pane. Notice that there are two different script areas for each of your sprites, meaning that the blocks only apply to the sprite that is selected.
4. Adding Loops
We will now write a section of repeating code, called a loop, that will repeat under certain conditions. For now, we won’t use any loops with conditions. Instead, we will use a loop that repeats forever (while the program is running). In your block palette, navigate to the “Events” section. Drag the “forever” block into your script area.
The “forever” block looks different from the “move 10 steps” block; it has a space inside. This space is called the body of the loop. All the code in the body of the loop will execute while the loop is running. In our case, the code in the body of the loop will repeat forever.
5. Writing the Body of a Loop
Drag the following blocks into the body of the loop, and click the blocks to run them. The sprite will move forwards and backwards on it’s own without you having to click on the blocks multiple times. This makes a loop very useful since programmers don’t have to perform repeated actions; the loop does all of the work.
We have added the “wait 0.5 seconds” block so that there is some time before each movement of the sprite. Make sure to select the second sprite and add the code for that sprite as well.
6. Adding an Event
Although clicking a script causes it to run, there is a more formal way to tell the code to start running. Go to the “Events” section in the block palette and clip the “When ⚑ clicked” block to the top of your code. This causes the code to run when the flag symbol above your project window is clicked. Add this for both sprites.
7. Add Background Music
Lastly, you can add some background music to your project. Switch over to the sounds palette, which can be found next to the block palette. You can import a sound to your program by selecting the “Choose a Sound” button, which allows you to use the sound.
Next, go back to your block palette and add another “When ⚑ clicked”, as well as a “forever” block under it. This is needed to make sure the sound loops while the program is running.
Your code should now look like this. Using this tutorial, you can write many different programs using loops, blocks, and sprites. Happy coding!