Posted on

Scratch Coding – Maze Challenge

The goal of this game is to move the sprite to the end of a maze, but beware there are obstacles all around.

Step 1: Setup

Start by adding a sprite, I’m going to make mine a bug:

I’ve duplicated my bug’s costume and added a red cross, this will be the sprite that I switch to when we hit the side of the maze.

We need a backdrop that will contain the walls or our maze:

Step 2: Sprite Movement

We want to have our sprite follow the direction of the mouse pointer, this way we can glide our character with our mouse.

We can use the following block to help us:

Since we always want to be moving towards the mouse pointer while the game is on, we will need to place this in a forever block.

Question: What happens if we have the following code?

To fix this, we need to make sure that the mouse pointer is at least 5 pixels away from the sprite. (we can use an if statement).

If we have hit the side of the maze, we need to show our crashed costume for 1 second and move the character back to the starting spot.

Step 3: Food

We need to add our food to the game:

The scripts for this food has to be always looking to see if the bug has touched it.

We will need a forever block and an if block that uses the sensing blocks.

If we touch the food, we can stop the background timer by calling the stop all block:

Step 4: Background

We should add a background timer to keep track of how long it takes to complete the maze.

We need to add a variable called ‘timer’.

In our forever loop we will change this value by 0.5, we will also add a wait block for the same amount of time. If we double click on the variable on the screen, we can make it smaller:

Extra:

Add new sprites that act to slow or speed up your sprite.

Since we can’t change the movement of the bug sprite from these new obstacles, we must broadcast a message to our bug sprite:

Then our sprite will listen for these events:

Code Solution:

Code for bug:

Code for the food:

Code for the backdrop: