AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Build a band program

Week 15 · Unit 6: Be a musician · 3 lessons
3

A band of sprites

Computing 4: Unit 6, pp. 74–89
GoalPlan a program with several sprites, give each sprite its own script and test that every part works.
New words
plan · a description of what you will make before you make ittest · to try a program to find out whether it does what you wantcomment · a note in a program that explains what the code does
Explanation

A band program has several sprites. Each sprite has its own script that starts with its own trigger. Scripts that start with the green flag run at the same time. Scripts that start with a key press wait for that key. First make a plan with a table. For each sprite write what starts it, what it does and how it repeats. Then build the scripts and add comments. Finally test every part. For each test write what to press and what you expect to happen. If the result is different, you have found a bug to fix.

SpriteStarts whenWhat it doesLoop
Drummerd key pressedsnare drum, restrepeat (8)
Guitaristg key pressednote C, then note Grepeat until s is pressed
Dancergreen flag clickedswitches between two dance costumesforever
Worked examples
Drummerwhendkey pressedplay drum(1) Snare Drumfor0.25beatsrest for0.25beatsrepeat8Guitaristwhengkey pressedplay note60for1beatsplay note67for1beatsrepeat untilkeyspressed?
Here are the scripts for the drummer and the guitarist. One round of the drummer takes 0.25 + 0.25 = 0.5 beats, so 8 rounds take 4 seconds. One round of the guitarist takes 2 beats.
whenclickedswitch costume todance1wait0.5secondsnext costumeforever
The dancer starts with the green flag and never stops by itself. It switches to its next costume every half second, so it seems to dance to the music.
TestWhat to doWhat should happen
1click the green flagthe dancer dances; no sound
2press d once8 drum hits in 4 seconds, then silence
3press gthe guitar plays C, G, C, G…
4hold the s key for 2 secondsthe guitar stops at the end of its round
A test plan says what to do and what should happen.
Class activity

Plan your band (in a group of three): draw a table with the columns sprite, starts when, what it does and loop. Choose three sprites such as a drummer, a singer and a dancer. Each person writes one script on paper using the Scratch blocks from this unit. Swap scripts and trace each other’s work. Then write three tests: what to press and what should happen. One person is the conductor and calls out the keys. The others act out the scripts. Does everything work as planned?

Practice
1
In the band plan, which key makes the drummer play, and how long does the drum part last?
2
The plan says that the singer starts when the v key is pressed. Tom presses v and nothing happens. Find the bug and fix it.
whenbkey pressedplay note64for1beatsplay note62for1beatsrepeat2play note60for2beats
3
Write a test for the guitarist: what do you press and what should happen?
4
Write a comment for the dancer script that tells a reader what it does.