We will add our first enemy to the maze. This will make it harder for the cat to move through the maze freely. The player has to choose his path wisely or risk getting caught.
We will use the Knight sprite from the Scratch library.
The knight will behave quite similarly to the cat sprite. It's allowed to move through the hallways, but it can't pass through walls. That means we can reuse a lot of the scripts we've already written for the cat.
12
. This makes the knight fit the maze.You might wonder about this last step. There aren't any clones to delete, and you would be right. There aren't any clones, not yet at least. However, we will soon write a script to create them.
However, first we will have a look at the copied takeStep function. You may have noticed that with the function xySave, the list has been copied as well. That's a good thing, because we will need that list. Note that both the cat and the Knight have their own personal version of the list. When created, it was set to be for this sprite only.
We don't need to make many changes to the function. The movement part works perfectly as it is. It's exactly the same way the cat can move. We only need to remove the last part of the script that deals with touching exit. The knight is not allowed to escape the level.
Now we have to call the function to actually make the knight move. We will also add an interesting feature based on which level we are playing. For each level, the number of knights will be increased by one. So level 1 will have one knight, level 2 will have two, and so on.
level
variable to repeat as many times as the level we want to play.We want to pick a random tile on the map, so we have to make a calculation based on a random number. Luckily, we designed the tiles in such a way that the middle segment is always free, so we won't have to check for collisions with walls.
The completed script will look like the following screenshot:
Remember how we already placed a delete this clone block at the end of the createMaze script? That was to remove any clones that will be created with this script during a game. We are now ready to write a script to move the clones.
90
, 0
, and 3
respectively in the () * pick random () to () block.The scripts are now complete and the knight can wander about the maze looking for a suspicious-looking cat.
We added an enemy that behaves in the same way as the player character. Due to this, we could reuse a lot of scripts, saving us some time. We still had to review each script to make some changes and to check we didn't leave any unwanted behavior, like a knight responding to the exit sprite.
3.145.191.134