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 is eaten; therefore, we don't need any fancy timing mechanism to deal with it. Hence, the apple is not available at the start as we need to place one first. We also specify the variables that will contain the location of the apple.

Finally, add the drawing code to the render() method:

        if (appleAvailable) {
            batch.draw(apple, appleX, appleY);
        }

Here, we tell the game to only render the apple if it has been placed.

Now we need to randomly place the apple on the screen:

    private void checkAndPlaceApple() {
        if (!appleAvailable) {
            do {
                appleX = MathUtils.random(Gdx.graphics.getWidth() / SNAKE_MOVEMENT - 1) * SNAKE_MOVEMENT;
                appleY = MathUtils.random(Gdx.graphics.getHeight() / SNAKE_MOVEMENT - 1) * SNAKE_MOVEMENT;
                appleAvailable = true;
            } while (appleX == snakeX && appleY == snakeY);
        }
    }

The previous code listing shows the required rule for placing the apple on the screen. First, we check whether we need to place an apple, then we randomly pick a location on the screen, which is a multiple of 32, and we repick it if the picked location contains the snake. As we are working in a 0-indexed environment we need to subtract one (1-20 becomes 0-19 and 1-15 becomes 0-14). Add this method to the render() method after we move the snake:

    @Override
    public void render(float delta) {
        queryInput();
        timer -= delta;
        if (timer <= 0) {
            timer = MOVE_TIME;
            moveSnake();
            checkForOutOfBounds();
        }
        checkAndPlaceApple();
        clearScreen();
        draw();
    }

You may have noticed two new methods creep in there, clearScreen() and draw(). These are the result of a very quick refactor to neaten up our render method. The clearScreen() method is as follows:

    private void clearScreen() {
        Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

The draw() method is as follows:

    private void draw() {
        batch.begin();
        batch.draw(snakeHead, snakeX, snakeY);      
        if (appleAvailable) {
            batch.draw(apple, appleX, appleY);
        }
        batch.end();
    }

Now run the project. Hopefully you will see something similar to the following screenshot. The apple will be randomly placed on the screen every time you launch the project.

Adding the apple

You may have noticed, however, that if you take control of the snake and head over to the apple, the snake will just glide over the top and the apple will remain. Not very game-like and not what we would want to happen.

Let's fix this!

We can do this quite simply by checking whether the apple and the snake head coordinates match:

    private void checkAppleCollision() {
        if (appleAvailable && appleX == snakeX && appleY == snakeY) {
            appleAvailable = false;
        }
    }

If they do collide, we set the appleAvailable flag to false. This will trigger a respawn of the apple.

Add the checkAppleCollision() method to the render() method above checkAndPlaceApple().

Run the project, and you will now find that the apple respawns every time the snakes head collides with it.

On to the final part of this chapter. We need to make the snake grow in length every time it eats an apple.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.10.32