Hardware Reference
In-Depth Information
Controlling the Direction and
Movement of the Adventurer Sprite
An important part of any computer game is being able to control the movement of a
sprite using keys or buttons. In Scratch you are able to create more than one script for
a sprite or for a stage, which can be run in parallel with each other. hese multiple
scripts running at the same time are called
threads
in computing. You need to create
a number of scripts for the Adventurer sprite to be able to control the sprite's move-
ment.
1.
Underneath the irst script you created to set the start position and health vari-
able, add a new
when
clicked
control block.
2.
Next add a
forever if
block. Notice that this block has a hexagon shape in it.
his is designed so that you can add extra blocks, such as operators or sensing
blocks, to add a condition. his means that the blocks contained within the
forever if
loop will only run if the condition is met. For this project, click
Sensing in the Blocks palette and add a
key right arrow pressed
block by
dragging it with your mouse and placing it inside the hexagon shape. his creates
a
conditional
statement. Notice that you can change which key is referenced by
using the drop-down menu on the block. his allows you to set diferent keys for
diferent directions of movement, as you will do later in this project.
In computing, a
conditional
statement is one in which an action will be taken
only if a certain condition is true. For example, in the block you create in Step 2,
the
forever
part of the
forever if
block will loop the sequence of instructions
contained within its structure only if the right arrow key on the keyboard is
pressed.
3.
Now add the motion block
point in direction
inside the
forever if
loop and set it to 90, which will point the sprite to the right.
4.
Underneath this block and still inside the
forever if
loop, add the motion
block
move 0 steps
and change the value of steps to 20. See Figure 3-17 to see
how the script should look at this point.
5.
Test that this script works by clicking the green flag to start all the scripts, and
then pressing the right arrow on the keyboard.
6.
Create three more scripts like this one for the left, up, and down keys, changing
the values to move the Adventurer sprite in the correct direction for each arrow
key. Save and test your ile.