Creating a sword

Now, let's create an additional weapon to make it easier to get rid of the enemies lurking in the maze.

Prepare for lift off

We will create a sword sprite which we can draw ourselves.

  1. So, we start by clicking on the Paint new sprite icon.
  2. In the editor, we start by choosing a brown color and the Rectangle tool.
  3. We draw two rectangular boxes forming a T-shape. This will be the handle for the sword.
  4. Next, we switch to a gray color.
  5. We select the Ellipse tool and drag out a long, narrow, oval shape to form the blade. It doesn't look very sharp up close, but at the downscaled size, it will.
  6. If you like, you can decorate the sword some more with a pommel, gems or engravings.
  7. We name the sprite sword for use in the scripts.
    Prepare for lift off

Engage thrusters

The player won't have access to the sword right from the start. It will lie somewhere in the maze. So to use it, the player has to make an effort to go and get it.

  1. As the first step, we can again copy a createMaze listener script from another sprite.
  2. We then create a second script starting with a when I receive startGame block.
  3. We enter 5 in the set size to ()%block to make the sprite fit the maze. Perhaps your sprite should be slightly bigger or smaller depending on how big you made the drawing.
  4. We enter 45 in the point in direction () block to place it diagonally.
  5. Then, we use the show block to show the sprite.
  6. We place it on a random free tile with the go to x: 60 * pick random 0 to 7210 y: 60 * pick random 0 to 5150 block.
  7. Then, we can use the wait until touching <cat>? block.
  8. Next, we need to create a new list named items. This list will be available for all sprites.
    Engage thrusters
  9. In the script, we add sword to the items list.
  10. Then, we use the hide block to hide the sprite.
    Engage thrusters

When this script completes, the sword has changed places from inside the maze to the players inventory. When it's in the inventory it can be used as a weapon to defeat enemies. We will add that functionality now.

  1. We start another script with a when <s> key pressed block.
  2. Then, we make a check with an if... block. Only when this resolves will we take action on the key press.
  3. We check the if <items> contains sword block then we make the sprite go to <cat>.
  4. Then, we need to create a new variable to save the direction the cat is facing. We name this variable catDir. It needs to be available for all sprites.
    Engage thrusters
  5. We make the sword point in the direction using the point in direction catDir block, so it will be facing the same way as the cat sprite.
  6. Then, we make it move using the move 15 steps block so the sword isn't on top of the cat but a bit in front of it.
  7. We use the show block to show the sprite.
  8. Then, we use the wait 0.2 secs block to keep it visible for a short while.
  9. We use the hide block to hide the sprite again.
    Engage thrusters

We still have to change the catDir value to actually make it work. To do that, we add a few blocks to the cat scripts.

  1. We click on the cat sprite to view its scripts.
  2. To each of the key pressed scripts, we add a set <catDir> to... block after we have changed its direction.
  3. We fill the slots with the built-in direction variable. This saves the cat's current direction to the variable so it can be used elsewhere.
    Engage thrusters

The sword should now function properly. Let's play a game to test it. Firstly, we have to move the cat to the sword to get it. Then we can use the sword with the S key. At each key press, it should be stuck out in the direction the cat is facing.

Objective complete – mini debriefing

We have now added a second weapon to the game. We could already use the bomb to remove enemies, but that poses a danger to the cat because the cat can get hurt by the explosion as well. The sword doesn't have that problem and it can be used repeatedly a lot faster. The player has to get very close to an enemy though, so that still poses a risk.

Another benefit of adding this weapon is that it offers another goal to achieve before running for the exit. The player has to weigh the benefit of gaining the item versus moving to the exit straight away. His decision will rely mostly on how many enemies are in the way.

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

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