[go: up one dir, main page]

0% found this document useful (0 votes)
88 views10 pages

Raspberry Pi Begginer Guide 2020 Part 8

This document provides instructions for creating interactive games using Scratch. It guides the reader through building a reaction time game where the player presses the space bar as quickly as possible after a prompt. It then calculates and displays their reaction time and how far the International Space Station would have traveled in that time. It also provides a challenge to create a game for a profession requiring quick reflexes. Finally, it outlines instructions for a two-button synchronization swimming game.

Uploaded by

New Register
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views10 pages

Raspberry Pi Begginer Guide 2020 Part 8

This document provides instructions for creating interactive games using Scratch. It guides the reader through building a reaction time game where the player presses the space bar as quickly as possible after a prompt. It then calculates and displays their reaction time and how far the International Space Station would have traveled in that time. It also provides a challenge to create a game for a profession requiring quick reflexes. Finally, it outlines instructions for a two-button synchronization swimming game.

Uploaded by

New Register
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE

5Figure 4-11: The space background appears on the stage

Upload your new sprite by clicking the upload sprite icon , next to the words ‘New sprite:’
at the top of the sprites pane. Find the file Astronaut-Tim.png in the Downloads folder, click to
select it, then click OK. The sprite appears on the stage automatically, but might not be in the
middle: click and drag it with the mouse and drop it so it’s near the lower middle (Figure 4-12).

5Figure 4-12: Drag the astronaut sprite to the lower middle of the stage

With your new background and sprite in place, you’re ready to create your program. Start
by creating a new variable called ‘time’, making sure that ‘For all sprites’ is selected before
clicking OK. Click on your sprite – either on the stage or in the sprite pane – to select it,

Chapter 4 Programming with Scratch 71


then add a when clicked block from the Events category to the scripts area. Next, add a
say Hello! for 2 secs block from the Looks category, then click on it to change it to say ‘Hello!
British ESA Astronaut Tim Peake here. Are you ready?’

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

Add a wait 1 secs block from the Control category, then a say Hello! block. Change
this block to say ‘Hit Space!’, then add a reset timer block from the Sensing category. This
controls a special variable built into Scratch for timing things, and will be used to time how
quickly you can react in the game.

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

Add a wait until Control block, then drag a key space pressed? Sensing block into its
white space. This will pause the program until you press the SPACE key on the keyboard, but
the timer will continue to run – counting exactly how long between the message telling you to
‘Hit Space!’ and you actually hitting the SPACE key.

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

wait until key space pressed?

You now need Tim to tell you how long you took to press the SPACE key, but in a way that’s
easy to read. To do this, you’ll need a join Operators block. This takes two values, including
variables, and joins them together one after the other – known as concatenation.

72 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE

Start with a say Hello! block, then drag and drop a join Operators block over the word
‘Hello!’. Fill in the first box with ‘Your reaction time was ’, making sure to add a blank space at
the end, then drag another join block into the second box. Drag a timer reporting block from
the Sensing category into what is now the middle box, and type ‘ seconds.’ into the last box –
making sure to include a blank space at the start.

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

wait until key space pressed?

say join Your reaction time was join timer seconds.

Finally, drag a set time to 0 Data variables block onto the end of your sequence, then
replace the ‘0’ with a timer reporting block. Your game is now ready to test by clicking on the
green flag above the stage. Get ready, and as soon as you see the message ‘Hit Space!’, press
the SPACE key as quickly as you can (Figure 4-13) – see if you can beat our high score!

5Figure 4-13: Time to play the game!

Chapter 4 Programming with Scratch 73


You can extend this project further by having it calculate roughly how far the International
Space Station has travelled in the time it took you to press the SPACE key, based on the
station’s published speed of seven kilometres per second. First, create a new variable called
‘distance’. Notice how the blocks in the Data category automatically change to show the new
variable, but the existing time variable blocks in your program remain the same.
Add a set distance to 0 block, then drag a ● * ● Operators block – indicating
multiplication – over the ‘0’. Drag a time reporting block over the first blank space, then type
in the number ‘7’ into the second space. When you’re finished, your combined block reads
set distance to time * 7 . This will take the time it took you to press the SPACE key and
multiply it by seven, to get the distance in kilometres the ISS has travelled.

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

wait until key space pressed?

say join Your reaction time was join timer seconds.

set time to timer

set distance to time * 7

Add a wait 1 secs block and change it ‘4 secs’. Finally, drag another say Hello! block onto
the end of your sequence and add two join blocks, just as you did before. In the first white
space, type ‘In that time the ISS travels around ’, remembering to include the space at the end,
and in the last white space type ‘ kilometres.’, again remembering the space at the start.

74 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

wait until key space pressed?

say join Your reaction time was join timer seconds.

set time to timer

set distance to time * 7

wait 4 secs

say join In that time the ISS travels around join kilometres.

Finally, drag a round Operators block into the middle blank space, then drag a distance
reporting block into the new blank space it creates. The round block rounds numbers up or
down to their nearest whole number, so instead of a hyper-accurate but hard-to-read number
of kilometres you’ll get an easy-to-read whole number.

when clicked

say Hello! British ESA Astronaut Tim Peake here. Are you ready? for 2 secs

wait 1 secs

say Hit Space!

reset timer

wait until key space pressed?

say join Your reaction time was join timer seconds.

set time to timer

set distance to time * 7

wait 4 secs

say join In that time the ISS travels around join round distance kilometres.

Chapter 4 Programming with Scratch 75


Click the green flag to run your program, and see how far the ISS travels in the time it takes
you to hit the SPACE key. Remember to save your program when you’ve finished, so you can
easily load it again in the future without having to start from the beginning!

5Figure 4-14: Tim tells you how far the ISS has travelled

CHALLENGE: WHO’S FAST?


As well as astronaut, what other professions require
split-second reflexes? Can you draw your own sprites
and backgrounds to show one of these professions?

Project 2: Synchronised Swimming


Most games use more than a single button, and this project demonstrates that by offering
two-button control using the ← and → keys on the keyboard.

ONLINE PROJECT
This project is also available online at rpf.io/synchro-swimming

76 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE

Create a new project and save it as ‘Synchronised Swimming’. Click on the Stage in the stage
control section, then click the Backdrops tab. Click on a water-like blue colour from the palette,
click on the fill with colour icon and click on the white backdrop (Figure 4-15).

5Figure 4-15: Fill the background with a blue colour

Right-click the cat sprite and click ‘delete’. In the sprite area, click the ‘choose sprite from
library’ icon to see a list of built-in sprites. Click on the Animals category, then ‘Cat1 Flying’
(Figure 4-16), then OK. This sprite also serves well for swimming projects.

Click the new sprite, then drag


two when space key pressed
Events blocks into the scripts
area. Click on the small
down‑arrow next to the word
‘space’ on the first block
and choose ‘left arrow’ from
the list of possible options.
Drag a turn

15 degrees
Motion block under your
when left arrow pressed block,
5Figure 4-16: Choose a sprite from the library then do the same with your

Chapter 4 Programming with Scratch 77


second Events block except choosing ‘right arrow’ from the list and using a
15 degrees Motion block.

turn

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

78 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE

at its top-right corner to delete it. Next, click on the ‘cat1 flying-b’ costume and use the name
box at the top to rename it to ‘right’ (Figure 4-17).

5Figure 4-17: Rename the costume as ‘right’

Right-click on the newly renamed ‘right’ costume and click ‘duplicate’ to create a copy. Click
on this copy to select it, click on the flip left-right icon , then rename it to ‘left’ (Figure 4-18).
You’ll finish with two ‘costumes’ for your sprite, which are exact mirror images: one called
‘right’ with the cat facing right, and one called ‘left’ with the cat facing left.

5Figure 4-18: Duplicate the costume, flip it, and name it ‘left’

Chapter 4 Programming with Scratch 79


Click on the Scripts tab above the costume area, then drag two switch costume to left
Looks blocks under your left arrow and right arrow Events blocks, changing the one under
the right arrow block to read switch costume to right . Try the arrow keys again; the cat now
seems to turn to face the direction it’s swimming.

when left arrow key pressed

switch costume to left

turn 15 degrees

when right arrow key pressed

switch costume to right

turn 15 degrees

when up arrow key pressed

move 10 steps

when down arrow key pressed

move -10 steps

80 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE

You might also like