Adding a knight

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.

Prepare for lift off

We will use the Knight sprite from the Scratch library.

  1. Click on the Choose sprite from library icon.
  2. We search for the Knight sprite. It's in the Fantasy category.
  3. We click on the sprite to select it, and click on OK to use it in our game.
    Prepare for lift off

Engage thrusters

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.

  1. We drag both the createMaze listener script and the takeStep function from the cat to the Knight sprite to make copies there.
  2. Then, we click on the Knight sprite to switch to its Scripts editor.
  3. To the createMaze listener, we add a set size to () % block and type in 12. This makes the knight fit the maze.
  4. At the end of this script, we add a delete this clone block.
    Engage thrusters

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.

Engage thrusters

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.

  1. Grab the if statement with the cursor and drag down to separate it from the script.
  2. Drag the script part to the left and drop it in the toolbox to remove it.
  3. Alternatively, we could also right-click on the part and choose delete. This also removes the script.

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.

  1. We create a new script starting with a when I receive <startGame> block.
  2. Then, we add repeat ().
  3. We fill the slot with the level variable to repeat as many times as the level we want to play.
  4. Then, we place the sprite with a go to x:() y:() block.

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.

  1. For the x position, the formula will be: tile width * random tile in row – (half the stage width – half the tile width). This translates to (60) * pick random (0) to (7) – (210).
  2. For the y position, the formula becomes: tile height * random tile in column – (half the stage height – half the tile height). This translates to (60) * pick random (0) to (5) – (150).
  3. When that's settled, we can use the create clone of <myself> block.
  4. Next, we wipe the xySave list clean with a delete <all> of <xySave> block.
  5. We use the add x position to <xySave> block.
  6. We also use the add y position to <xySave> block.

The completed script will look like the following screenshot:

Engage thrusters

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.

  1. We obviously begin a new script with when I start as a clone.
  2. Then, we show the sprite using the show block.
  3. We add a repeat until loop.
  4. There we check whether the clone is touching <explosion>?
  5. Until that's the case, we turn in a random direction with a turn <clockwise> () degrees block.
  6. We limit the clone to the cardinal directions by entering 90, 0, and 3 respectively in the () * pick random () to () block.
  7. Then, we call the takeStep function to actually move the clone.
  8. We close the script with a delete this clone block, which triggers after a clone is hit by an explosion.
Engage thrusters

The scripts are now complete and the knight can wander about the maze looking for a suspicious-looking cat.

Objective complete – mini debriefing

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.244.250