# Teki – Calculation of scores and levels

In the very last lesson we added the Timer to our Teki Android game in this tutorial series. The Timer will be started when we touch the screen, so when we start the game.

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.

score++

``````fun startTimer()
{
timer = Timer()

{
override fun run()
{
score++
Gdx.app.log("score", "\$score")
}
}, 0, 1000)
}

startTimer()``````

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)
{
level++
nextLevel += 20
}

After the line of the score’s log message, we will add a log line for the level as well.

Just set the cursor to the line of the scores log message and press Ctrl+d. It will duplicate the line.

Change the log message to show the level.

Gdx.app.log(“level”, \$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.

#### TekiTutorial::class

Finally the whole source code of the TekiTutorial::class.

``````import com.badlogic.gdx.ApplicationAdapter
import java.util.*

{
var batch: SpriteBatch? = null

var screenWidth = 0
var screenHeight = 0

var gameState = 0

// Background
var backgrounds = mutableListOf()
val numberOfBackgrounds = 3
var backgroundX = FloatArray(numberOfBackgrounds)

// Teki
var tekisLegStates = mutableListOf()
var tekiX = 0f
var tekiY = 0f
var tekisStates = 0

//Timer
var timer: Timer = Timer()

//Score
var score = 0
var level = 0
var nextLevel = 20

var velocity = 12
var distance = 0

var size: Float = 0f

override fun create()
{
batch = SpriteBatch()

screenWidth = Gdx.graphics.width
screenHeight = Gdx.graphics.height

size = (screenWidth*0.06).toFloat()

//Background
for(i in 1..numberOfBackgrounds)
{
}

//Teki
tekiX = screenWidth*0.35f
tekiY = screenHeight*0.22f

for (i in 0 until numberOfBackgrounds)
{
backgroundX[i] = screenWidth.toFloat() * i
}
}

override fun render()
{
batch!!.begin()

for(i in 0 until numberOfBackgrounds)
{
drawBackground(i)
}

distance += velocity

if (gameState == 0) // Main Menu screen
{
drawTeki(0)

if(Gdx.input.justTouched())
{
gameState = 1
startTimer()
}
}
else if (gameState == 1) //Running game screen
{
for(i in 0 until numberOfBackgrounds)
{
if (backgroundX[i] <= -screenWidth)
{
if (i == 0)
{
backgroundX[i] = backgroundX.max()!!.plus(screenWidth) - velocity
}
else
{
backgroundX[i] = backgroundX.max()!!.plus(screenWidth)
}
}
else
{
backgroundX[i] = backgroundX[i] - velocity
}
}

//Teki
if(distance % 20 == 0)
{
when(tekisStates)
{
0 -> tekisStates = 1
1 -> tekisStates = 2
2 -> tekisStates = 1
3 -> tekisStates = 0
}
}
drawTeki(tekisStates)
}
else if (gameState == 2) //Game Over screen
{
stopTime()
}
else if (gameState == 3) //High Score screen
{

}

batch!!.end()
}

fun startTimer()
{
timer = Timer()

{
override fun run()
{
if(score == nextLevel)
{
level++
nextLevel += 20
velocity += 2
}

score++
Gdx.app.log("score", "\$score")
Gdx.app.log("level", "\$level")
}
}, 0, 1000)
}
fun stopTime()
{
timer.cancel()
}

fun drawBackground(number: Int)
{
// Background
batch!!.draw(
backgrounds[number],
backgroundX[number],
0f,
screenWidth.toFloat(),
screenHeight.toFloat())
}
fun drawTeki(number: Int)
{
//Teki
batch!!.draw(
tekisLegStates[number],
tekiX,
tekiY,
size*1.2f,
size)
}
}

TekiTutorial()``````

#### 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! 🙂

Click to rate this post!
[Total: 0 Average: 0]