The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

(^62) THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE
drag the first move 10 steps block and pull it and the blocks beneath it out of the forever
block, then drop them underneath the when clicked block. Click and drag the forever
block to the blocks palette to delete it, then click and drag the repeat 10 block under the
when clicked block so it goes around the other blocks.
when clicked
repeat^10
move^10 steps
play sound meow
wait^1 secs
move -10 steps
Click the green flag to run your new program. At first, it seems to be doing the same thing as
your original version: repeating your sequence of instructions over and over again. This time,
though, rather than continuing forever, the loop will finish after ten repetitions. This is known as
a definite loop: you define when it will finish. Loops are powerful tools, and most programs –
especially games and sensing programs – make heavy use of both infinite and definite loops.
Variables and conditionals
The final concepts to understand before beginning to code Scratch programs in earnest are
closely related: variables and conditionals. A variable is, as the name suggests, a value which
can vary – in other words, change – over time and under control of the program. A variable has
two main properties: its name, and the value it stores. That value doesn’t have to be a number,
either: it can be numbers, text, true-or-false, or completely empty – known as a null value.
Variables are powerful tools. Think of the things you have to track in a game: the health of
a character, the speed of moving object, the level currently being played, and the score. All of
these are tracked as variables.
WHAT HAPPENS NOW?
What happens if you change the number in the loop
block to make it larger? What happens if it’s smaller?
What happens if you put the number 0 in the loop block?

Free download pdf