The Maze Game in Scratch Coding
A puzzle game with multiple paths that lead towards the goal is the Maze game. This creates twist and confuses the solver. A solver tries to find the right path to reach the goal.
The Maze games provide ample challenge for the solver and boost problem solving abilities by targeting patience, concentration and building memory. You must have solved many mazes in your life. So, today let’s learn how to make your own maze game using Scratch Software.
Make a Plan for The Maze Game
Let’s start by making a game plan. Let’s find out answers of these questions. What controlling system we would be using? What are the objectives? How we are going to make it more challenging? Do we need some variable for our the maze game?
Let’s start our the maze game
Let’s start development of our the maze game
Backdrop
As we are coding a maze game on scratch, so we need a simple maze image as our backdrop. Here, I am simply using a maze image from Google. Or you may draw your own maze by clicking at the paint brush at the backdrop section. I am using an already saved image by clicking upload backdrop from a file option.
Sprite
For our maze game we need three sprites. A main sprite that could be anyone of your choice, Home Sprite that indicates the target for the solver and the Oops sprite to show game is over.
Main sprite
Select a sprite from sprite library by clicking on monkey option.
I have selected BAT 1 as my sprite. Check the sprite size, it should be small enough to move easily through the wall of maze. Click on costume, select the shrink option and reduce the size by clicking on the sprite. Make sure to reduce size of both the costumes of BAT1.
Now we will create two more sprites with the Home and Oops!
Home sprite
Objective of the game is to Help the Sprite(BAT1) reach its home before the timer rans out. For this we will make a HOME sprite by clicking on the paint the sprite option and simply write Home.
OOPS! Sprite
Now, we want something to happen if we ran out of time. For this create a Oops! Sprite by painting.
Make A Variable
I am aiming to make my maze game more exciting by adding a timer. So the solver needs to be quick to find the right path before he runs out of time.
Go to Data & Blocks click make a variable and name it as Time.
Coding
Let’s start coding our sprites.
Coding for bat1
We want our main sprite to be controlled by pressing up, down, right and left keys for moving around the maze. We want our sprite to start from specific location. Make sure you are placing your coordinates correctly as per your maze. For this we will set a starting point for our sprite and use go to x: y: block.
Now code our sprite to move upward, downward, right and left when respective arrow keys are pressed. We want our sprite to change his costume whenever it moves. Our sprite should be able to sense the walls of our maze so that it won’t pass through the wall.
Create four if conditions for movement:
IF,
- Right arrow is pressed than change x by 3 and add next costume block
- Left arrow is pressed than change x by -3 and add next costume block
- Up arrow is pressed than change y by 3 and add next costume block
- Down arrow is pressed than change y by -3 and add next costume block.
Add colour sensing so that if it touches the wall of maze it should move back with the change and don’t crosses the wall. Create four color sensing if conditions for movements in all the directions. Here I am using the or Block from operators because my maze has two shades black and grey so it should be able to sense both the colors.
Create a nested if for color sensing in the same if for movement. Place all the four conditions in forever block as we want it happen always.
Complete code for bat1
Coding for home sprite
Home sprite is the end or goal for the solver to be at Home before the timer runs out. So first set the go to x:y location as per your maze.
Let’s make a code that whenever if home is touched by the Bat1 it should broadcast the message Thank You for Helping for 2 seconds and stop everything.
Coding for Oops!
For Oops! Set the time to 100. When the game starts we want oops to hide. Use forever block and use change time by negative 1 as after waiting for 1 second. Create if condition that if the time is equal to 0 then show Oops! And stopp all otherwise keep it hidden.