In this chapter we are going to add the calculation of scores and levels. The Timer is executed every seconds and this will be the scoring. One score equals one second. Every 20 seconds the level will be increased by one, and the speed by 4. It means the game will be even faster.
In the last step we gonna accelerate the velocity as well.
After a short introduction, let’s start coding. 😉
Step 1 – Count the score
The first thing, what we are going to do is to declare the score variable at the beginning of the TekiTutotrial::class with initial value of 0.
var score = 0
We will change the value of the score variable only in the startTimer() method. As we talked about, the score equals to the seconds. It means every time when the timer ticks, the score value is going to be increased by one.
Change the log message as well to see the score in the Logcat of Android Studio. Run the app.
Step 2 – Count the level
Every time when you reach 20 scores, the level is going to be increased by 1. To have it later visual, we have to declare it’s variable, called level.
var level = 0
To know when the level has to be increased, we will define one more variable, called nextLevel. This variable will be increased by 20 every time when the next level has been reached.
var nextLevel = 20
The initial value will be 20, because at the start the first level comes when you have already 20 scores.
Inside the run() method of the Timer we have to check if the score equals to the nextLevel. So there we will have an if-statement, before the increase of the score.
if (score == nextLevel)
In the if-statement will be the increase of the level and we will set the nextLevel to the next boundary of level.
if (score == nextLevel)
nextLevel += 20
After the line of the score’s log message, we will add a log line for the level as well.
Change the log message to show the level.
Run the app, and check it.
Step 3 – Increase the velocity
In this lesson we gonna talk about the accelerate of the velocity. We will do it here, because this will be also in this if-statement, what we defined now.
It is just a single line of code. Previously we have defined the velocity variable. Now we gonna increase it by 2. You can play with it.
The higher this number will be the game gonna be harder.
velocity += 2
Run again the app and see how it is going to be faster and faster by every level.
Finally the whole source code of the TekiTutorial::class.
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂