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.
If you're using a tablet or iPad without a physical keyboard, you won't be able to use keyboard keys like the arrow keys in your project. Instead, we'll add simple on-screen buttons (using sprites) that you can tap to do the same things. This keeps your project working great! Just follow these steps wherever the lesson talks about pressing a key.
First, add a new sprite for your button:
Now, program your button sprite to make things happen when you tap it. Here's how it works for different situations:
Example 1: If the lesson uses a key to trigger an action on a specific sprite (like making something move, turn, or jump)
Instead of code like this on your target sprite:
when [left arrow v] key pressed
change x by (-10) // or any action
Or this:
if < key [left arrow v] pressed? > then
change x by (-10) // or any action
end
Add this code to your new button sprite:
when this sprite clicked
broadcast [do action v]
Then, on your target sprite, add this to receive the message:
when I receive [do action v]
change x by (-10) // or any action
Tap the button on the screen, and the action will happen, just like pressing the key! Use a unique broadcast name for each different action or key.
Example 2: If the lesson uses a key to change a variable (like adding to a score or setting a value)
Instead of code like this:
when [space v] key pressed
change [score v] by (1)
Or this:
if < key [space v] pressed? > then
change [score v] by (1)
end
Add a new button sprite. Then, put this code on the button sprite:
when this sprite clicked
change [score v] by (1)
(If the variable is "for this sprite only," make sure it's set to "for all sprites" so the button can change it.)
Tap the button, and the variable changes, no message needed since it's something shared!