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

Moving Sammy the snake


So, we have Sammy the snake on the screen, sitting there, looking snaky. However, it isn't much of a game. If it were, we could finish the book right here! What we need to do now is get that snake slithering across the screen!

First, let's sort out the playing area. Currently, the resolution is 640 x 480 pixels and the snake texture is 32 x 32. This means we have a grid of 20 x 15—derived by dividing up the resolution by the texture (640/32 = 20, 480/32 = 15)— of the different positions the snake head could be in. The reason we are going to do it this way is because the original game moved with a periodic movement of one snake component at a time. We are going to do the same.

Let's define our timer. We are going to start off with an interval of one second between movements. So let's create a constant field:

    private static final float MOVE_TIME = 1F;

Now, define a variable to keep track of the time:

    private float timer = MOVE_TIME;

Finally, let's get it updated in...