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
2

Fade out and edit

Computing 4: Unit 6, pp. 74–89
GoalFollow and edit algorithms that change the volume and stop on a condition.
New words
trigger · an event that starts a script, such as a key press or a clickfade out · to get quieter or fainter and fainter until nothing is leftedit · to change something that already existstrace · to follow a program step by step and write down what happens
Explanation

A script does not have to start with the green flag. A trigger is an event that starts a script, such as pressing a key or clicking the sprite. To edit an algorithm you can change the trigger, the notes, the loop or the stop condition. Change volume by a minus number makes the music quieter. The volume block reports the current volume, so a repeat until loop can watch it. A fade-out program lowers the volume each round until it is small enough. After you edit a program, trace it again to check that the new result is the one you want.

whenbkey pressedset volume to90%play note60for1beatschange volume by-20repeat untilvolume<30
Worked examples
RoundVolume when checkedIs volume < 30?Note played at volume
190no90
270no70
350no50
430no30
510yesthe loop ends
Trace the fade-out program. Scratch checks the volume at the start of every round. The last round at volume 30 still plays, because 30 is not smaller than 30. After that the volume is 10 and the loop ends.
Beforewhenclickedplay note67for0.5beatsrest for0.5beatsforeverAfterwhenbkey pressedplay note67for0.5beatsrest for0.5beatsrepeat untilkeynpressed?
Editing changes what a program does. In the second script the trigger, the loop and the stop condition have all been changed. It starts when the b key is pressed and stops when the n key is pressed.
whenclickedsetghosteffect to0changeghosteffect by25wait0.5secondsrepeat4
Sprites can fade out too. The ghost effect hides a sprite. At ghost 0 the sprite is fully visible and at ghost 100 it has disappeared. This firefly goes 25, 50, 75 and then 100, so after 2 seconds it is gone.
Class activity

Human fade-out (with family): pick a word or a hum. Make cards for the volume: 100, 75, 50 and 25. Say the word loudly at 100, then turn to the next card, and make your voice a little quieter each time. The rule is: stop when the volume is smaller than 30. How many times did you say the word? Now change the rule so that you stop when the volume is smaller than 60. How many times now?

Practice
1
Trace the program. How many notes does it play and at what volumes? What is the volume at the end?
whenclickedset volume to100%play note64for1beatschange volume by-25repeat untilvolume<40
2
Edit the program in question 1: it should start when the m key is pressed and stop when the volume is smaller than 60. How many notes does it play now?
3
A firefly starts invisible. What does the player see while this program runs, and how long does it take?
whenclickedsetghosteffect to100changeghosteffect by-25wait0.5secondsrepeat4
4
Name three things that you can change when you edit an algorithm.