Skip to Content
LibGDX Game Development By Example
book

LibGDX Game Development By Example

by James Cook
August 2015
Intermediate to advanced content levelIntermediate to advanced
280 pages
5h 31m
English
Packt Publishing
Content preview from LibGDX Game Development By Example

Adding the apple

Next up, we need to get our snake eating something. Let's get our apple implemented.

We will need to get our apple to do the following things:

  • Randomly being placed on the screen, not on the snake!
  • Only place one if there isn't an apple on the screen already
  • Disappear when it collides with the snake's head

Right! Let's add the texture:

    private Texture apple;

Then, let's amend our show() method and add the code to instantiate the apple texture:

    apple = new Texture(Gdx.files.internal("apple.png"));

Let's add a flag to determine if the apple is available:

    private boolean appleAvailable = false;
    private int appleX, appleY;

This will control whether or not we want to place one. In the Snake game, the next apple appears after the current apple ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering LibGDX Game Development

Mastering LibGDX Game Development

Patrick Hoey
Learning LibGDX Game Development- Second Edition

Learning LibGDX Game Development- Second Edition

Suryakumar B Nair, Andreas Oehlke

Publisher Resources

ISBN: 9781785281440Supplemental Content