Microbit Flappy Bird

by favr46 in Living > Toys & Games

7551 Views, 10 Favorites, 0 Comments

Microbit Flappy Bird

Flappy Bird With MicroBit

Flappy Bird is a mobile game developed by Vietnamese video game artist and programmer Dong Nguyen, under his game development company, Gears.

I personally loved this game and trice making it with Arduino and Lcd display. Recently, I got my hands on Microbit and decided to code this game on Microbit using Microsoft's Make code block coding environment.

Here, I'm sharing the steps to code this game. I tried to keep it simple. Follow the steps and you will be able to make it as well.

Let's start....

Supplies

  1. Computer/Mac with an internet connection
  2. Micro:Bit (Optional, You can use the simulator to play.

Declaring the Variables

F7.jpg

Before we get started, we will declare some variables. These variables will be used to store information to be referenced and manipulated when we program this game and thus allow us to code more easily.

For creating variables search for Variables under the Blocks Toolbox and then click on Make A Variable. Write the name of the variable in the New Variable Name dialogue box that pops up and clicks OK.

We need to create 7 Variables for our game:

  1. Bird
  2. EmptyObatacleY
  3. Index
  4. Obstacle
  5. Obstacle2
  6. Obstacle3
  7. Ticks

On Start

f2.jpg
XY.jpg

Look for the Set to block under variable in the block toolbox. Drag this block to the workspace and attach it to the On Start block. Use the drop-down icon to select the ticks variable and fill 0 to the empty space. Now, drag one more set to block for the obstacle and click on Advanced and click on Arrays. Drag an empty array block and place it inside the set obstacle to block.

To create a sprite for the Bird, place a set Bird to block under the previous block and then go Click on Advanced and click on Game. Drag create sprite at x:__ and y:__ and attach it in the set Bird to block. Fill X = 0 and Y = 2. Lastly, from the Game toolbox, find the block to set the blinking rate of the Bird sprite and set it to 300.

Button a and B

f3.jpg

We have two buttons on our Microbit which we will program to move our bird up and down. Here, Up means that the sprite will change its position by -1 and for Down, the sprite will move by +1.

So, we will now drag two On Button pressed blocks from the Input toolbox to the workspace. Use the drop-down arrow to select buttons A or B. Now look for change y by block in the Game toolbox and snap it under in button A pressed. Enter -1 in the empty space. Repeat this to program button B of the microbit.

Forever

f4.jpg

In the forever block, we will program the obstacles. For this, we shall add a couple of codes inside the forever loop.

Deleting the obstacle wall Once it reaches the End(leftmost grid, X=0)

The first thing we will do is to check when we should delete the walls and obstacles and we should do it when it has reached the end where X is equal to zero. So let's start by grabbing a while loop from the Loop toolbox and dragging it inside the forever loop. Inside the while loop we should check for two conditions, the first condition is that if there are still obstacles in our obstacle array and the second thing is to check their location. Now, to check two things at once we will use an and boolean. Look for an and boolean and drag it to where the "true/false" was. So now we can have two conditions and the only way this will be true is if both conditions are true.

Next, we will compare the length of the obstacle array by grabbing the equality logic side under comparison and dragging it as our first condition into our first box. Now, the length of the array will be our first value for this boolean and here we're just going to change equals to greater than so while this array size is greater than zero and the value of their x value is zero then we can start removing the obstacles/sprites in our optical array.

Now, the second condition should be checking rather the position at X is zero, so now we go to array and we can use get value and again as we have to compare this to some other value so we should go to logic comparison and grab the comparison block into our second argument for the end and here we put the list as the first argument.

Next, let's change the list to the list we have, which is an obstacle and now we should change this value so let's do our second condition for our while loop. Go to logic and grab another comparison and for the first comparison we're going to be using the X value inside of our array obstacle, so drag the sprite at X and here instead of using sprite, we will use the values in our array. So we're going to go over to array list get value at and we're going to replace that with our sprite. Next, change the list to the obstacle.

So while this whole statement is true we want to be deleting our obstacle element as soon as it reaches X = 0(left most verticle grids). So to do that we go to Game and we can use the delete block and we're going to drag it into the while do. Next, look for get and remove value at block under arrays and snap it with delete block. Use the block which says obstacle so this while loop will make sure that by the time the wall reaches the end(at X=0) it will be deleted.

Shifting The Obstacle:

So now let's add the code so that it will start shifting to the left as soon as it is created. For this, we will have another loop but this time it's going to be a for loop. Snap the for loop block right underneath the while loop and change the value in the list to obstacle and for values, we can use our second variable for obstacle, which is obstacle 2. and we can drag it in there now we should change the obstacle 2's position of x by negative 1 so it shifts to the left so here we can go to game sprite change x5 and it's our sprites we're going to change it to obstacle2. Next, we will drag the block that will change obstacle2's X position by -1 from the game toolbox.

Creating Obstacles:

To code, the obstacles, we will start with the if-then condition block. Here we will check for the condition that if the remainder of ticks divided by 3 is equal to zero or not because if the remainder equals zero that means we are in the multiple of three and then, therefore, we create our wall. We will then go to variable and use the set variable and set our variable named emptyobstacleY. The value we're going to set it to is going to be a pick random number between 0 and 4 from the Math toolbox. Snap it inside the if-then condition block.

Next, we're going to use a for loop. Notice that it has already been using index. So we want them to start from zero to four which is what it starts with as default. So we don't have to change that inside the for loop we want to check the index is not equal to our emptyobstacleY and if it is we simply add a value to it if it's not we do nothing.

So to check for that we use an if loop logic. Grab the if true then and put it inside of our for a loop. The condition we're checking for is rather indexed is not equal(to create space between obstacles for the bird to fly through) to emptyobstaclesY. To check this, grab the boolean for not equal. The first value should be index and the second should be our emptyobstacleY. Now we are going to use another array function and the one we're going to use is add value to the end.

So here we can use this list at value to the end and change the list to our obstacle and the value will be adding creating a sprite at that location X = 4, so that it starts up all the way to the right and for Y we should use the index.

Game Over: When Bird hits the Obstacles:

The final step in the game is to check if the obstacles have hit our bird in that case we should signal the game over and have the user restart the game. To do this we're going to use a for loop to go through the elements in our obstacle array. To check if it's the same location as our bird we will go to loops and again we are going to use the for elements and use the obstacle3 variable that we have declared and the list is going to be the same as an obstacle.

We will check whether obstacle3's location is the same as our bird and check stuff we use the logic conditionals. For this grab the if statements. Here in the if statement, we're trying to check if the X value of obstacle3 equals the X value of bird and we check the Y value of obstacle3 and see if it matches with the obstacle.

To see if it matches with the Y of the bird, here in the condition we check if the X and Y coordinates of obstacle3 are equal to our bird's X and Y values. If the X and Y value both matches that means it is at the same location and therefore we have lost the game.

We will go to logic and grab the boolean and, and put it in our condition. so the first condition should check for the X values so we will go down to logic and grab the comparison equality block to check the Y value of our obstacle3. Then go to the game, here we have sprite sprites x replace sprite with obstacle3. Drag sprite X again as our second equality and this time change sprite to the bird. On the right side of this end loop we're gonna use something similar, but this time we will compare the obstacle3 position at Y with the Bird's sprite at Y.

To indicate that the game has ended we go to the game and look for the block that says game over and then we will put it inside the ongoing block. This will conclude the game if the bird hits the wall.

Delaying and Spacing out the Obstacles:

Finally, we should add a pause delay of 1000 ms(1sec), so that there's a pause so that as you can see on the preview there's add the delay between each obstacle. So even with the delay if you look at the inputs we can see that at each forever loop we are creating obstacles and there is no space between the obstacles. To space out the obstacles, so that every three forever loops we create our obstacles, and to count how many times we have gone through the forever loop we use a variable that we declared already known as tick variable, which will count how many times we have iterated through this forever loop. As we want to increase tick by one, look for the change value by one block under variable toolbox and it right above the pause, inside of the if loop.

Complete Snapshot and Downloadable File

microbit-screenshot.png

Please click and enlarge the picture above to see the screenshot of the program.

Although this is game is ready to play but I would like to improve it a bit more. Here are some features that I would like to add to this game:

  • Add Levels to the game with the speed of obstacles increasing after some time.
  • Add Life
  • Add total Score
  • Add the classic Mario sound.

Here is the downloadable .hex file of this game. You can simply transfer it to your Microbot or tinker it by Importing it to the Microsoft make code.