Music Sprite Programming
Senior 1 · ICT
Music Sprite Programming
Lesson 1 of 5
22 min
Phones play a tune when a message arrives, games play a sound when you score, and music apps let you build a song from loops. All of that sound is controlled by code. In this unit you will make sprites that play music when you click them, build your own tune, record a sound clip, and finish with a robot that dances to your beat.
You will use Scratch, a free programming language made by the MIT Media Lab. Instead of typing code, you snap coloured blocks together. Scratch runs in a web browser at scratch.mit.edu, or offline with the Scratch app.

Point at the round Choose a Sprite button (the cat face) below the sprite pane. Four options appear:
| Option | What it does |
|---|---|
| Choose a Sprite | Opens the library of ready-made sprites: animals, people, instruments and more |
| Paint | Opens the paint editor so that you can draw your own sprite |
| Surprise | Adds a random sprite from the library |
| Upload Sprite | Imports a picture saved on your computer (for example a PNG or JPG file) as a new sprite |
To remove a sprite you do not need, such as the Scratch cat, click the small bin icon on its thumbnail.
Each sprite can have several costumes: different pictures of the same sprite. A singer might have one costume with her mouth closed and one with it open. On the Costumes tab, right-click a costume and choose duplicate to copy it, then change the copy with the paint tools. The Stage has pictures too, called backdrops.
Select the sprite, open the Sounds tab and point at the Choose a Sound button. You can Choose a sound from the library (sorted into groups such as Animals, Loops, Percussion and Voice), Record one with a microphone, take a Surprise, or Upload a sound file (MP3 or WAV) from your computer. You can rename a sound by typing in the name box above it.
In this unit, scripts are written as text, one block per line. Lines indented under a block sit inside it, and end marks where a C-shaped block closes. Round brackets ( ) hold a value or a choice from a menu, square brackets [ ] hold a name or a key, and pointed brackets < > hold a yes-or-no condition.
A sound block on its own does nothing until something starts it. You make a working combo by snapping an Events block (a hat block, which has a rounded top) above one or more Sound blocks. Here are three combos:
The first combo runs when the green flag is clicked, the second when the sprite itself is clicked, and the third when the space bar is pressed. Meow is the sound that comes with the Scratch cat; use the name of any sound in your sprite's Sounds tab.
Worked example
a singer who looks as if she is singing. The singer sprite has two costumes, singing and not singing, and a sound called Singer1.
Because the middle block waits until the song ends, the singer keeps her singing costume for the whole song and only then closes her mouth. If you used start sound instead, the last block would run straight away, so the costume would switch back while the song was still playing and she would never appear to sing.
Timing. Suppose sound A lasts 3 seconds and sound B lasts 2 seconds. play sound (A) until done followed by play sound (B) until done plays them one after the other: 3 + 2 = 5 seconds. start sound (A) followed by play sound (B) until done starts them together, so the script finishes after 2 seconds while A plays on for 1 more second.
Tip
Memory hook: a hat block starts a script; nothing runs without one. "Until done" means wait for me; "start" means don't wait. When a question asks why a costume changes back too early, look for a start sound block.
Mugisha has drawn a drum on paper, photographed it and saved the photo on his computer. Which option in the Choose a Sprite menu turns the photo into a sprite?
Solution: Upload Sprite. It imports a picture file from the computer. Paint is for drawing a new sprite inside Scratch.
Write a combo that plays the sound Clap whenever the letter c is pressed.
Solution: when [c] key pressed on top, then start sound (Clap) (or play sound (Clap) until done) underneath.
A script has play sound (Drum) until done but no hat block above it. What happens when the green flag is clicked?
Solution: Nothing. A script only runs when an event starts it. Add when green flag clicked on top.
Sound X lasts 4 seconds and sound Y lasts 3 seconds. How long does the script take to finish if it is (a) play sound (X) until done then play sound (Y) until done; (b) start sound (X) then play sound (Y) until done?
Solution: (a) 4 + 3 = 7 seconds, one after the other. (b) Both start together and the script waits only for Y: 3 seconds (X keeps playing for 1 more second).
Summary
Sources and credits
The combo examples, the timing examples and the practice items were written by Ganzaa.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
when green flag clicked
play sound (Meow) until done
when this sprite clicked
start sound (Meow)
when [space] key pressed
play sound (Meow) until done
say [Muraho!] for (2) secondswhen this sprite clicked
switch costume to (singing)
play sound (Singer1) until done
switch costume to (not singing)