Adding scripts to Spaceship

The Spaceship sprite can move and fire ammo at the enemies. To do so, we need to create codes to do the following:

  • Start
  • Move
  • Shoot
  • Response
  • End
Adding scripts to Spaceship

Engage thrusters

Now we are ready to equip the Spaceship with scripts, so it can be controlled.

To create the script that executes upon game initialization, perform the following steps:

  1. Start with a when I receive <game_start> message block.
  2. Move to the bottom center of the stage and enter 0 and 100 in the go to x: () y: () block.
  3. Switch the costume to normal using the switch costume to <> block.
  4. Enable go to front and show.

The following screenshot shows the final script:

Engage thrusters

Horizontal movement

For movement, we will create two more blocks using More Blocks, one for the horizontal movement and another for the vertical movement. The More Blocks feature lets the users group the commonly used codes in custom blocks, making the code shorter and easier to read. In terms of computer programming languages, More Blocks are like methods or functions. Each More Block can have zero or several input parameters. Similar to the variables in a mathematical function, input parameters are plugged into More Blocks to produce the desired result. For example, one can have a More Block named Jump that will jump num_jump times, num_jump being the input parameter. At runtime, if num_jump holds a concrete value such as 3, then the Jump block would jump three times. For horizontal movement, we are going to create a More Block named Move Horizontally, which when executed, would move the Spaceship horizontally, as shown in the following screenshot:

Horizontal movement

To create code for horizontal movement, perform the following steps:

  1. Select the More Blocks tab and click on the Make a Block button.
    Horizontal movement
  2. On the New Block dialog box, click inside the empty More Block and type Move Horizontally. Expand the Options tab and click on the Add number input: button.
    Horizontal movement
  3. Then, enter the value of direction for the boolean input parameter. The boolean datatype has only two possible values: 0 (false) or 1 (true).
    Horizontal movement

    You should see the new More Block as shown in the following screenshot:

    Horizontal movement
  4. From the Operators tab, drag out the <?>* <?> block and drag the direction block to make the <x position> + (<direction> * <move_steps>) block visible.
    Horizontal movement
  5. Build the go to x: ( <x position> + ( <direction> * <move_steps> ) ) y: <y position> block.
    Horizontal movement
  6. Add this script to define the direction using the define Move Horizontally <direction> block.
    Horizontal movement

Let's add scripts to move the Spaceship when a user hits the right or left arrow key as follows:

  1. Start with when <right arrow> key pressed. Move to the right via Move Horizontally <1>.
    Horizontal movement
  2. Start with when <left arrow> key pressed. Move to the right via Move Horizontally <-1>.
    Horizontal movement

Vertical Movement

To write the code for the vertical movement, we will also create a custom block.

Vertical Movement

The steps to write the code for vertical movement are as follows:

  1. Create a custom block named Move Vertically.
    Vertical Movement
  2. Right-click on the body of the Move Horizontally block and select duplicate.
    Vertical Movement
  3. Update the copy so that it is go to x: () y: (); the slots are filled as shown in the following screenshot:
    Vertical Movement
  4. Then, attach the go to x: () y () block under the Move Vertically definition block. The resulting custom block should look like the following screenshot:
    Vertical Movement
  5. Start with when <up arrow> key pressed. Move to the right via Move Vertically () with the value 1.
    Vertical Movement
  6. Start with when <down arrow> key pressed. Move to the right via Move Vertically () with value -1.
    Vertical Movement

The Spaceship should shoot ammo when the Space bar is pressed. To do so, when the Space bar is pressed, a clone of the Spaceship Ammo would be created to fly straight up until it hits the edge or an enemy sprite.

Vertical Movement

The steps to build the shooting code are as follows:

  1. Start with the when <space> key pressed block.
  2. To prevent the game from hanging up when the user hits the Space bar too many times, we will add a timeout between each round of firing. Let's wait for 0.05 seconds by using the wait () secs block.
  3. Now add the code to fire the ammo by using create clone of <Spaceship Ammo>. Note that we still cannot shoot the Spaceship Ammo. This is not possible until we add the scripts to the Spaceship Ammo sprite later in the project.

The finished script should look like the following screenshot:

Vertical Movement

For the sake of game responsiveness, the Spaceship sprite does not continue to check whether it has collided with the enemy sprites or their ammo. Instead, the checking is done by the enemy sprites and their ammo. When notified, the Spaceship reacts to the collision by simply changing its costume.

The steps to build the script are as follows:

  1. Start with the when I receive <ship_collided> block.
  2. Then, use the switch costume to <hit> block.
  3. Wait for 0.5 seconds for the user to see the Spaceship explode (using the wait () secs block).

The following screenshot shows the final script:

Vertical Movement

The Spaceship sprite terminates when it receives the game_over message.

The steps to write the code are as follows:

  1. Start with the when I received <game_over> block.
  2. Then, add the hide block.
  3. Finally, add the stop all block.

The finished script should look like the following screenshot:

Vertical Movement

Objective complete – mini debriefing

Now, try testing the Spaceship sprite by moving it horizontally using the left and right arrow keys. Then, test the vertical movement using the up and down arrow keys. Additionally, you can test each script block by double-clicking on it. When a script block is being run or executed, its outline would turn faint yellow.

Objective complete – mini debriefing

Classified intel

In this section, we introduce More Blocks. I hope you found creating a custom block fairly easy. However, deleting a custom block is not as intuitive. There is no "delete this custom block" available when right-clicking on the block definition. To remove a custom block, first remove all the references to it. Then, drag the definition block to the tool area in order to delete it.

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

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