This is a simple version of the Flappy Bird game for micro:bit. The objective is to direct a flying bird, which is moving continuously to the right, between sets of obstacles. If the player touches an obstacle, they lose. The purpose of this tutorial is to teach the basics of game sprites, arrays, and loops.
MIcroBit: Crashy Bird
Roboteers will code a flappy bird game using arrays and loops.
Attention (5 Min.)
In this lesson we will develop a new game using accelerometer!
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
4 and store it inside
Using for loop, iterate from
4. For every coordinate not equal to
emptyObstacleY create and add obstacle sprites to the end of the
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
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
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.)
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.
Students reflect on their performance. Give positive and constructive feedback on each student
How are the obstacles generated? How is their information stored?
What is an array?