To make use of our generated maze, we will need to include a character the player can control. We'll use the default Scratch cat as the protagonist and add all the required control script to make it move through the maze.
If we haven't left it on the stage from the start, we have to add the Scratch cat as a new sprite as shown in the following steps:
The Scratch cat will be loaded for use in the game. Next, we will add a script to it to make it interactively controlled by the player.
Let's first set up the broadcasts that will trigger the cat sprite to take proper actions. We need two of those events. The first one is very simple and will be repeated in all game objects as follows:
This makes sure all other sprites are hidden when the tileGenerator sprite runs. They will remain hidden until we call them to do something later. The cat sprite will be activated right after the maze is created.
With a function, you can describe a certain operation that is repeated many times during the program. Instead of describing the steps required each time they are needed, just point to the function and tell the computer to perform those actions. Using a function can save space in the running program, because we set specific sets of actions aside as separate scripts. This will also make the general flow of the program easier to understand.
findStartPosition
.Note how a new starting block (with a curved top) will appear called findStartPosition. This is the start of the script where we can write all the steps the function should take as shown in the following screenshot:
We want to place the cat in an open hallway and not in a wall. To test if the cat can move to a certain position, we will move it and check if it then collides with a wall. If it does, we will move it another tile segment and test again. We repeat this process until we can place the sprite. Now perform the following steps:
We can then add the basic movement controls. As we have to keep in mind that the cat shouldn't be allowed to walk through walls, we have to perform a check before each move. This check will be the same for each step we take.
We will create another function to define this repeated collision check. Regardless of the key pressed or the direction the cat will move in, the check will stay the same.
To set up the keyboard controls, we create four-key pressed script, as we have done in earlier examples. For each directional key, we point the cat in the right direction as follows:
When the cat is facing the correct direction, we can make it try to take a step forward. We create a function to describe this move attempt. If the way is clear, the cat will move. If it's blocked by a wall, the cat will stay where it is. Perform the following steps:
takeStep
and click on OK without selecting options.Another starting block will appear called define takeStep. This is the start of the script where we can write all the steps the function should take. Perform the following steps:
We could leave it at that point, because the cat is now in a new valid position. However, the movement will look very jumpy this way. So we will improve that by adding a few scripts to create fluid movement which are as follows:
That completes our main player control scripts. The cat sprite can be moved freely through the corridors of the maze but it won't be able to cross a wall.
With this step, we have probably completed the most difficult part of the script. We used the new Make a Block option to create functions that make script a little easier to read and can save work or excessive copy-pasting when creating complex operations.
The beginnings of a game can be visible now; the cat can be moved through the maze.
3.144.31.163