Music Sprite Programming
Senior 1 · ICT
Music Sprite Programming
Lesson 5 of 5
25 min
You now know how to add sprites and sounds, use a variable as a switch, program drums and notes, and record your own clip. In this project you will combine all of them: a robot that dances to a drum beat when you switch it on and stops when you switch it off. Programmers work in stages, plan, build, test, improve, and you will too.
Before touching the blocks, write down what each part must do. A simple planning table:
| Part | What it must do | Blocks it needs |
|---|---|---|
| Robot sprite | Change costume in time with the beat while the switch is on | forever, if, next costume, play drum |
| Button sprite | Turn the dancing on and off; stop the music when turned off | when this sprite clicked, if … else, set, stop all sounds |
| Stage | Play your Audacity clip as background music while the switch is on | forever, if, play sound until done |
| Variable dancing | 0 = off, 1 = on; for all sprites | set |
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.
Robot script:
costume1 stands for the name of your robot's first costume. While dancing is 1, the robot changes pose on every drum hit. The drum blocks also act as the timing: each lasts 0.5 beats, so at the starting tempo of 60 the robot changes pose every half second.
Button script:
This is the toggle from Lesson 2. The stop all sounds block cuts the background music at once when the robot is switched off; without it, the clip would play on to its end.
Stage script:
The background music uses the same "forever if" pattern. The volume is set to 70% so that the drum hits of the robot can still be heard above the music.
| Test | Expected result |
|---|---|
| Click the green flag | The robot stands still in its first costume; no music |
| Click the button once | The button says "Dance!"; the robot dances; drums and music play |
| Click the button again | The robot stops; all sound stops at once |
| Click it a third time | Dancing starts again |
| What you see | Likely cause | Fix |
|---|---|---|
| The robot never dances | The if is not inside forever, so it checks only once | Move the if inside the forever block |
| The robot flickers too fast to see | Costume changes with nothing to slow them down | Keep a drum block (or a wait) after each next costume |
| Music goes on after switching off | The clip plays until it is done | Add stop all sounds in the else part of the button |
| The robot is dancing as soon as the flag is clicked | dancing kept its value from the last run | Add set [dancing] to (0) under the green flag |
| The robot "jumps" when it changes costume | The costumes are not centred on the same point | In the paint editor, line the costumes up on the centre |
Tip
Test one change at a time. When something breaks, the last thing you changed is the first suspect. Watching the dancing monitor on the stage tells you straight away whether the problem is in the button or in the robot.
Why must dancing be made "for all sprites" and not "for this sprite only"?
Solution: The Button changes it, while the Robot and the Stage read it. A variable made "for this sprite only" could be used by one sprite only, so the robot would never see the button's changes.
At tempo 60, how many times does the robot change costume in 6 seconds of dancing?
Solution: Each costume change is followed by a drum of 0.5 beats, which is 0.5 seconds at tempo 60. In 6 seconds: 6 ÷ 0.5 = 12 costume changes.
Keza deletes both drum blocks from the robot script to make it silent. Now the robot looks like a blur. Explain and fix.
Solution: The drum blocks were also the timing. Without them, forever changes the costume many times a second. Put wait (0.5) seconds after each next costume.
Suggest one test that checks the button works correctly, with its expected result.
Solution (one example): Click the button twice. Expected: the robot dances after the first click and stops, with all sound off, after the second.
Summary
Sources and credits
The dancing-robot design, scripts, test table, bug table and practice items were written by Ganzaa; the idea of switching costumes in time with sounds, and the tip about centring costumes, are adapted from Rock band.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
when green flag clicked
set [dancing] to (0)
switch costume to (costume1)
forever
if <(dancing) = (1)> then
next costume
play drum ((2) Bass Drum) for (0.5) beats
next costume
play drum ((1) Snare Drum) for (0.5) beats
end
endwhen this sprite clicked
if <(dancing) = (0)> then
set [dancing] to (1)
say [Dance!] for (1) seconds
else
set [dancing] to (0)
stop all sounds
endwhen green flag clicked
set volume to (70) %
forever
if <(dancing) = (1)> then
play sound (my beat) until done
end
end