Time for action - add background music

Let's add some music:

  1. From the sprites list, select Stage.
  2. Add the when flag clicked block to the stage's script area.
  3. From the Control palette, add the forever block.
  4. From the Sounds palette, add the play sound until done block to the forever block.
  5. Let's import the music file. Click on the Sounds tab, and then click the Import button to display the Import Sound dialog box.
  6. Browse to and select the music file you want to add. For example, I'm going to use the Drum sound from the Music Loops folder in Scratch's sound library. Click OK to add the new sound.
  7. Click back to the Scripts tab and set the play sound block to use the new sound file.
  8. Click the flag to play the game and listen to the music.
Time for action - add background music

What just happened?

Hopefully, you chose a sound or song you liked. Each time we clicked the flag, the music played forever—or as long as the ball did not hit the red stripe. It was otherwise independent of the ball or the paddle, which is why we associated the script with the stage.

If your music didn't play based on the exercise, make sure you added the script to the stage, not the paddle or ball sprites.

Do something, forever

When we want to iterate through a script an infinite number of times, we can use a forever block. Sound is only one example. Another example includes moving across the stage while switching between costumes, which can be included in a forever loop to give the appearance of walking or running. You could also apply a graphical effect to a sprite every five seconds.

The length of time it takes to complete one trip through the loop depends on the code that's included in the loop. For example, if we play a seven minute song, the loop repeats once every seven minutes.

The forever loops can be as complicated as we need them to be. We can nest other forever loops, repeat blocks, and multi-block scripts.

Working with sound

We've now seen two ways in which we can work with sound. In Chapter 5, we added sound effects to play based on an event. In Chapter 6, we imported a music file that plays continuously as theme music.

Have a go hero

Find a sound that might be suitable for a game over sound, and play it when the ball hits the red stripe.

Tip

Hint: Wait until the ball touches the red color.

Pop quiz

Which of the following resources can you use to import a sound into your project?

  • Scratch's built-in sound library
  • Your personal MP3 collection
  • A third-party sound download site
  • All of the above

Forever, on one condition

We've already seen a few instances of a forever loop, and we know that the code inside the forever loop runs as long as the project runs. Adding the background music to the game is an example. Click the flag, and the music stops. If the ball hits the red stripe, the entire game stops.

Sometimes, we may want to run a block of code only when a certain condition is met. Let's take a look at a conditional loop using the forever if block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.132