Coding Ireland STEM Report 2024 Have Your Say
Mathematics Scratch
Enrichment
60 mins
225 points
What you need:
  • Computer/laptop

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.

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


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:

when green flag clicked set size to (15) %

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.

when green flag clicked set size to (15) % //add new code under here go to x(0) y(0)//set these to the start of the maze point in direction (0) // 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:

when green flag clicked set size to (15) % go to x [0] y [0] point in direction (0)//add the code under here set [steps v] to (5) hide variable [steps v]

Join our club 😃

To view the remaining 6 steps and access hundreds of other coding projects please login or create an account.

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