Scratch is a block-based coding environment. Scratch is a tool that can be used by people of all ages. Especially teachers can teach coding logic to children or can create very good educational content with Scratch. Scratch enables other programming languages ​​to be done in a short time. Of course, you can not write all programs with you can do in other programming languages ​​with Scratch. Because its features are restricted.

In this article we will explain how to make a sample maze game with Scratch step by step.

The objective of the Adventure Maze Game is to find the output by following the right path in the maze within 120 seconds. There is a prize(cake) on the way out. You also need to overcome obstacles (ghosts) and collect rewards (stars) while advancing through the maze. If more contact with ghosts is established, health will decrease and the game will end. It is possible to eliminate the ghosts with a bullet.

Full of the game can be reached from the following link.

https://scratch.mit.edu/projects/269897337/



Step 1: Encode the characters movements

First, open a new project with your Scratch account and make the background color a darker color. We call it a dark color because the maze will be white, so you may experience difficulties in coding in the advanced stages.

If possible, choose monochrome floor. Before adding the maze, add the character and create the basic codes that will turn up, down, left and right, as shown in the screenshot below. In this way, you will be able to focus on the movements of the character and correct the errors.





With codes here,

Orientation of the character according to the direction keys.

Moreover, as you press the arrow keys, the characters costume will change and we will see it as if it were walking.

Here are two different images in the characters costumes.

The reason for using the blocks is to stop the progression of the character when it comes to the maze walls when we add a white labyrinth. You can think of it as an impact-response event.

Step 2: Addition and coding of the maze

We can add the maze after coding the character. You can search the maze as "maze png" from visual search or download the labyrinth used in this project and upload it to your own project.





With codes here,

The maze was taken to the center of the screen by clicking the flag. x = 0, y = 0 is the center point of the screen.

you-lost is the news that will be released when the time is up or the character is no longer alive. In below, we will add news release codes.

Stage 3: Addition and coding of ghosts

After encoding the labyrinth, you can add ghosts or any other kind of character you want after encoding. The purpose of adding these characters is to make the main character (your cat) feel hurt when they touch them. So we designed it as obstacles in front of the cat.





With codes here:

When the game ends successfully or the time is not enough, we will let the ghosts hide with the news that will be released in case of death.

We make the ghosts hide when bullet hit them. Thus, we will remove the obstacles in front of the cat.

Step 4: Adding and encoding stars and point variable

After the addition of ghosts, you can perform a star or gold insertion as a reward. According to the scenario of the game, the goal is to increase the score and at the and show as much as we collected.





With codes here:

When the game is finished successfully or the time is not enough, stars will be hidden with the broadcasts.

When the game is finished successfully, the number of stars collected will be displayed in the coordinate of the star we have set again. We are setting this up with the finished broadcast.

When the star touches the cat it will be hidden and increase the score.

Step 5. Add and encode ammo symbol and ammo variable

After the addition of the stars we can add and encode the ammunition symbol and ammo variable. The aim here is when touch the ammunition icon it will be hidden and the ammunition variable is increased. You must add and encode oneammunition sprite and clone after encode it or your work will be extended.





With codes here:

When the game ends successfully or the time is not enough, ammunition sprite will be hidden with the broadcasts.

When we touch the ammunition, we keep the ammunition hidden and increase the ammunition variable by 5.

Clicking on the flag sets ammunition variable to 20. So we will have 20 bullets at start.

Step 6: Setting the duration with the time variable on the scene, make setting the other variables appearing and hiding, and adjusting the sounds

After coding the ammunition, you can click on the scene to make the necessary codings. It will be provided to set the time to 120 seconds when the game is started, to decrease every second, to set the game sounds, to hide and hide the variables.





With codes here

When the game started, the variables were shown, the first scene was played, game music will beplayed until the game will over. (First you must add the music you want from the sounds section).

When the game was finished, the ending music(gong) will play and the variables will be hidden.

A time of 120 seconds was added to the game with the time variable. If the character can not finished in 120 seconds game will be lost.

Step 7: Adjusting the shotting

First of all we select the main character and press the space key to launch the shot broadcast with the following code block:





We then add the direction variable and make it 1 (right), - 1 (left), 2 (up), -2 (down) according to the direction we press on the main character. The reason we do this is to make the bullet move in the direction the cat is turning. We can make the direction variable a secret.









Then add the lead to be seen during the launch as a separate dummy and do the following coding.





With codes here

When we do not press the space button and the game ends, we ensure that the bullet is hidden.

With key space is pressed, if the ammunition variable is more than 0 the bullet will appear, rotates in the direction of cat, will be positioned on the cat (this happens suddenly) and the lead will jump at a certain rate and will be hidden at the end of the range and the ammunition variable will decrease by 1 each time.

When the bullet touches the maze, it will be hidden even though its range is not over.

Step 8: Coding events when the cake is reached

If our cat runs out of the maze successfully within 120 seconds and reaches the cake, we make the following coding to ensure that the star can be seen in the color changing way. And we encode the cat and the cake are brought to the center of the screen:





With codes here

Cake will hide when you-lost broadcast comes,

Set position of the cake when game stars.

If game finish successfully we set cakes position middle right of screen.

Step 9: Making the final coding for the cat

Our game is almost over. We are doing the following coding for the cat.





With codes here

When receive you-lost broadcast cake will be hide

When the game starts, health variable will be set to 100, score variable will be set to 0 and cat will be positioned at the stars position

When the game is finished successfully, the cat will be positioned middle left of the scene.

When the spacebar is pressed to release the bullet, we have released the shot broadcast.

Finally

This example game coding was made to give ideas. We can not say it was perfect. You not have to use these codes. You can use your algortihm.

You can make this game with more practical code solutions. You can add more features. We would like you to write your comments on this topic in the comments section.

When coding, it will be more efficient if you first create your scenario and go through a step-by-step solution.

Remember to take a break when you are tired of coding.

Have a nice day...