Write a Flappy Bird Clone using Basic4Android and LibGDX – Part 5

Part 5 – Adding player input
Grab the source code:

Now we’re ready to start detecting user input to make the bird flap its wings
We need to setup a method for performing an action when the user taps the screen. This is the IP_TouchDown method. This first needs declaring, just like everything else so let’s declare it in our Globals section just like everything else:

Dim IP As lgInputProcessor

Initialise the object in the LG_Create section with:

‘Sets the InputProcessor

We then add the method for handling touches to the screen at the end of our code.

Sub IP_TouchDown(screenX As Int, screenY As Int, Pointer As Int) As Boolean
myBird.my = 4*scaleY ‘flap wings
End Sub

The method accepts screen and screen values which tell us where on the screen the player touched and a pointer value, which tells us how many fingers are touching the screen. We’ll ignore these for now, because we want the bird to flap its wings whenever anywhere is tapped on the screen.
So every time the screen is touched we run the line within the method. We take the mybird.my value and set it to 4 x scaleY. scaleY is set to a value that should cause the bird to set its upwards speed to look the same no matter what the phone screen resolution.
In our LG_Render section, just before the command to draw the bird, we add the code to update the birds Y position:

‘stop bird going too fast
If myBird.my > (-10*scaleY) Then
myBird.my = myBird.my – (0.2*scaleY)
End If
myBird.y = myBird.y + myBird.my

We’ll only adjust the birds my value if the bird’s not going over -10 pixels per frame, this is just to stop it falling too fast. So we’ll say that -10 is our “terminal velocity” in this sense. So assuming my isn’t too low, then we set it my – 0.2*scaleY this applies a gravity value to the my value for each frame. We then update the mybird.y value by the my amount. For each frame the my value goes down by a gravity amount, it gives the impression of the bird dropping. Try changing the gravity value and seeing the effect it has.
Okay, we need to set the bird angle, this calls for a crazy formula loosely based on the my value. Pointing straight to the right is zero degrees. I had a play around with some numbers and came up with this as something that works well. Try playing with this too and see if you can make it simpler. This needs to go just before the draw bird command in the LG_Render section.

birdAngle = (((myBird.my/scaleY)+6)*5.5)+335

We’ll call that it for this session as the collision detection is going to take a little explaining, and I’d like to add scoring in the next tutorial too.
Download the source code here:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s