Adding a ghost

Our next enemy will be quite different. It will be easier to script, but offers a bigger challenge to the player. This enemy will be a ghost that is allowed to move through walls freely. Therefore, it can pop up anywhere without restrictions.

Prepare for lift off

We first load a ghost sprite from the Scratch library. To do this, we perform the following steps:

  1. We click the Choose sprite from library icon.
  2. The ghost is also in the Fantasy category, just like the knight.
  3. There are two options. We select Ghost2, because it looks more scary.
  4. Press OK to add Ghost2 to the game.
    Prepare for lift off

Engage thrusters

Structurally, the scripts for the ghost will be the same as those for the knight and the cat. However, it doesn't need the takeStep function, because the ghost can just move all over the place.

  1. We can first copy the createMaze listener script and the startGame listener script from Knight. This will shrink the ghost to the right size for the maze and also sets up where it should be placed.
  2. Then, we only need to remove the two add () to xySave blocks, because the ghost doesn't require them. Other than that, these two scripts will be exactly the same as for the knight.
    Engage thrusters

So we can quickly move on to creating the clone script.

  1. We start again with a when I start as a clone block.
  2. Then, we first show the sprite again using the show block
  3. Next, we use the point in direction () block to determine which way the ghost will move.
  4. This direction can be completely random, so we fill in 1 and 360, in the pick random () to () block.
  5. Then, we will add a repeat until... loop.
  6. We check again for touching <explosion>? like we did earlier with the knight.
  7. As long as everything is well, we will use the move 2 steps block.
  8. As a precaution, use if on edge, bounce, so the ghost doesn't get stuck on the stage edge.
  9. We conclude this script with a delete this clone block for the case where the ghost does get hit.

That's our second enemy done.

Engage thrusters

Objective complete – mini debriefing

In this step, we see that more complex scripts don't always mean a more dangerous enemy. The script for the ghost is simpler than that for the knight. However, the ghost poses a much bigger danger to the player because it can move through the walls. If the player isn't careful, he/she can be easily driven into a dead end.

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

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