Adding scripts to Shield and Shield Life

Shield protects and is also protected by the Spaceship. Each time the enemy sprite collides into it, Shield will lose one grid of power, displayed by Shield Life.

Adding scripts to Shield and Shield Life

When the power is gone (all red), Shield is destroyed and the game is over.

Adding scripts to Shield and Shield Life

Shield Life displays the shield power grid by changing its costumes, each at a power grid damage level. The technique to display damage level with costumes is to match the costume number with the number of hits. Scratch costumes start from one, instead of zero, so costume#1 depicts zero hit, costume#2 for one hit, and so on.

Adding scripts to Shield and Shield Life

Prepare for lift off

Shield Life has two local variables: num_shield_hits and num_max_hits. The number_shield_hits variable tracks how many hits the Shield sprite has suffered, and the num_max_hits variable tells the maximum number of hits that Shield can take before its complete destruction.

Engage thrusters

The following steps are used to build the scripts for the Shield sprite:

  1. Add the when <green flag> clicked block, but before the player starts to play, just hide (using hide).
  2. The next script starts with when I receive <game_start>. First, add switch costume to <normal>, then go to x: () y: (); fill in the values 7 and -110 respectively. Finally, add show.
  3. To define the behavior during gameplay, start with when I receive <shield_hit>, make it real by adding switch costume to <hit>, wait <frame_rate> secs, then switch costume to <normal>.
  4. If the shield is destroyed, the Shield sprite would receive the shield_destroyed message. Therefore, add when I receive <shield_destroyed>, switch costume to <destroyed>, and stop <other scripts in sprite>.

The final script looks like the following screenshot:

Engage thrusters

Next, we will create the scripts for the Shield Life sprite.

Before the game starts, this sprite will hide. At the start of the game, it initializes its local variables.

The following steps handle the <green flag> click and game_start messages:

  1. Add the when <green flag> clicked block, but before the player starts to play, just hide (using hide).
  2. The next script starts with when I receive <game_start> message. First add go to x: () y: () with the values -51 and -120, then set <number_max_hits> to () with the value 7, and set num_shield_hit to () with the value 0. Next, add switch costume to <0 hit>, then go to front, and show.

During the play, these sprites process four broadcast messages: shield_hit, shield_destroyed, life_power_up, and level_up.

The following steps build the required scripts:

  1. The first script starts with when I receive <shield_hit> and change num_shield_hit by () with the value 1. Then, add switch costume to (<num_shield_hit>+()) with the value 1. Add an if () then condition block. Check whether the number of hits the shield has taken is greater than the number of maximum hits it can, by using the <num_shield_hit> is greater than <num_max_hits> - 1 block, and then add the broadcast <shield_destroyed> block.
  2. The last script starts with when I receive <life_power_up>, restore the power grid and add set <num_shield_hit> to () with the value 0. Then, update the look using switch costume to <0 hit> and show.

The finished script will look like the following screenshot:

Engage thrusters

Objective complete – mini debriefing

We added scripts to the Shield and Shield Life sprites. The Shield sprite makes the game more dynamic and interesting, whereas the Shield Life sprite displays the life statistics visually and in a more lively manner.

Classified intel

During the game, a gamer needs to track not only the power level as shown in the Shield Life sprite but also the score. Space Age displays the score on a scoreboard using three sprites, one for each digit. They are complete sprites that work right out of the box. The Hundredth sprite displays the hundred's place, the Tenth sprite displays the ten's place, and the One sprite displays the one's place. Just like the Shield Life sprite, each scoreboard sprite has costumes that matches the shown values, as shown in the following diagram:

Classified intel

To display numbers from 0 to 9, all the three sprites have costumes ranging from costume#1 to costume#10, with costume#1 displaying the number 0 and costume#10 displaying the number 9.

For example, to display the score 257, the Hundredth sprite would switch to costume 3, the Tenth sprite to costume 6, and the One sprite to costume 8.

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

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