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

More on collisions


You might have found while playing our little game that you can make the snake turn back on itself or it even goes through itself. Anyone who remembers the original will know that this results in an immediate game over!

What we are going to look at now can essentially be split into two parts. First, we will fix the snake's ability to slither back on itself, as seen in the following screenshot:

Second, we will look at stopping the game when the snake collides with another part of its body, as seen in the following screenshot:

Stopping the doubleback

With our snake slithering around, if you change the direction of the snake and you tell it to go back the way it is traveling, it will slide right through itself. This isn't how the game should be!

Essentially, what we want to do is, if the player tells the snake to change its direction and move in the exact opposite direction, ignore that input command.

First, we will create a method to do this precise check:

private void updateIfNotOppositeDirection...