Adding scripts to the player sprite

The player sprite in Hunger Run is relatively simple. Its main action is to jump to avoid lethal food.

Prepare for lift off

Open the player sprite and verify that the player sprite has two costumes: Marco and Polo as shown on the following screenshot:

Prepare for lift off

Engage thrusters

We will create codes for the player sprite to do the following:

  • To initialize when the game starts
  • To fall down
  • To jump
  • To end the sprite

Perform the following steps to initialize when the game starts:

  1. Start with a when I receive <game_start> block.
  2. Check whether to be Marco or Polo. The user selects the character at the beginning of the game. Add an if player_name = () then () else () block; in if player_name=(), enter Marco. If the player's name is Marco, add the switch costume to <marco> block. Inside else (), add the switch costume to <polo> block accordingly.
  3. Enter 120 in the set <jump_steps> to () block.
  4. Enter 0.5 in the set <jump_wait_time> to () block.
  5. Enter 8 in the set <fall_speed> to () block.
  6. Enter 0 and 80 in the go to x: () y : () block respectively.
  7. Enable show.
    Engage thrusters

Perform the following steps to fall down:

  1. Start with a when <green flag> clicked block.
  2. Enable hide.
  3. Add a forever block. In the forever block, add a repeat until touching <Brick>? block. In the repeat until touching <Brick>? block, add a change y by () * fall_speed block; enter -1.

Perform the following steps to jump:

  1. Start with a when <space> key pressed block.
  2. Enable the change y by jump_steps block.
  3. Enable the wait jump_wait_time secs block since sprite will continue to jump. This is to wait between each jump to prevent player from jumping too high.

Perform the following steps to end this sprite:

  1. Start with a when I receive <game_over> block.
  2. Enable the stop <other scripts in sprite> block.

The finished scripts are shown together in the following screenshot:

Engage thrusters

Objective complete – mini debriefing

In this section, we created the code for the player sprite to initialize when the game starts, fall down, jump, and end the sprite.

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

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