Mathematics Scratch
Enrichment
60 mins
Teacher/Student led
+75 XP
What you need:
Chromebook/Laptop/PC or iPad/Tablet

Maze - Artificial Intelligence

In this lesson we will program a sprite to figure out how to navigate a maze using random decision making and we will time the beetle to see how fast it can complete the maze.

Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Create a new Scratch project

    Create a new Scratch project and delete the cat.

    2 - Add the maze sprite

    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 centre of the stage area.

    If you cannot upload the maze image as a sprite, you can use the starter project here.


    3 - Add the beetle sprite

    Add the beetle
    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).

    Shrink the beetle
    As the beetle sprite is too large to fit in the corridors of the maze, we'll need to add the following code to the beetle to shrink it:

    whenclickedsetsizeto15%

    Position and point the beetle
    Once you have added this code, click the green flag to shrink your beetle and then drag it to the start of the maze (near the bottom left of the maze). Now add the following code to your existing code block so that the beetle always starts in this location and is pointed in the right direction.

    whenclickedsetsizeto15%add new code under heregotox0y0set these to the start of the mazepointindirection0 point upwards

    It's important to drag the beetle to the start of the maze before you add the go to x: 0 y: 0  block as when you add this block it's pre-populated with the coordinates of where the sprite currently is.


    4 - Edit the maze sprite

    We need something at the exit of the maze that we can detect when our beetle sprite gets there, so we're going to add a small green circle by editing the maze. 

    Click on the maze sprite in the sprite list below the stage area and then click on the 'Costumes' tab, this will open the sprite editor. To add a green circle follow these steps:

    1. Click 'Convert to Vector'
    2. Click the 'Circle' tool
    3. Choose a green colour
    4. Turn off the outline
    5. At the exit of the maze draw a circle by:
    6. - holding down the Shift button
      - and holding down the left click button
      - and dragging across to draw a circle
    7. Drag the circle to the exit of the maze

    If you make a mistake just click the back button (a rounded arrow pointing left).


    5 - Create a steps variable

    We're going to create and use a 'steps' variable for the amount of steps the beetle will take each time it moves. Create a new variable called 'steps' and add the following code:

    whenclickedsetsizeto15%gotox0y0pointindirection0add the code under heresetstepsto5hidevariablesteps

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    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 https://scratch.mit.edu
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more