Maze
Name | Maze |
---|---|
Language | Scratch |
Age Level | AGE LEVEL (coach) |
Skill Level | SKILL LEVEL (coach) |
Materials | Scratch.mit.edu or Scratch Desktop |
Prerequisites | ACADEMIC PREREQUISITES (coach) |
Topic | ACADEMIC TOPIC (coach) |
Duration | DURATION (coach) |
Authors | Lisa Nasu-Yu, Jumana Amr, Denis Koterev, Alex Koterev, Jayden Tsai |
Last Updated | July 21, 2022 |
Summary
Create a maze game using scratch.
Navigate your sprite through the maze and reach the goal before time runs out!
Overview
- Maze Outline Sprite
Draw an outline for the maze using the Scratch art tools and create a sprite. - Motion
Navigate the sprite with your mouse and stop its motion when it is touching the maze walls. - Timer & Goal
Create a countdown variable and goal sprite to sense when the sprite has reached its destination. - Win/Lose
Create win/lose backdrops that appear at the end of the game.
What you’ll learn
- Events
- Control flow
- If block
- Forever block
- Variables
- Boolean operators
- Cloning
Instructions
Step 1: Maze outline
Our first step is to make a maze layout for our player to run through.
- Click on
Stage
in the bottom right to select the background of your game. - Click on the
Backdrops
tab/folder in the top left in betweenCode
andSounds
to open a list of all background images - Click on the
Line
tool to create a straight lines. - You can press the SHIFT key to help you make perfectly horizontal and vertical lines
- Make sure that all walls are the same color because the color of the walls is how we will implement collison to the walls.
Finally rename the backdrop to Maze.
Sample Maze
![](assets/SampleMaze.png)
Step 2: Sprite
The next step is to create a sprite.
A sprite is an image that can be coded individually to create content in your game.
Creating a Sprite
You can pick an existing sprite or create a new one. A new scratch project already comes with a sprite of the Scratch Cat.
- To create a new sprite hover your mouse over the blue circle with a white cat’s face on the bottom right of the screen.
- Click on the
Brush
to create an empty sprite or click on theMagnifying Glass
to select a pre-drawn sprite
Editing the Sprite
By clicking on the sprite and pressing on the costumes tap in the top left corner, you will be able to edit how your sprite looks.
Everytime you add something to your drawing it will be seperate from the rest of the drawing.
This means that if you want to edit something like rotation or size it might be difficult.
Grouping fixes that issue by keeping everything in proportion within the same group.
Highlight everything you want to be in a group and press the Group button.
Grouping in Scratch
![Grouping in Scratch](assets/grouping.jpg)
The sprite can be anything, just make sure it can fit through your maze.
Rename your sprite to Main.
Step 3: Motion
Once you are finished with making the sprite, you can now begin with the coding.
Coding in scratch reads from top to bottom. Blocks on top will run before the blocks under it.
Setting up
- Make sure that you are in your Main sprite and that you are in the
Code
folder/tab. - Navigate to the Events section and drag
when flag clicked
block to the right. This block will begin the program - Next is to set a starting position and rotation for the sprite
- Navigate to the Motion section and drag the
go to x:(0) y:(0)
block under the event block. X is the horizontal position of the sprite while Y is the vertical position. - Set
X
andY
to your desired starting position by clicking on the numbers - Next is to set the starting rotation of the sprite.
- In the Motion section find the
point in direction (90)
block and drag it to the bottom of your code. - Make sure that the number is 90
This will reset this sprite everytime the green flag is pressed.
Forever Block
- The
On green flag pressed
will only run once at the very beginning. For something to run forever you will need theForever
block. - The
Forever
block is found in the Control section, find it and drag it to the bottom of the code. - Notice how you can have blocks inside the
Forever
block. Anything the inside the block will be repeated forever. - You might also notice how you can not have anything under the
Forever
block. This is because the code will never be able to go beyond the forever loop.
Motion
Now to make the sprite follow the mouse
- In Motion drag the
Move (10) steps
block inside the forever block. This block moves the sprite a number of steps into the direction the sprite is looking at. - Clicking the green flag will make the sprite move to the right
- The sprite might be moving a little to fast, so try changing the sprites speed by changing the amount of steps it takes. 2 steps looks like a good speed.
- To make the sprite follow the mouse you need the
Point towards (mouse-pointer)
block found in Motion. - Drag this block inside the
forever
block but also above theMove
block since you want the sprite to look at the mouse before moving.
Solution
![Motion script](assets/png.png)
At this moment you might have definitely noticed that the sprite can move through walls.
Making the walls solid
Now that the sprite can move, it is time to make the walls solid
IF block
- You are ganna need the
if
block found in Controls to check if the player has collided with the wall. - You may have noticed a hexagonal slot right after the if. Any block that has a hexagonal shape can fit inside that slot.
- Any hexagonal block returns something called boolean
- Boolean is a variable that can either be true or false.
if
will only activate once the boolean block is true.
Coding
- Drag
if
under themove
block inside theforever
block - Go to the Sensing section and drag
touching color ()?
block into the slot. This block returns a boolean. - Click on the color to bring up color menu.
- Use the eye drop option at the bottom of the menu to select the color of the walls. This is also why it was important to make sure that the walls were the same color.
- Inside the
if
block placemove (10) steps
and change the amount of steps into -10. The sprite will take 10 steps backwards. - Inside the
if
block you can also dragsay (Hello!) for (2) seconds
. Change “Hello!” to “Ow!” and reduce the duration to 1 second. - The sprite will now say “Ow!” every time it touches the wall.
Solution
Step 4: Time and Goal
Variables
Variables are numbers that can change.
They are useful because they allow things to move and change mid game.
It would be hard to make a fun game where nothing can move or change.
The sprites positions are variables and so are booleans.
Create a Timer
After you have finished adding movement to the player, the next objective is to add a timer and goal.
- Navigate to the Variable section on the left, and create a new variable by selecting
Make a Variable
. - Name the variable as
Timer
and select the optionavailable for all sprites
. - Then, within your Main sprite, create an
when green flag clicked
event. - Navigate back to the variable section and drag the
set my Variable to 0
block underneath this event. - Change the variable to the
Timer
variable, and set its value to 25, representing a 25 second time limit. - Underneath the block which set the
Timer
variable to 25, add aforever
block. In this block, we will make the value of theTimer
variable decrease by 1 every 1 second. - Navigate to the Control section, and drag the control block
wait 1 second
into theforever
loop. - Afterwards, drag the variable block
change My Variable by 1
underneath thewait 1 second
block. Make sure to replace theMy Variable
variable with theTimer
variable, such that it currently changes theTimer
variable by 1. - To make the timer to count down, change the value within the newly added
change My Variable by 1
block from 1 to -1.
Solution
![Timer script](assets/Maze2.png)
Ending the game
Try running the project for 30 seconds by pressing the green flag.
Notice that after 25 seconds, the Timer
variable has a negative value. This is because the timer variable within the forever loop will indefinitely decrease every second until it is stopped. The next step is to stop the game once the timer reaches zero.
- Underneath the block which decreases the
Timer
variable by 1, add anif
block. - From the operators section, set the comparison block
=
as theif
block’s condition by dragging it into the hexagonal space beside theif
block. - The
=
block compares two number to see if they are equal. This block returns a boolean. - In the
=
block, add theTimer
variable on the left of the equals and 0 on the right. This will allow theif
block to check if theTimer
variable’s value is equal to zero. - From the Control section, drag the
stop: all
block into theif
block. This way, if the value of theTimer
variable is equal to zero, theif
block will stop everything.
Solution
Create a Goal
Great job on getting this far!
The next objective is to add a goal at the end of the maze. To do this, we need to create a new sprite that will allow us to check whether the player has reached the end of the maze.
- Create a new sprite for the goal.
- Change the sprite’s name to Goal
- Set the sprite’s
x
andy
coordinates to where you would like to place the goal. This can be done by dragging the sprite in the screen or manually inputting itsx
andy
coordinates in the panel above the sprite selection sheet. - Within the Main sprite’s forever loop, which was previously established to monitor the
Timer
variable, add anotherif
block underneath the previousif
block. - Navigate to the Sensing section, and drag the
touching
block into the new, emptyif
block. - Change the sprite within the
touching
block to the newly created Goal sprite. - From the Control section, drag the
stop: all
block into theif
statement. This way, if the player’s sprite touches theGoal
sprite, the if statement will stop everything.
Solution
![Timer script](assets/Maze4.png)
Step 5: Win/Lose
Next, to indicate to the player whether the game has been won or lost, win and lose screens will be used.
Creating Win/Lose backdrops
- Pick and customize existing Scratch backdrops or draw new backdrops for the Win and Lose screens
- Rename the backdrops Win and Lose
Switching Backdrops
- Go to the Main sprite code.
- Place the control block
switch backdrop to
above stop all in theif touching Goal
block. Change the backdrop to Win - Repeat for the Lose backdrop in the if
Timer = 0
block - Try the game out for yourself. Notice that after you complete the game, the player sprite and goal sprite remain after the backdrop is switched to both the win and lose screen.
- In the Main sprite, navigate to the Looks section and drag the
hide
block right before switching the backdrop to the win screen and the lose screen. - In the Goal sprite, navigate to the Events section, and select the event
when backdrop swithces to
. - Create Three of this event.
- Set the first event to trigger when the backdrop switches to Win and the second to trigger when the backdrop switches to Lose.
- Underneath both events, add the
hide
block from the Looks section.
Now, you have the player and goal sprites disappear when the backdrop switches.
- Now, try running the game.
- Notice the backdrop does not return to the maze backdrop after winning/losing and the Main and Goal sprites are nowhere to be seen.
- In the player’s sprite, Add a
switch backdrop
block andshow
block just below the sprite’swhen green flag clicked
block. Change the backdrop to Maze. - Go to the Goal sprite, and create a third
when backdrop swithces to
event. Set the condition to when the backdrop switches to Maze. - Add a
show
block from theLooks
section underneath this event.
Solution
![Goal Script](assets/Maze7.png)
BONUS!
Using the skills you learned you can add a collectable that the player has to pick up before being able to complete the game!
Creating a clone
- Create a new sprite called Collectable and make it anything you like.
- Create a new Variable called
Items
for all sprites. - Create an
when green flaged clicked
event and addhide
block underneath. - Add
create clone of myself
from the Control section. This will create a clone that will need its own code.
Coding the clone
- To add code for the clone we will need
when I start as clone
block from the Control section. - Set its position using the
go to x: y:
block. Have the collectable somewhere away from the Main and Goal sprites starting positions. - Also add the
show
block because we want to see the clone. - Add a
forever
block with anif
block inside. - Have the
if
condition to be if touching Main sprite - inside the
if
block havechange variable by 1
block from the Variables section. - Make sure that the variable is set to
Items
and make it change by -1. - Finally add a
delete this clone
from Control beneath thechange variable by 1
block.
At the start of the game the Collectable sprite will create a clone that will disapear when touched. It will reduce the number of Items
to zero, indication that there is no more collectables to collect.
Solution
![Collectable Script](assets/Maze9.png)
Implementing
We are not done just yet! The Main sprite only checks if you have touched the Goal. We need to make it so that it also checks if you have collected the Collectable
- Go to the Main sprite and remove the
touching Goal
block from the if. - In the Operations add the
and
block into the empty slot. This block will only return true if both conditions are true. - Return the
touching Goal
block onto the left slot of theand
block. - Add an
=
block onto the right slot. - Inside the
=
block make it so that it comparesItems
to zero.
Your Done! The game will only show the win screen if the player is touching Goal and Items
is equal to zero!
Solution
![Collectable Script](assets/Maze10.png)
Conclusion
Full Solution
Main sprite Code | Collectable sprite Code
:– | :–
![Main script](assets/Maze11.png) | ![Collectable script](assets/Maze9.png)
Goal sprite Code | Game
![Goal script](assets/Maze7.png)|![Game](assets/Game.png)
Completed game at https://scratch.mit.edu/projects/410818157/editor
Quiz
Test your skills with a quick quiz!
Maze Quiz
- What is a Variable
- What is a Boolean
- What will
3 = 2
block return - How does a
if
block activate
Quiz Solutions
1. A **variable** is number that can change
2. A **boolean** is a variable that can only be **true** or **false**
3. It will return **false**
4. `if` block will only activate if a **boolean** condition is **true**