The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

(^78) THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE
second Events block except choosing ‘right arrow’ from the list and using a
turn  15 degrees Motion block.
when left arrow key pressed
turn^15 degrees
when right arrow key pressed
turn^15 degrees
Press the ← or → key to test your program. You’ll see the cat sprite turning as you do,
matching the direction you’re choosing on the keyboard. Notice how you didn’t need to click on
the green flag this time; this is because the Events trigger blocks you have used are active at
all times, even when the program isn’t ‘running’ in the normal sense.
Do the same steps twice again, but this time choosing ‘up arrow’ and ‘down arrow’ for
the Events trigger blocks, then move 10 steps and move -10 steps for the Motion blocks.
Press the arrow keys now and you’ll see your cat can turn around and swim forwards and
backwards too!
(^) when left arrow key pressed
turn^15 degrees
when right arrow key pressed
turn^15 degrees
when up arrow key pressed
move^10 steps
when down arrow key pressed
move -10 steps
To make the cat sprite’s motion more realistic, you can change how it appears – known in
Scratch terms as its costume. Click on the cat sprite, then click on the Costumes tab above the
blocks palette. Click on the ‘cat1 flying-a’ costume and click on the round X icon that appears

Free download pdf