Scratch Snake Tutorial – Section 10 – Sound Playback

This page is part of the Snake in MIT Scratch Tutorial.

Sound Playback

All the code for sound playback is contained within the Sound Controller sprite script.

The sounds are triggered using Scratch broadcast blocks. When the script receives an event it plays the appropriate sound clip:

Sound Events

Scratch broadcast blocks are used to trigger sound clips.

When the code from a game event such as the apple being eaten wishes to play a sound it uses the broadcast block instead of playing the sound directly:

Apple Eat Check

A sound is triggered when the snake eats an apple.

During the Eating Apple section of the code a broadcast Apple Eaten block triggers the sound.

Storing all the play sound blocks together in one script has the advantage that if the sound clips need to be changed they are easy to find. Instead of searching through the game logic for events that trigger sounds they can all be located by looking in the Sound Controller sprite script.

More information on the Scratch broadcast block can be found in the documentation.

A Move Sound with Two Tones

As the snake moves it plays two tones that repeat.

The repetition is accomplished using the modulo operation:

Two tones are repeated as the snake moves.

Two tones are repeated as the snake moves.

When the green flag is clicked a variable called Move Count is set to zero. This value is used to record which movement tone is due to play next.

Each time the Snake Moved event is received the system checks the value of Move Count using an if statement in order to play the appropriate sound clip.

The following calculation is performed to set Move Count to the correct value for the next move:

Move Count = (Move Count + 1) mod 2

The Move Count value is increased by one and then constrained to the number of movement tones (two in this case). More movement sounds could be added by increasing this limit.


All the sound playback code is contained within the Sound Controller sprite script.

Scratch broadcast blocks are used to trigger sounds. This centralizes all of the play sound blocks making it easier to change sound clips.

Special code that uses the modulo operator creates the two tone repeating snake move sounds.

Next Section: Conclusion

Table of Contents