Shooting those baddies!

Now that we have enemies, we need to create a way to get rid of them. Let's create a weapon for your player character, which he can fire at the enemies to remove them.

At the end of this project, we will also write a script so that the enemies can hurt the player.

Prepare for lift off

For the weapon, we will again search for a sprite from the Scratch library. With a few changes, this can be turned into a spear for the diver to use.

  1. Click on the choose sprite from library icon.
  2. Browse the Things category to search for a suitable projectile.
  3. The Magic Wand looks like a useful option so we pick that.
    Prepare for lift off
  4. Change the name of the sprite to Spear.
  5. Next, open the Costumes editor to make some changes to the picture.
  6. Check whether the editor is in Vector Mode.
  7. First, we use Select to select the image and then use Ungroup.
  8. Then, we can use Select to select the yellow lightning bolts individually and delete them by clicking on the Delete button.
  9. We change the colors to match to that of the diver's more closely. Change the shaft to orange, the tip to yellow, and choose a dark orange or brown color for the outline. Use the Color a shape tool to apply the changes.

The modified spear is now ready for duty.

Prepare for lift off

Engage thrusters

We will allow the player to fire a spear from the diver's position when pressing the Space bar. When the spear hits an enemy, the enemy will be removed immediately (and possibly the player will earn a point). But we will work on that later.

  1. Switch to the Scripts tab and start a new script with a when <green flag> clicked block.
  2. We will use the cloning technique, so let's first click on hide to hide the spear.
  3. We also set the size to 30% in the set size to ()% block to make it to scale with the diver and starfish.
  4. Next, we add a forever loop that will contain the cloning process.
  5. Inside the loop, we add a go to <Diver2> block so that the spear sprite will always follow the diver sprite.
  6. We also enter the value 90 in the point in direction () block to make the spear always face right.
  7. We add an if () then condition to check for player input.
  8. We will check when the key is pressed using the key <space> pressed? block.
  9. If this is true, then we add a create clone of <myself> block.
  10. Finally, enter the value 0.5 in the wait () seconds block and wait so that there is a little delay between the creation of each clone.

The following screenshot shows the final script:

Engage thrusters

To set the clones in motion, we create a second script as follows:

  1. We start with a when I start as a clone block.
  2. We will enable the show block to show the clones which were previously hidden.
  3. Next, we add a repeat until () loop.
  4. For the condition, we choose touching <edge>.
  5. So we can delete this clone when it touches the right edge of the stage using the delete this clone block.
  6. Inside the loop, we place a move () steps block to make the spear move forward; enter the value 5 in it.

The following screenshot shows the final script:

Engage thrusters

Test the game briefly to see whether the spear behaves as we expect it to. The spear should start from the player position and then move in a straight line to the right until it reaches the edge and is removed.

We can hold down the Space bar, but because of the wait block placed after the clone block, a spear will only be created after every 0.5 seconds.

Now to make the enemies respond to being hit by the spears, we make an addition to their script. This addition should be placed inside the repeat until () block of the when I start as a clone script since we will want to constantly check for a hit as long as the enemy clone is around.

  1. We add an if () then condition.
  2. We will check whether an enemy clone is touching the spear by using the touching <Spear> block.
  3. If that is the case, then we will delete this clone immediately by using the delete this clone block.
  4. This script is same for all the three enemy types, so we will add it to all the three enemy clone scripts. Remember that you can drag-and-drop a piece of script on a sprite to copy it.

The following screenshot shows the final script:

Engage thrusters

Objective complete – mini debriefing

When we try the game again, the enemies can now be destroyed by the spears.

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

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