Scratch Game
60 mins
370 points
What you need:
  • Computer/laptop

Maze Game

In this game students program their character to navigate their way through a maze using the arrow keys on their keyboard.

This message will be visible to everyone. Send

    This code along has finished! ⌚

    You can watch it back by playing the video!

    The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

    1 - Open the Scratch project

    We have created a Scratch project that has the maze already added to it.

    Open this project by clicking on this link

    2 - Add the beetle sprite

    Add the Beetle sprite from the sprite library. Once you have added it, you will see it in the stage area.

    3 - Shrink the beetle

    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: 0 y: 0  block to do this. Add the following code to the beetle sprite:

    when green flag clicked set size to (15) % go to x [-175] y [-55] //these are the coordinates for the start of the maze

    The values for x and y are the coordinates for the start of the maze.

    4 - Program the arrow keys to move the beetle

    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.

    Add the following code to the beetle sprite:

    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


    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.

    5 - Stop the beetle going through the walls

    Now that you've tested moving your beetle around you may have noticed that it can walk through the walls of the maze! To stop it doing this, we're going to sense when the beetle touches the maze sprite and if it does, move it back 10 steps.

    Add this code to the beetle sprite underneath the go to x: -155 y: -75 block:

    when green flag clicked set size to (15) % go to x [-155] y [-75] //add the code under here forever if < touching [maze v] ? > then move (-10) steps end end

    6 - Improve the game?

    That's it, you have coded a game!! 😀🙌 

    Have fun playing it or have a think about how you could improve the game and make it better by adding to or changing the code? Have you any ideas?

    Here are some ideas for improving the game:

    Add a finish area
    Edit the maze backdrop to have a small green circle at the finish. Program the beetle to say "You did it!!" using a say block when the beetle touches the colour green.

    when green flag clicked forever if < touching color [#2b9348]? > then say [You did it!!] for (2) seconds end end

    No mistakes!
    Instead of moving the beetle back 10 steps when it touches a wall, move the beetle all the way back to the start by using a go to x: 0 y: 0 block.

    go to x (-155) y (-75) // the start of your maze

    Add levels
    Download other maze images and add them as sprites. When you complete the first maze, hide it and how the next maze and re-position the beetle at the start. Use the hide and show blocks.

    hide show

    Join our club 😃

    Create your profile, earn points & badges and learn how to code!

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

    Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more