fbpx

Flappy Bird – Scoring

Reading Time: 5 minutes
In the very last lecture of the Flappy Bird tutorial we learned how to add to game the collision detection. When the circle of the bird overlap the rectangels of the pipes, then we are going to get a log message in the Logcat of the Android Studio.

In this lecture we are going to learn how to add the scoring to the Flappy Bird game. It means the score has to be even higher when our bird could fly through the gap of the pipes.

Before the start we have to clarify when we will get 1 score. When the bird could fly through the gap between the pipes, but it is not quite true. In the previous lecture we defined the collision detection. If the birds circle overlap the rectangelas of the pipes, then the bird collided with the pipes. In case if the bird is in the gap, then logically this shapes won’t be overlaped, so we can the score. It looks like easier, right? 🙂 So let’s start coding.

Step 1 – Scoring integer

The first thing what we are going to do is the scoring integer variable. We ill define it in the FlappyBird::class and the initial value will be 0, because at the start of the game we won’t we point, logically.

var score = 0

Step 2 – Scoring pipe

During the game we have to that the bird could fly through the gap of the pipes or it couldn’t. For this we have to wher is the next pipe. As we have defined earlier, we have in the same time 4 pipes in the game. Every pipe has a specific x position on the screen. They are stored in the pipeX mutableList.

We will get a score, when the pipe’s x position is less then the half of the width. It means the bird could fly through the gap. So we will define an if-statement inside of the render method and when the gameState not null, but before the our touch.


Scoring

What you can see in the if-statements is that first we check the position of the current pipe, then the score is going to be increased by one, and we will jump to the next pipe.

Step 3 – Show the collected points

Now we are counting the points, but we have only a log message. We want to see it on the screen. For this we have 10 images in the assests folder from 1.png to 9.png.

As we have done with the states of the bird’s flap, we are going to have a mutableList, and it will contain the 10 images. We will add to the list the images using a for-loop in the create method.

for (i in 0..9)
{
    scoreTextures.add(Texture(${i}.png))
}

To figure out which picture and where has to be visible is a little bit difficult, but don’t worry, I am going to explaine it. 🙂

We have to split the score to it’s digits, because we have to know in every position of the score number the exact number. If we know this, then we can set the correct number’s picture. To do that we will use modulo by 10. After that we will get the numbers in reverse order, so we have to call reverse on the list of the numbers the reverse method. We are going to do that only if the score is higher then 0.

To draw the correct numbers in the correct order next to each other we will add to the x position the size multiplied by the i variable from the for-loop.


Scoring textures

If you are surprised why we do use the _score variable, then here is the answer 🙂 During the while loop, where we split the score integer to it’s digits, we have to modify the score variable, and we are going to see always 1 digit on the screen.

Step 4 – FlappyBird::class

If you are lost, then you can compare your source code here with our whole FlappyBird::class


FlappyBird::class

In the next lecture we will finish our Flappy Bird tutorial with game over screen.

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Click to rate this post!
[Total: 0 Average: 0]
Follow and like us:

2 thoughts on “Flappy Bird – Scoring”

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications