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.
Worked examples
Round
Volume when checked
Is volume < 30?
Note played at volume
1
90
no
90
2
70
no
70
3
50
no
50
4
30
no
30
5
10
yes
the 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.
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.
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?
It plays 3 notes at volumes 100, 75 and 50. The volume at the end is 25, because 50 − 25 = 25 and then the check 25 < 40 is true.
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?
It plays 2 notes, at volumes 100 and 75. The volume ends at 50, and 50 < 60 is true. The first block must change to when m key pressed.
3
A firefly starts invisible. What does the player see while this program runs, and how long does it take?
The firefly slowly appears and is fully visible at the end. The ghost effect goes 75, 50, 25, 0. It takes 2 seconds.
4
Name three things that you can change when you edit an algorithm.
For example: the trigger that starts it, the notes or numbers, the loop or how many times it repeats, the stop condition and the volume change.