Adding scripts to the Game Manager sprite

In Project 4, Space Age, we introduced the practice of adding a Game Manager sprite to handle the game initiation and termination, level up, as well as scoring. Keep the game logic in a centralized location such as Game Manager, which makes it easier to configure and update the game. The Hunger Run starter project also includes a Game Manager sprite.

Prepare for lift off

Verify that the Game Manager has costumes named Starter Screen and Level One.

Prepare for lift off

Engage thrusters

We will create the code to initialize the game and determine the game completion or termination.

Perform the following steps to initialize the system:

  1. Start with a when <green flag> clicked block.
  2. Enter 2 and 5 in the go to x: () y: () block respectively.
  3. Enable the switch costume to <Start Screen> block.
  4. Enable show.
  5. Enter 480 in the set <grid_length> to () block.
  6. Enter 0 in the set <game_score> to () block.
  7. Enter 0.2 in the set <frame_rate> to () block.
  8. Enter 10 in the set <grid_count> to () block. This number should be equal to or less than the number of the costumes in the brick sprite.
  9. Enter 3 in the set <bad_food_start_grid_idx> to () block. The bad_food_start_grid_idx variable is the platform number when bad food can start appearing. For example, if this variable is set to 3, then mushroom can appear starting from grid 3.
  10. Enter 0.75 in the set <scroll_speed> to () block.
    Engage thrusters

Perform the following steps to initialize the game:

  1. Start with a when I receive <game_start> block. The game_start message is broadcasted when the Start button is pressed.
  2. Enter 0 in the set <curr_scroll_amount> to () block.
  3. Then, enable hide.
  4. Enable the play sound <level_start> block.
  5. Then, broadcast the broadcast <level_start> message block.
  6. Enter 2 in the wait () secs block. This is to give the player a chance to take a breather before the fast-scrolling action.
  7. Next, broadcast the broadcast <start_scrolling> block. The brick and food sprites will respond by starting to scroll.
    Engage thrusters

To win the game, Marco or Polo has to finish the run without eating bad food. When the last frame has been reached, the brick sprite would send out the scroll_max_reached message. To handle the scroll_max_reached message, perform the following steps:

  1. Start with a when I receive <scroll_max_reached> block.
  2. Enable the switch costume to <You Won> block.
  3. Enter 24 and -24 in the go to x: () y: () respectively.
  4. Then, enable show.
  5. Next, broadcast the broadcast <game_won> block.
  6. Enable the play sound <triumph> until done block.
  7. Finally, enable the stop <all> block.
    Engage thrusters

Objective complete – mini debriefing

We have added scripts for the Game Manager sprite to initialize the game and determine the game completion or termination. You can now test Hunger Run and may the odds be ever in your favor.

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

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