Adding scores and power-ups

To prepare for the upcoming boss battle (next project), it's a nice touch to power up the player character. A power-up system also offers a better reason for shooting enemies apart from the need for survival.

Engage thrusters

We will assign a score value to each enemy type. The player can increase the score by shooting the enemy of the corresponding type. When the player has defeated enough enemies, the spear will be powered up in some way.

  1. We have three different enemy types, so we will first create the following three new variables to hold the scores. These variables are available for all sprites.
    • scoreRed
    • scoreBlue
    • scoreYellow
    Engage thrusters
  2. Click on the checkboxes in front of the new variables to make them visible on the stage. This way we can keep track of our scores.
  3. At the start of the game, we set the values of these variables to 0 as shown in the following screenshot. The best place to do this is inside the stage object.
    Engage thrusters
  4. Next, we will add a point to the correct variable when an enemy is defeated. We already built a script to check when an enemy is touched by a spear sprite.
  5. Just before we delete this clone, we only need to change the correct variable by 1 point in the change <scoreRed> by () block as shown in the following screenshot:
    Engage thrusters

It should be obvious that the scoreRed variable is meant for a red starfish. For the other two colors, we would use scoreBlue and scoreYellow variables respectively.

Now that the score values are increasing as enemies are defeated, we can use those values to increase the power of the spear. Each score value/color will have a different effect on the spear. These effects are as follows:

  • The scoreRed value will influence how quickly the player can shoot another spear
  • The scoreBlue value will determine when the spear will be upgraded to a new costume
  • The scoreYellow value will increase the number of spears shot simultaneously

Let's start scripting the scoring system into the spear sprite. We will tackle the scoreRed functionality first. As the scoreRed value increases, spears will be spawned at a faster rate. We put a limit of 30 on the value to prevent the spawn rate from increasing to absurd levels.

  1. We start this piece of script with an if () then () else () condition block.
  2. We use this block to check whether scoreRed is less than 30 by entering 30 in the scoreRed < () block. We now allow the script to calculate the spawn rate.
  3. If this is true, we calculate the spawn rate based on the scoreRed value as follows:
    1. First, divide the scoreRed value by 30.
    2. Then, deduct the result from a base value of 1.5.
    3. We wrap this calculation in a wait () secs block to cause the delay between spawns.

The order of the preceding calculation operators is very important. The operator blocks will be processed from the innermost block to the outer blocks. The result is the number of seconds we have to wait for a new spear to appear.

  1. Take the wait () secs block and place it inside the else bracket to create a minimum spawn time of 0.5 seconds when the scoreRed value exceeds 30.
  2. Place this piece of script where the wait 0.5 secs block was, right underneath the create clone of <myself> block.
    Engage thrusters

Next, we will handle the scoreBlue effect. For this effect, we need to draw a second costume as follows:

  1. We switch to the Costumes tab of the spear sprite and use duplicate to create a copy of the costume.
  2. We recolor the second costume in blue tones using the Color a shape tool.
  3. Naming is important here. We name the first sprite as basic spear. The second sprite will be called ice spear.
  4. Go back to the Scripts tab to write a script for switching between these two costumes.
  5. We start with an if () then () block to check the scoreBlue value. An else () block is not needed in this case.
  6. We will enter 29 in the scoreBlue > () block.
  7. Inside the condition check, we place a switch costume to <ice spear> block.
  8. We place this if () statement inside the forever loop. It's not relevant if the Space bar is pressed for this functionality.
  9. When we change the appearance of the sprite, we also need to make sure that it's set to its basic appearance at the start of the game. We add a switch costume to <basic spear> block at the start of the script.
    Engage thrusters

The script for the scoreYellow value is the most complicated. Not only do we need to create multiple clones when the score reaches a certain value, but we also need to reposition and angle the clones so they don't overlap on exactly the same spot.

When the score reaches 15, we will start creating two spears at once. We will angle them outwards a bit, so they move forward in a V shape.

When the score reaches 30, we will create three spears at once. One spear will move straight ahead, while the other two will move outward in a V shape.

  1. Let's grab another if () then () else () block.
  2. Place the create clone of <myself> block inside the else () bracket. This will be the default condition for creating one clone when all other conditions fail. This happens when the score is still smaller than 15.
  3. Inside the condition block, we check if scoreYellow is greater than 14 by entering 14 in the scoreYellow > () block.
  4. If that is the case, we will create two clones at an angle as follows:
    1. First, we enter 15 in the turn <counter clockwise> () degrees block to turn the sprite upwards a bit.
    2. Then, create your clone by using the create clone of <myself> block.
    3. Next, enter 30 in the turn <clockwise> () degrees block to angle the same distance downwards.
    4. Again create your clone by using the create clone of <myself> block.
Engage thrusters

We leave this script for a moment before we place it in its proper spot.

Now on to scripting what happens when the scoreYellow value reaches 30.

  1. Again, we need an if () then () else () condition block. The quick way to get what we want is to right-click on the piece we just wrote and then on duplicate.
  2. Then, we change the value to 29 instead of 14.
  3. We add another create clone of <myself> block at the start of the if () bracket to create a clone that will move straight ahead.
  4. We change the position of the other two clones to spread them out a bit using the follow steps. This way they won't overlap at the start of their movement, which looks a bit nicer.
    1. Add change y by () with value 10 to move the upward angled clone up a bit.
    2. Then, add change y by () with value -20 to move the downward clone down an equal distance from the original position.
    Engage thrusters
  5. Remove the create clone of <myself> block from the else () bracket.
  6. Replace it with the other if () then () else () condition script.

That completes our scripting for the scoreYellow value. The completed result will look as shown in the following screenshot. Make sure that all blocks are sorted in the correct order or the script might behave strangely.

Engage thrusters

Objective complete – mini debriefing

Test the script to see the scores increase as you shoot enemies. Also note how the behavior of the spear changes as you reach certain score values.

The following is a screenshot of the entire stack of scoring scripts we just built. It looks quite intimidating when viewed as a whole, but because we broke it down into separate steps, it became a lot easier to deal with.

Objective complete – mini debriefing

If you have trouble staying alive while testing this script, detach the stop <all> block from the diver script. This will prevent the game from stopping when the diver is touched by a starfish.

Classified intel

The order in which the condition checks for the scoreYellow value are placed is very important. Since the score is increasing from 0 upwards, we first need to check the highest value (> 29), which is the last condition to be achieved. When this condition is not met, the script will then check if scoreYellow has reached 15 yet. And if that's not the case either, it will default the basic, innermost, else condition.

If we switched these checks around, the >29 condition would never be reached because the >14 condition would resolve and throw us out of the loop before we reached the other condition check.

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

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