Adding scripts to Spaceship Ammo and Enemy Ammo

The Spaceship sprite fights enemy sprites with Spaceship Ammo, and enemy sprites attack with the Enemy Ammo sprites.

Prepare for lift off

Spaceship Ammo has one local variable, speed. The speed variable is how fast this sprite moves. Upon creation, it flies out of the Spaceship sprite and moves straight up until it hits an enemy or the top edge of the Stage.

Prepare for lift off

The Enemy Ammo sprite is very similar to the Spaceship Ammo sprite and is different only in direction, hit target (the sprites it can hurt), and costumes.

Prepare for lift off

Enemy Ammo moves down, but Spaceship Ammo moves up. Also, Enemy Ammo can hit the Spaceship and Spaceship Shield sprites, but Spaceship Ammo can hit enemy sprites. More interestingly, Enemy Ammo, contrary to Spaceship Ammo, is level-aware: it changes the costume based on the current game level. Due to the similarity between Enemy Ammo and Spaceship Ammo, the Enemy Ammo scripts are included in the starter project.

Engage thrusters

When the game starts, the Spaceship Ammo sprite will only initialize its variables but not participate in the game yet.

To create the code to initialize upon receiving the game_start message, perform the following steps:

  1. Start with the when I receive <game_start> block.
  2. Use set <speed> to (); fill in the value 100.
  3. Use set <power_boost_on> to (); fill in the value false.

The final script should look like the following screenshot:

Engage thrusters

The following steps create the code for each Spaceship Ammo clone:

  1. Start with when I start as a clone.
  2. Add the go to <Spaceship> block.
  3. Then add the go to front and show blocks.
  4. Add a bit of zest with play sound <laser>.
  5. Add an if () then () else condition block to check whether the power_boost_on condition is true.
  6. If power_boost_on is true, this ammo can destroy multiple enemies. So it uses the change y by (<speed> * <frame_rate>) and repeat until <y position> is greater than () blocks; fill in the value 180 in the repeat until block.
  7. If power_boost_on is false, the ammo would move up repeatedly until it hits either an enemy or the ceiling. In other words, change y by (<speed> * <frame_rate>) and repeat until (<y position> greater than (180) or touching <Rock>? or touching <Robot > ? or touching <Monster>? or touching <Mother Ship>?.
  8. After hitting the ceiling or an enemy, delete the clone using delete this clone.

The final script should look like the following screenshot:

Engage thrusters

If the game is over, each clone will delete itself. Add the when I receive <game_over> and delete this clone blocks.

Engage thrusters

Next, let's add the scripts for the Enemy Ammo sprite:

  1. When the game starts, don't show the sprite. Wait for the creation of the clone. Then, add the when green flag is clicked and hide blocks.
  2. Now let's add the script for a clone. Start with when I start as a clone.
  3. Add the switch costume to <game_level> block.
  4. Add the go to x: (pick random () to ()) y: () block; fill in the values -240 and 240 for x and 180 for y. Then, add the go to front and show blocks.
  5. Now add set speed to (pick random () to ()); fill in the values 10 and 15.
  6. The effect of repeatedly falling down or moving straight down on the y axis is brought using repeat until ((y position < -180) or (touching <Spaceship>)) and change y by ((-1) * (speed * frame_rate)).
  7. After getting out of the repeat () loop, this ammo clone has either touched the stage bottom or the Spaceship. Add if touching Spaceship and then broadcast ship_hit.
  8. Finally, delete the clone using delete this clone.

Besides hitting the bottom or the Spaceship, an Enemy Ammo clone is also deleted when the current level completes. Add when I receive <level_up> and delete this clone. The finished script looks like the following screenshot:

Engage thrusters

Objective complete – mini debriefing

We finished creating the scripts for Spaceship Ammo as well as for Enemy Ammo. Now you can test each script by double-clicking on the script. If you double-click on the when I start as a clone block once, one Spaceship Ammo clone will show at the bottom and flies straight up until it hits the top. For Enemy Ammo, the clones will show at the top and fall down until it hits the bottom or a Spaceship sprite.

Classified intel

Global variables are shared by all the sprites, and local variables are for one sprite or clone. You can think of global variables as values that are posted on the bulletin board for all to see. On the contrary, the local variables are like a letter. When a sprite is cloned, that clone contains its own copy of that letter.

Classified intel
..................Content has been hidden....................

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