Book Image

LibGDX Game Development By Example

By : James Cook
Book Image

LibGDX Game Development By Example

By: James Cook

Overview of this book

Table of Contents (18 chapters)
LibGDX Game Development By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The state of the game


We now have our Snake game at the point where it is almost a complete game; we just need to add a few more things to achieve this. Currently, our game stops when the snake collides with itself. What we need to add is a way to restart the game if this happens, so the player can have another go!

To do this, we will do the following:

  • Adding a game state

  • Displaying game-over text

  • Restarting the game

Adding a state

When you think about it, a game can have many states. Seriously, pick one of your favorite games and consider the states the game could be in. Let's list some out:

  • Start

  • Playing

  • Paused

  • Game over

  • Restart

It could be argued that start and restart are the same thing, but that really depends on the game and what you, as the game developer, would like to achieve. For our little Snake game, we are only going to focus on two states for now: playing and game over.

Let's first start off by creating an enumeration of our states, and let's create a member of GameScreen to use it:

private...