Skip to main content

MIcroBit: Crashy Bird

Objective

Roboteers will code a flappy bird game using arrays and loops.

Essential Keywords


Attention (5 Min.)

In this lesson we will develop a new game using accelerometer!

Learners Guidance 

Step 1: Add the Bird to the Game

First, we are going to add a sprite for the bird from the Game menu and make it blink.

Step 2: Make the Bird fly

Before creating the code for the game actions, let’s first add some controls so that we can move around. We’ll control the bird by pressing the A button to go up or the B button to go down.

Step 3: Generating obstacles

This is where things will start to get interesting. We’re going to randomly generate obstacles. We’ll keep all obstacles inside the array. All obstacles will have a single hole for the bird to fly through.

First, create an array of obstacles which will hold all of the obstacle sprites.

Now generate vertical obstacles consisting of 4 sprites and 1 random hole. Create new variable called emptyObstacleY. Using pick random, generate a random number from 0 to 4 and store it inside emptyObstacleY.

Using for loop, iterate from 0 to 4. For every coordinate not equal to emptyObstacleY create and add obstacle sprites to the end of the obstacles array.

Now with every micro:bit restart you should see different autogenerated vertical obstacles.

Before continuing, make sure that obstacles are generated randomly and that the bird is moving up and down.

Step 4: Make obstacles move

Access each obstacle using a for element loop (iterate over the obstacles array) and decrease the obstacle X coordinate by 1. Right click on the value block and rename it to obstacle ; then drag that obstacle block on top of sprite in the change x block.

Obstacles should move towards left every second.

Step 5: Make obstacles disappear

Make obstacles disappear after reaching leftmost corner. Iterate over all obstacles, delete the obstacle sprites where the X coordinate equals 0, and remove them from the obstacles array.

Step 6: Generate more obstacles

At the moment, our code generates just one vertical obstacle. We need to put obstacle generation code into the forever loop so that it keeps generating more and more obstacles.

Now our screen is full of moving obstacles. Create some spaces between generated obstacles. Let’s introduce a ticks variable to count how many iterations the forever loop has done and execute obstacle creation only if ticks is divisible by 3.

Step 7: Game Over

Right now nothing happens when the bird is hit by obstacle. Fix this by iterating over the obstacles array and checking if any obstacle sprite coordinate equals the bird coordinate.

Congrats! You have now built a working game!

Explore (25 - 30 Min.)

Time to customize! See If you can add score variable that displays the score after you lose the game, or make the obstacles move faster as the game progresses.

4. Share (Min.)

Peer review!

Students can share their work with peers for constructive feedback and then use this feedback to revise and improve their work.

5. Closure (Min.)

Review content and summarize key points.

6. Evaluate

Students reflect on their performance. Give positive and constructive feedback on each student

Essential Questions 

How are the obstacles generated? How is their information stored?

What is an array?