# Teki – The game states

In the very last lecture we have defined the background and the movement to the left. It works pretty well, but it is annoying, that it starts running when we open the app.

To avoid this, and to get back the control of the game, we are going to define the game state. At the end it will have 4 possible values, but currently we will talk about only 2. Running, and not running.

Let’s start coding. 😉

#### Step 1 – Define the gameState variable

Our first step is going to be to add the gameState varible to the game. It means we will declare it at the beginning of the TekiTutorial::class.

var gameState = 0

The initial value will be 0, and it means the Main Menu screen.

#### Step 2 – Checking the value

In the create() method we don’t need to change the value. We will observe and modify it only in the render() method.

We are going to define in this chapter the 4 states, but as we talked about in the begin of the chapter, we will have codes only for the values of 0 and 1.

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

}
else if (gameState == 1) //Running game screen
{

}
else if (gameState == 2) //Game Over screen
{

}
else if (gameState == 3) //High Score screen
{

}

the gameState check``````
###### Relocate the backgroundX

In the previous lecture we have defined the moving background, and for that the FloatArray of backgroundX. We want to have this moving effect only when the game is running, so we are going to replace it when the gameState is equal to 1.

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

}
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
}
}
}
else if (gameState == 2) //Game Over screen
{

}
else if (gameState == 3) //High Score screen
{

}

Repleaced for-loop``````

#### Step 2 – justTouched

The GDX library has an input option, and this has a justTouched method. We are going to implement this method inside the render method.

To observe the touches, we will have in if-statement. This will be executed, if the screen has been touched.

``````if(Gdx.input.justTouched()) { }

The justTouched method``````

What we want to have currently inside in this if-statement is to change the gameState to 1, so we want to start the game. In our case it means that everywhere we touch the screen, this
if-statement is going to be executed.

``````if(Gdx.input.justTouched())
{
gameState = 1
}

The justTouched method``````

#### TekiTutorial::class

If you have made everything correctly, then you should see this, if you run game on your device.

… and as usual, the whole TekiTutorial::class

``````import com.badlogic.gdx.ApplicationAdapter

{
var batch: SpriteBatch? = null

var screenWidth = 0
var screenHeight = 0

var gameState = 0

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

var velocity = 12

override fun create()
{
batch = SpriteBatch()

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

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

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

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

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

if (gameState == 0) // Main Menu screen
{
if(Gdx.input.justTouched())
{
gameState = 1
}
}
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
}
}
}
else if (gameState == 2) //Game Over screen
{

}
else if (gameState == 3) //High Score screen
{

}

batch!!.end()
}

fun drawBackground(number: Int)
{
// Background
batch!!.draw(
backgrounds[number],
backgroundX[number],
0f,
screenWidth.toFloat(),
screenHeight.toFloat())
}
}

TekiTutorial::class``````

#### 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]