Loops and Sound in Snap!

In the last lesson, you learned how to make a sprite move and turn in Scratch. In this lesson, you will learn how to add a new sprite, repeat blocks, and add sound to your programs.

Adding Another Sprite

To add a new sprite to your project, you can click on one of the options by the Sprites area of the screen. For now, click on the icon for add a new Turtle sprite.

To change the way your new sprite looks, you can import a new costume for it. At the top of the Snap! screen, click on File and then Costumes.

The Costumes window will appear. Choose a costume that you like and click Import. Then click Cancel to close the Costumes window.

Now the Sprites area on your screen should contain two sprites.

Each sprite in a Snap! project has its own scripts. To see the scripts for a particular sprite, make sure that sprite is selected in the Sprites area.

Exercise: To review what you learned last time, write a program that makes your new sprite move in a square. How many blocks did you use?

Loops

You had to repeat the move and turn blocks to make the sprite move in a square. To make your program simpler, you can use the forever block in the Control menu. This block is called a loop. A loop is a programming structure that repeats a portion of a program. Look at the Control menu. Which of the other blocks do you think might be loops?

Other blocks can be placed inside the forever block. The forever block repeats the blocks inside it until you press the stop sign to stop the program.

Exercise: Try out the program below to make the sprite move in a square. The green pen commands are located in the Pen menu. You can use them to draw a sprite’s path on the screen.

The repeat block is a loop that repeats the blocks inside it a certain number of times. For example, the program below makes a sprite tilt back and forth five times.

Exercise: Modify your program to make the sprite move in a square exactly one time. Can you make the sprite move in a hexagon?

Adding Sound

You can add sounds to your program to make it more fun. If you have sound clips that you want to add to your project, you can upload those using the Sounds tab. Once you have added a sound file to your project, you can play it using the play sound and play sound until done blocks on the Sound menu.

You can also use the play note and rest blocks (Sound menu) to create music. The play note block requires two numbers. The number on the left determine the pitch of the note; it should be between 30 and 130. The number on the right determines the length of the note; it can be any positive number.

The rest block can be used to separate notes. During the rest, no sound is played. The number in the rest block is the length of the rest. It can be any positive number.

Exercise: Use the play note and rest blocks to create a short song. Place the set tempo block (Sound menu) at the start of your song. How does changing the number in this block affect the music?

Exercise: Make a sprite move in rhythm with your song, and then use a loop to make the sprite repeat the song and dance.