In this game students program their character to navigate their way through a maze using the arrow keys on their keyboard.
Create a new Scratch project and delete the cat.
Go to the Scratch website using the link below and click on the 'Create' link in the blue bar at the top.
By default, each new project starts with the cat sprite already added. To delete the cat click on the x in the blue circle beside the cat in the sprite list.
Save the maze picture in this step to your computer and upload it into Scratch as a new sprite. Once it appears, drag it into the center of the stage area.
You can upload images and sprite files into your project. To upload a sprite follow these steps:
The new sprite will upload into your project and appear in the stage area.
Add the 'Beetle' sprite from the sprite library.
Drag the beetle to the start of the maze (the gap at the left of the maze).
To add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
As the beetle sprite is too big to fit through the corridors of our maze, we'll need to shrink it down. We also want to position the beetle at the start of the maze when we start the game, so we'll use a 'go to x [] y []' block to do this. Add the following code to the beetle:
when green flag clicked
set size to (15) %
go to x [0] y [0] //set these so the beetle appears at the start of the maze
The values for x and y will already be filled in for where your beetle is (at the start of the maze) so there's no need to change them.
Next we'll program the up, down, left and right arrow keys on your keyboard to move the beetle. Each of these keys will point the beetle in the appropriate direction and make it move 10 steps.
We're going to program the up arrow first and then duplicate and change this code for the down, left and right keys.
when [up arrow v] key pressed
point in direction (0)
move (10) steps
when [down arrow v] key pressed
point in direction (180)
move (10) steps
when [left arrow v] key pressed
point in direction (-90)
move (10) steps
when [right arrow v] key pressed
point in direction (90)
move (10) steps
Arrow | Direction |
---|---|
up | 0 |
down | 180 |
left | -90 |
right | 90 |
Once you have all of the arrow keys programmed, click the green flag and then test each arrow key to see if it moves the beetle in the right direction. If any of them are not working, check the code for that arrow to make sure it's correct.
Since your tablet or iPad doesn’t have a physical keyboard, you’ll use on-screen buttons to complete this task. Wherever the instructions in this lesson mention pressing a key, you’ll need to tap a button on the screen instead. So, while your steps are a little different, you’ll still be able to make everything happen in your project.
So for example, instead of doing either of these:
when [left arrow v] key pressed
move (10) steps
if < key [left arrow v] pressed? > then
move (10) steps
end
You need to add an on-screen button (like an arrow sprite) and use this code:
when this sprite clicked
move (10) steps
Now, just tap the button on the screen to perform the same action!