Adding enemy patterns

We will create two more enemy types that follow different attack patterns. The starfish in our first sprite are moving in a straight line one after the other. For the second enemy sprite, we will create a script that causes that enemy to spread out, making it a bit harder to avoid. For the third enemy, we will write a script that makes this enemy type a bit unpredictable, moving in a wavy pattern and turning unexpectedly.

Prepare for lift off

Before we begin scripting, we need two more enemy sprites, which we will create using the following steps:

  1. Right-click on the Starfish sprite and choose duplicate. Do this twice to create two more enemy sprites, including the enemy sprite scripts.
  2. To differentiate between the three enemy types, we will recolor the second and third sprites.
  3. For the second sprite, we select a blue color and fill the body of the sprite with it.
  4. For the third sprite, we select a yellow color and do the same.

The sprites are automatically renamed as Starfish2 and Starfish3 as shown in the following screenshot. This is fine for our purposes, so there is no need to manually change the names.

Prepare for lift off

Engage thrusters

We can now modify the script to create varied movement patterns. The basic idea stays the same. We generate a few clones over time that move across the screen independently based on their instructions.

Let's change the script for the blue enemy sprite to make the clones spread out instead of following behind each other:

  1. Go to the Scripts tab of Starfish2.
  2. Add a turn <clockwise> block to the repeat loop, just before the create clone of <myself> block.
  3. Take the pick random () to () block, which has the values -30 and 30 respectively, out of the point in direction () block and place it inside the turn <clockwise> block.
  4. Remove the + operator block.
  5. Make sure that the direction the sprite will initially point to is -90 degrees (towards the left side).
  6. Change the number of repeats to 3 in the repeat () block.
  7. Also change the wait time before the next clone is created to 0.3 in the wait () secs block.

The following screenshot shows the final script:

Engage thrusters

These enemies will spawn a bit slower and in smaller groups than the red enemies. With a small addition to the script, they also have a very different movement pattern. The blue enemies will start moving at different angles. This makes them spread out as they advance.

The yellow enemy will also take the red enemy script as a basis. To make this enemy move even more erratically, we will place a turn command inside the clones instead of placing it inside the original sprite.

We will focus our attention on the clone script first.

  1. At the start of the repeat until () block, place a turn <clockwise> block.
  2. Now we drag the pick random () to () block, which contains the values -30 to 30 respectively, from the point in direction () block in the <green flag> script to the newly created turn block inside the clone script.
  3. Change the numbers to -10 and 10 to decrease the angle variation.
  4. In the <green flag> script, we will also change the number of repeats and the wait time just like we did for the blue sprite.
  5. Also, remove the remains of the calculation we plundered and make sure the sprite initially points to -90 (left) degrees.
  6. Also, change the movement speed in the clone script from 5 to 4.

The following screenshot shows the final scripts:

Engage thrusters

As a final step in this project, let's make the enemies an actual threat to the player. When an enemy touches the player sprite, it should be game over for the player. Now that we have created all enemies, we can easily check for collisions with them.

  1. Go to the Scripts tab of the diver sprite.
  2. We replace the forever block with a repeat until () condition block. But make sure to set aside the keyboard control script for reuse.
  3. In the condition slot, we check whether the diver is touching any of the starfish sprites. This requires two or operators, so we have three condition slots; one for each sprite.
  4. Reset the keyboard controls inside the repeat loop.
  5. To finish off, when the condition is met, this script should stop all scripts by using the stop <all> block.

When a starfish touches the diver, the game will freeze immediately by effectively ending the game.

Engage thrusters

Objective complete – mini debriefing

We now have three similar yet distinct enemies to challenge the player. Run the game with the <green flag> button to see all of them go at once.

This project shows that a few simple changes can have big results for your game. It's often not necessary to invent a completely new script for each sprite. Clever copying and reusing of what you already made can save you a lot of time.

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

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