Flappy Bird – The gravity and the touch

In the very last lessons in our Flappy Bird tutorial we have learned how to have a flapping bird, and a static background which is fill out the whole screen.

In this lesson we are going to learn how to get the flappy bird interacting with us. It means that the bird has got a very simple control mechanism. What we need to do is just touch the screen, doesn’t matter where, and then the flappy bird going to start flying up against the gravity … and fall down, if we don’t touch again the screen 😉

So how do we get a tap to be detected? Actually it is very straightforward.

justTouched

The GDX library has an input option, and this has a justTouched method. We are going to implement this method inside the render method.

To observe the touches, we will have an if statement. This will be executed, if the screen has been touched.


The justTouched method

So what we actually want is for this bird to fall down, and then when we tap the screen the bird flies up and then falls down again. It means, we want to create a gravity system. So for the gravity we are going to know the birds position and the velocity. For this we will create these as Floats.

Y coordinate

We are going to call the position of the bird as birdY. The x coordinate of the bird is going to be always the same, because the bird will move only up and down. We will declare and initiate the birdY variable inside the FlappyBird:class.

var birdY: Float = 0f

But we don’t want it to be zero, because we want it to be equal to where the bird starts. This is also the center of the screen.

birdY = (Gdx.graphics.height/2size/2)

We will do this in the create method, because we want it to happen only once.

Velocity

Next step is to setup the velocity, and this means how fast the bird is moving. This variable, as a float, going to be zero at the beginning. Later on each time the value of it going to be increased.

After this the birdY will be increased or decreased by the velocity. So we are going to get this effect of gravity like our bird is falling down.

gameState

Before we test the gravity, we have to implement the gameState. We are going to use it to check if the game is running or not. At the start of the game our bird has to be in the middle of the screen. When the user taps the screen the bird should fall down.

If the gameState is not zero, then all of the processing going to be executed in the render method.

Inside the justTouched method the gameState will get the number of 1 and the whole justTouched method is going to go inside the else statement, where we are checking the gameState value.

If the gameState is not 0 anymore, then the velocity has to increased, and should decrease the y coordinate of the bird, so this 2 lines go the if-statement.

Gravity

Our bird falls down a little bit slowly, so we will set another variable, called gravity. The initial value is going to be 2, because 1 is to slow.

Inside the if-statement of the gameState we are going to increase the velocity by the gravity variable.

The bird goes up

To have our bird flying when we touch the screen, we have to decrease the velocity by each tap and we want to do this when the game is already running.


The justTouched method

This means, if we touch the screen, the velocity will be decrease, and it will decrease the y coordinate of the bird also. This will look like our bird is flying upwards.

The bottom of the screen

It is a little bit strange, that our bird can disappear at the bottom of the screen. To avoid this, inside the if-statement of the gameState we will have one more if-statement, and this will check the birdY coordinate and the velocity.


The justTouched method

The birdY > 0 means, that the bird has reached the bottom of the screen.

The velocity < 0 means, that our bird still will fly, even if it reached the bottom.

FlappyBird::class

Finally the whole FlappyBird::class.


AndroidManifest.xml

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! 🙂

 


 

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

Leave a Reply

Your email address will not be published. Required fields are marked *