The last part of the Flappy Bird tutorial has arrived. In the very last lesson we learned how to implement the collision detection. So know already when the bird’s circle overlaps the pipes’ rectangles.
In this lesson we are going to add the Game Over screen to the app. It means, that the game will stop when collision is detected, and the Game Over picture is going to be poped up.
So let’s get started.
var gameOver: Texture? = null
… and also the initialization goes to the create method, again as we have then earlier.
gameOver = Texture(“gameover.png”)
What we have to do with the Texture is that we have to draw it to the screen. Before that we have to change the gameState a little bit.
Until now we had two game states.
For the Game Over screen we have to introduce the game over state.
In the previous versions we checked if the gameState is not 0, but it is not enough already. So we have to change this, namly the first condition to
gameState == 1, then to
gameState == 0, and the last condition will be
gameState == 2.
So basically now the content of the if-else-statement is going to be the same.
To continue the implementation, we have to figure out when the game has to be over.
We have defined the game state value, so now we have to change the code. First we are going to add the new gameState to the collision detection, so inside of the if-statement.
gameState = 2
The second change goes to the acceleration of the speed when the gameState is equal to 1. There we have defined an if-statement, which checkes the y position of the bird. If it is not 0, then the bird should fly, otherwise the game has to be over. It means, that we are going to extend the statement with the else part. There we will set the gameState to 2.
Game over, when the bird has reached the bottom of the screen
It is important to have the correct modification, so I just give it to you. As we talked few lines ago, we have already the 3 states.
if-statement of the gameState
Into the gameState 1 goes the same code what we had when we have just gameState != 0.
Into the gameState 0 goes the justTouched method. If the screen is touched, then the gameState get the value of 1.
Into the gameState 2 goes the draw method of the Game Over image.
The image has to be center horizontally and also vertically. To get this we will divide the width and the height of the screen. The width will be 6 times bigger then the size variable. The height will be equal to the size. We have to subtract the half of the width of the image from the horizontal position and again the half has to be subtracted from the height.
What has yet to be here, when we touch again the screen, is to restart the game. So there we will have this lines.
if-statement of the justTouched
As you can see we will set the gameState to 1 and before that we have the startGame method.
In this mehod we will set back to the original value the birdY, the pipeX and the distVertBwPipes mutableLists. We have have already defined them in the create method, so we are going to move birdY initialization and the for loop to the startGame method.
We have still 3 variables, what we have to reset.
Finally, if you are a little bit lost, then here you can check the final source code of the FlappyBird::class.
I hope you enjoyed this tutorial. It is a good start of you want to get to have beginner knowledges, how does the Gdx library works.
We have learned how to add images to the screen and to identify their positions and how to have moving sprites. We have added a tri-state mechanism to the game also, which allows us to start, play and finish the game.
I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂
Subscribe to receive exclusive content and notifications