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

Part 6 – Collision detection and restarting the game

Download the source code here!

https://dl.dropboxusercontent.com/u/8673694/you%20tube%20files/basic4android/CloneyBirdTutorial/Part6.zip

Now we have our bird on the screen, it’s responding when we tap the screen, flapping its wings, but passes straight through every pipe, and even the ground. Let’s change that.

We know the size of our pipes, we know the size of the gap in the pipe, so we can calculate whether the bird is in that gap or not. The first thing to check is whether the X position of the bird overlaps a pipe. We then check the Y position to see if it’s in the gap or not, if it is then we can give it a point, if it’s not in the gap, then it’s crashed into the pipe and we kill it, game over.

Before we do this, we’ll need to add some variables in our globals section. We use gamerunning to store whether game is running or not, we use this later for showing a title screen before we start the game. We then declare birddead to check if the bird is living or dead, a restartCD variable to act as a countdown timer to stop the game restarting straight away after the bird dies. Finally we add an integer to store the score value.

Dim gamerunning As Boolean = True

Dim birddead As Boolean = False

Dim restartCD As Int = 60

Dim score As Int = 0   

Moving onto the LG_Render section of code, add the following code in the update pipes loop, after the pipe.x=pipe.x-speed line but before the if pipe.x < -100 line.

‘check for collision

‘if bird x coord is between left and right side of pipe

If (myBird.x+vpW*0.085+(myBird.width*scaleX) > Pipe.x) AND (myBird.x < (Pipe.x+vpW*0.125)) Then

‘ if bird y coor is not in the gap in the pipe

If (myBird.y < Pipe.y+(img_pipe.Height*scaleY*0.4375)) OR (myBird.y+vpH*0.04 > Pipe.y+((img_pipe.Height*0.5625)*scaleY)) Then

            ‘if birds not dead then kill it

            Ifbirddead = FalseThen

                  ‘set bird y movement to zero

                  myBird.my = 0

           End If

            ‘set bird dead to True

            birddead = True

‘set game speed to zero to stop screen scrolling

            speed = 0

Else

‘else increment score by one if bird passes pipe

            If Pipe.scored = False Then

                  ‘set pipe scored to true to only a one to score

 Pipe.scored = True

                  ‘increment score

                  score = score + 1

            End If

End If

End If ‘END COLLISION CHECK

 

Let’s look through this and try to make it simple. The first IF is checking the x position of the bird against the pipe, the pipe.x value is the left side of the pipe, so pipe.x + 12.5% of the viewport is the right side of the pipe. We then check if the bird is in the gap or not, using scaleY and the percentage of the height of the pipe the gap appears, we can calculate where the gap is on the screen and compare it to the birds Y position. If the bird is not in the gap then the first block of code is ran, otherwise the block after the else is ran. If the bird hits the pipe we set the bird.my to zero, we also set the birddead Boolean to true and the speed to zero to stop the bird moving to the right.

If the bird doesn’t hit the pipe, then we set the scored boolean value of the pipe to true and increment the score.

Now we have our collision detection code in place, give the game a go, check that you can crash into the left side of the pipe, the bottom of the gap, and the top of the gap in the pipes.

 

We’re now going to stop the bird going off the bottom of the screen to simulate crashing into the ground, add the following code after the pipes update loop:

‘stop bird going off bottom of viewport

If myBird.y < vpH*0.20 Then ‘if bird lower than 20% of viewport hieght

speed = 0 ‘set speed to zero to stop scrolling

birddead = True’kill bird

myBird.my = 0 ‘set bird my speed to zero

myBird.y = vpH*0.20 ‘set bird y coord to 20% of viewport height

End If

We check the bird.y value, if it goes lower than 20% off the bottom of the viewport then we stop the bird and kill it. We set the speed to zero, birddead to zero, mybird.my to zero and myBird.y to 20% up the viewport.

Try it out, you should find your bird hits a pipe and falls to the ground, or just stops if it hits the ground. We now have an issue however as we have to restart the game by quitting with the back button and re-opening the app.

Before we do that, we need to decrease out restartCD value for each frame, add the following lines to the end of the LG_Render method:

‘decrease countdown timer if bird is dead

If birddead = True Then

restartCD = restartCD – 1 ‘decrease restartCD

End If

So let’s add code to let us restart the game. We’ll add this in the IP_TouchDown method. Replace the current code in there with the following:

If gamerunning = True Then ‘if game running

            Ifbirddead = FalseThen’ and bird alive

                  myBird.my = 4*scaleY ‘flap wings         

            ElseIfbirddead = TrueANDrestartCD < 0Then

                  ‘restart game bird

birddead = False ‘revive bird

                  pipes.Clear ‘remove all pipes

                  speed = 3’reset speed

                  myBird.Y = vpH*0.5′ set bird y coord to restart

                  myBird.my = 4*scaleY’set starting flap

                  restartCD = 60’set restart countdown

                  pipeCD = 100’set first pipe countdown

                  birdColour = Rnd(0,4) ‘ random bird colour

                  score = 0 ‘reset score

            End If

      Else

            ‘start game

            gamerunning = True’ set game to running state

            myBird.my = 4*scaleY’ alter my to make bird flap

      End If

 

This basically means that if the game is running and the bird is dead then make the bird flap it’s wings, or, if the bird’s dead and the countdown timer is less than zero then reset all the variables. Pipes.clear deletes all the pipes in the list. This causes the game to restart as the main loop speed is set back to a number higher than zero and the bird position is reset, we even randomise the colour of the bird!

The bit at the bottom just starts the game running after the title screen is shown, this is the title screen we haven’t added yet. That comes in the next tutorial.

 

Advertisements

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