O'Reilly logo

LibGDX Game Development By Example by James Cook

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Increasing the length of the snake

We have the snake eating the apple; however, there aren't any consequences to this. As a part of the game, we need to make the snake increase the length of his body for each apple he eats. Our requirements for this are as follows:

  • Add a body part to the snake when it eats an apple
  • As the snake moves, the body parts should follow
  • There will be multiple body parts

First, let's create a class that will contain the length of the snake's body. This can take the form of an inner class for now:

 private class BodyPart { private int x, y; private Texture texture; public BodyPart(Texture texture) { this.texture = texture; } public void updateBodyPosition(int x, int y) { this.x = x; this.y = y; } public void draw(Batch batch) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required