Example 3: A Simple Game - Raspberry Pi User Manual

Hide thumbs Also See for Raspberry Pi:
Table of Contents

Advertisement

C H A P T E R 1 0

Example 3: A Simple Game

Using Scratch for simple animation is one thing, but the software also allows users to read
inputs from the keyboard to introduce interactivity . By combining some simple animation
controls to the previously described program, you can create a simple game—and, at the
same time, introduce the concepts of sprite collision, if statements and input.
For this example, start a new Scratch project—remembering to save the previous example, if
you haven't already done so—and begin by dragging a
area . This time, rather than telling the code blocks to execute when the flag icon is clicked, go
to the Control block palette and drag a
block .
As the name suggests, the
user—in this instance, the spacebar being pressed—and uses that as the trigger for execut-
ing a list of blocks . The block works at any time—if you press the spacebar now, the sprite
will obey its instructions and move 10 steps to the right .
The
when space key pressed
block in another important way: it can be customised . Click the down-arrow button next to
the word
to see a list of all the keys the block can watch, and then select
space
from the list to change the block into a
arrow
A game in which the player can move in only one direction isn't much fun, so drag a new
when space key pressed
list—you can only have a single trigger block—so start a new list somewhere further down .
As before, use the down-arrow button next to the word
ing it into a
when left arrow key pressed
to Motion mode and connect a
block before changing it to read
key pressed
If you press the left and right arrow keys now, you'll see that the cat moves according to your
input (see Figure 10-7): pressing the left arrow moves the cat 10 steps to the left (although,
as far as Scratch is concerned, it's moving minus 10 steps to the right), and pressing the right
arrow moves the cat 10 steps to the right .
A N I N T R O D U C T I O N T O S C R A T C H
when space key pressed
when space key pressed
block also differs from the
when right arrow key pressed
block into the Scripts area . This can't link to the existing block
block . Finally, switch the block palette back
block beneath the new
move 10 steps
move -10 steps
block to the Scripts
move 10 steps
block above the
block looks for input from the
when [flag icon] clicked
to customise the block, turn-
space
when left arrow
.
143
move
right
block .

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents