Adding scripts to the food sprite

For each platform, we will create clones of food sprite. To make the game more interesting, the food clones are randomly placed on its assigned platform and scroll with that platform. If the player sprite eats or touches bad food, then the player dies and the game ends. For all other food types, eating food will earn the player one point.

Prepare for lift off

Before diving into the scripts, we need to take a look at the food sprite costumes, as well as their scrolling mechanism.

The costumes of the food sprite are grouped into bad and good food, with bad food at the front of the costume list. The good_food_start_cos_idx variable defines which costume index is the starting index of good food costumes. We will use good_food_start_cos_idx later in the scripts to determine whether Marco eats a bad food sprite.

As shown in the following screenshot, if good_food_start_cos_idx is set to 3, then food sprites with Red Mushroom or Green Mushroom costume would be bad. Food sprites with all other costumes are good.

Prepare for lift off

To make the game harder, increase the value of good_food_start_cos_idx, say to 4. Then donut would be considered bad as well—if it tastes so good, it has to be bad, right?

Prepare for lift off

Engage thrusters

We will create scripts for the following:

  • To handle the <green flag> click
  • To handle the level_start message
  • To initialize the look and location
  • To scroll
  • To handle collision with the player

Perform the following steps to handle the <green flag> click:

  1. Start with a when <green flag> clicked block.
  2. Next, let's initialize the food variables: good_food_start_cos_idx, new_grid_idx, num_food_per_grid, and num_costumes.
    • The variable good_food_start_cos_idx is the starting costume ID of the good food group.
    • The variable new_grid_idx is for the next platform index to be used for a new clone.
    • The variable num_food_per_grid is the number of food clones to create for each platform.
    • Finally, the num_costumes variable is the number of costumes the Food sprite has.
  3. Enter 3 in the set <good_food_start_cos_idx> to () block.
  4. Enter 0 in the set <new_grid_idx> to () block.
  5. Enter 4 in the set <num_food_per_grid> to () block.
  6. Enter 8 in the set <num_costumes> to () block.
  7. Then, enable hide because we only want the clones on stage, not the main sprite.
    Engage thrusters

Perform the following steps to handle the level_start message:

  1. Start with a when I receive <level_start> block.
  2. Add a repeat until ( new_grid_idx > grid_count) block.
  3. Inside the repeat until ( new_grid_idx > grid_count) block, add a repeat num_food_per_grid block.
  4. Inside the repeat num_food_per_grid block, add a create clone of <myself> block.
  5. Place a change <new_grid_idx> by () block following the repeat num_food_per_grid block; enter 1.
    Engage thrusters

Perform the following steps to initialize a clone's look and location when it starts:

  1. Start with a when I start as a clone block.
  2. Set my grid index to the new grid index using the set <my_grid_idx> to new_grid_idx block.
  3. Add a set <my_loc_in_grid> to pick random () to () block; enter -240 and 240 respectively.
  4. Set the startup location in the grids for this clone using the set <my_start_loc_in_grids> to ((my_grid_idx * grid_length) + (my_loc_in_grid)) block.
  5. Drag out a if () else () block.
  6. Update it to be a if (my_grid_idx < bad_food_start_grid_idx) block. If this true, then add the then and switch costume to (pick random good_food_start_cos_idx to ()) blocks; enter 8. The costumes with index ranging from good_food_start_cos_idx to num_costumes are good. The variable bad_food_start_grid_idx controls whether bad food should show in this grid. For example, if the value of bad_food_start_grid_idx is 2, then bad food will show from grid 2 and up. This is to tweak the Hunger Run difficulty level. If bad_food_start_grid_idx is low, bad food would show sooner, thus the game, harder.
  7. If the condition in the if (my_grid_idx <bad_food_start_grid_idx) block is false, then add the else and switch costume to pick random () to () blocks; enter 1 and 8 respectively. The costume selection set includes bad food.
  8. Finally, go to the starting location in the grids. Enter -120 in the go to x: my_start_loc_in_grids y: () block.
    Engage thrusters

Perform the following steps to scroll with the platforms:

  1. Start with a when I start as a clone block.
  2. Add a forever block.
  3. In the forever block, add the set x to curr_scroll_amount + my_start_loc_in_grids block.
  4. To check when this clone has gone out of the viewable area, add an if () then () block. Then, update it to be a if <abs> of x position > () block; enter 230. Then, add the hide, else, and show blocks accordingly. Instead of 240, we use 230 to avoid clones getting stuck at the edge because their position never gets larger than 240 or smaller than -240. You can test it out on your own by changing the value to 240 to see the undesirable "sticking" effect.
    Engage thrusters

Perform the following steps to handle collision with the player:

  1. Start with a when I start as a clone block.
  2. Add a forever () block. Then, inside the forever () block, add an if touching <Player> ? then () block.
  3. Inside the if touching <Player>? then () block, add an if costume # < good_food_start_cos_idx then () block.
  4. Inside the if costume # < good_food_start_cos_idx then () block, add a broadcast <bad_food_eaten> block. In other words, if the player sprite eats a bad food item, then the food clone sends out a bad_food_eaten message.
  5. Place an else () block and inside it, add a play sound <point> block. Then, place the change game_score by () and broadcast <score_updated> blocks; enter 1. In other words, if the player sprite eats a good food item, then they earn one point.
  6. Finally, enable the delete this clone block to wrap up collision handling.
    Engage thrusters

Objective complete – mini debriefing

We have created the code to handle the <green flag> click, to handle the level_start message, initialize the look and location, scroll, and handle collision with the player. To test the scrolling, you can build a block as shown in the following screenshot, and double-click on it to start auto-scrolling:

Objective complete – mini debriefing
..................Content has been hidden....................

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