Adding scripts to the brick sprite

Brick sprites are the building blocks that form the game platform. In this section, we will show how to clone the brick sprite to form the game platform. Moreover, we will make the platform auto-scroll to make the game faster-paced and more interesting. In Hunger Run, to keep it simple, we will start the game with one brick for each grid. Further, we will create all the bricks at game start to reduce game lag. For real world games with much larger grids and more sprites, the sprites may be created when needed for scalability.

Adding scripts to the brick sprite

Engage thrusters

We will create scripts for the following:

  • To respond to game start
  • To initialize a clone's costume and location
  • To scroll automatically
  • To end the game

Perform the following steps to create the code to respond to the game start and create one brick for each grid:

  1. Start with a when I receive <game_start> broadcast message block.
  2. Set the index of the new grid to 0 using the set <new_grid_idx> to () block.
  3. Set the index of the minimum grid to 0 using the set min_grid_idx to () block.
  4. Set the maximum scrolling amount to 480 and the grid count to 0 using the set <max_scroll_amount> to (() * ( grid_count - () )) block.
  5. Then, for each platform, create a clone and assign it the clone platform index. First start with the repeat until (new_grid_idx > grid_count) block.
  6. Inside the repeat until (new_grid_idx > grid_count) block, place a create clone of <myself> block. Then, enter 1 in the change <new_grid_idx> by () block.
    Engage thrusters

Before building the scripts, let's first look at how to find initial and current positions of the scrolling sprites.

Assume that your game grids look like the following screenshot, all the bricks start with x=0 inside their grid, thus point to the center of the grid. In the grids, however, each brick is at (my_loc_in_grid + 1/2*grid_width + grid_width*my_grid_idx). The Food sprite's initial position is computed in the same way. In the example that follows, brick 1 starts at x=480, and apple starts at x=680.

Engage thrusters

Perform the following steps to create code to initialize a clone's look and location:

  1. Start with a when I start as a clone block.
  2. Set my location in the grid to 0 using the set my_loc_in_grid to () block; enter 0.
  3. Next, set my start location in grids by using the set my_start_loc_in_grids to (my_loc_in_grid+my_grid_idx*grid_length).
  4. Set my grid index to the new grid index using the set <my_grid_idx> to new_grid_idx block.
  5. Add the switch costume to ( (my_grid_idx + ()) - my_grid_idx) block; enter 1. We add 1 to my_grid_idx because the Scratch costume number starts at index 1 but the grid index starts at 0.
  6. Then, enable the go to x: my_start_loc_in_grids y: platform_y_value and show blocks.
  7. Next, add a forever block.
  8. Inside the forever block, add the set x to curr_scroll_amount + ( my_grid_idx * grid_length ) block.
  9. Under the set x to curr_scroll_amount + ( my_grid_idx * grid_length ) block from step 6, add an if (<abs> of x position) > () then block; enter 350. Then, enable the hide else show block. The <abs> of x position > () block is a mathematical method that returns the absolute value of its input. You can try to use numbers other than 350, especially if you wish to create new brick costumes. This value should be between 240 (half of the stage or a grid) and 480. If this value is set too high, you will find that the sprites are "stuck" to the edge of the Stage and follow the player while scrolling. If this value is set too small, then the sprites would disappear suddenly.
    Engage thrusters

Perform the following steps to create code to scroll automatically:

  1. Start with a when I receive <start_scrolling> block.
  2. Inside the when I receive <start_scrolling> block, add a forever block.
  3. Enter -1 in the change <curr_scroll_amount> by (() * scroll_speed) block, and check whether the current scrolling amount is greater than the maximum scrolling amount using the if (<abs> of curr_scroll_amount > max_scroll_amount) block. When the maximum scrolling amount is reached, the player has reached the edge of the grids. If so, then broadcast the broadcast <scroll_max_reached> block.
    Engage thrusters

Finally, perform the following steps to create the code to end the game:

  1. Start with a when I receive <game_over> block.
  2. Delete the clone using the delete this clone block.
    Engage thrusters

Objective complete – mini debriefing

In this section, we created scripts to respond to the game_start message, initialize a clone's look and location, scroll automatically, and end the game.

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

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