# 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.

``````if(Gdx.input.justTouched())
{

}

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.

``````if(Gdx.input.justTouched())
{
velocity = -20f
}

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.

``````if(birdY > 0 || velocity < 0)
{
velocity += gravity
birdY -= 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.

``````import com.badlogic.gdx.ApplicationAdapter
import kotlin.math.min

{
var batch: SpriteBatch? = null
var backGround: Texture? = null

var size: Float = 0f

var birdsState = mutableListOf()

var flapState = 0

var birdY: Float = 0f
var velocity: Float = 0f

var gameState = 0
var gravity = 2f

override fun create()
{
batch = SpriteBatch()

size = min(
(Gdx.graphics.width*0.1).toFloat(),
(Gdx.graphics.height*0.1).toFloat()
)

backGround = Texture("background-day.png")

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

override fun render()
{
if (gameState != 0)
{
if(Gdx.input.justTouched())
{
velocity = -20f
}

if(birdY > 0 || velocity < 0)
{
velocity += gravity
birdY -= velocity
}

when(flapState)
{
0 -> flapState = 1
1 -> flapState = 2
2 -> flapState = 3
3 -> flapState = 0
}
}
else
{
if(Gdx.input.justTouched())
{
gameState = 1
}
}

batch!!.begin()
// Background
batch!!.draw(backGround,
0f, 0f,
Gdx.graphics.width.toFloat(),
Gdx.graphics.height.toFloat())
// Bird
batch!!.draw(
birdsState[flapState],
(Gdx.graphics.width/2 - size/2),
birdY,
size,
size)
batch!!.end()
}
}

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! ðŸ™‚