21. Micro:bit Avoiding Bricks

21.1. Introduction

Let’s use micro:bit to make a game of avoiding bricks. It can control the spots by gravity to avoid the falling bricks. Once it touches the bricks, the game ends. Let’s see how it works。

21.2. Quick to Start


Materials


Nezha expansion board × 1

micro:bit × 1

Connections Diagram


  • 将micro:bit插入哪吒扩展板。

../../_images/case_18_03.png

21.3. MakeCode Programming


21.4. Principle:

The implementation logic is roughly divided into two parts, the small highlight part controlled by the player and the randomly generated obstacle part. For simplicity, here we set:

1.The small bright spot can only move left and right at the bottom of the screen

2.The length of the obstacle is fixed as three

3.The descending speed of obstacles remains unchanged

4.The game ends when the small bright spot hits an obstacle

5.Due to the small size of the screen, only one obstacle will appear at the same time. When one obstacle drops to the bottom, another obstacle will be generated again.

21.5. Implementation

Next is the implementation part. We need to use two infinite loops to implement the logic of the small bright spot and the logic of the obstacle respectively. 4 sprites (actually a variable representing the sprite) will be generated, one for the small bright spot, and 3 Obstacle sprites also need 2 variables, x and sudu, x is used to accept the generated random number and set the abscissas of 3 obstacle sprites, sudu is used to control the descending speed of obstacles, open the micro:bit online programming website (https://makecode.microbit.org/)

21.6. Start programming.

On start

1.Under the variable classification, use the variable module to set 6 variables, namely x, sudu, spirt (representing small bright spots), zhangai1 (representing the leftmost obstacle wizard), zhangai2 (representing the middle obstacle wizard), zhangai3 (representing the rightmost obstacle) Elf)

2.Set the value of x to the random number module to generate a random number from 0 to 2

3.Set the value of sudu to 600

4.Set spirt to the Create Wizard module under the game category, fill in the variable 2 for the x coordinate and 4 for the y coordinate to ensure that the initial state of the small bright spot is in the bottom middle of the screen

5.Set zhangai1 to create a wizard module, fill the x coordinate with the variable x, and fill the y coordinate with 0

6.Set zhangai2 to create a wizard module, fill the x coordinate with the variable x + 1, and fill the y coordinate with 0

7.Set zhangai3 to create a wizard module, fill the x coordinate with the variable x + 2, and fill the y coordinate with 0

../../_images/case_20_15.png

21.7. The bright spot part

1.Judge the acceleration in the x direction, if it is over 200, add 1 to the spirt x coordinate and pause for 200ms

2.Judge the acceleration in the x direction, if it is less than -200, subtract 1 from the spirt x coordinate, and pause for 200ms

../../_images/case_20_16.png

21.8. Obstacles part

1.The 3 obstacle sprites y coordinate minus 1 each time

2.Judge If spirt touches zhangai1 or spirt touches zhangai2 or spirt touches zhangai3, the game is over when it touches any one among them.

3.If the y coordinate of zhangai1 (or zhangai2 or zhangai3) is 4, regenerate a random number from 0 to 2 and set it to the variable x

4.Use the variable x to set the coordinate x of zhangai1 to the variable x and the y coordinate to 0

5.Set the coordinate of zhangai2 with variable x. The x coordinate is variable x + 1, and the y coordinate is 0

6.Set the coordinate of zhangai3 with variable x. The x coordinate is variable x + 2, and the y coordinate is 0

7.Pause variable sudu time

../../_images/case_20_17.png

Programme as the picture shows:

../../_images/case_20_18.png

Result

  • It can control the spots by gravity to avoid the falling bricks. Once it touches the bricks, the game ends.

../../_images/case_20_19.gif