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.
We will create scripts for the following:
Perform the following steps to create the code to respond to the game start and create one brick for each grid:
0
using the set <new_grid_idx> to () block.0
using the set min_grid_idx to () block.480
and the grid count to 0
using the set <max_scroll_amount> to (() * ( grid_count - () )) block.1
in the change <new_grid_idx> by () block.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
.
Perform the following steps to create code to initialize a clone's look and location:
0
using the set my_loc_in_grid to () block; enter 0
.1
. We add 1 to my_grid_idx because the Scratch costume number starts at index 1
but the grid index starts at 0
.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. Perform the following steps to create code to scroll automatically:
-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.Finally, perform the following steps to create the code to end the game:
18.191.237.79