官术网_书友最值得收藏!

Introducing Sammy the snake

Before we start making the Snake game, we need to set up our textures for the snake and the game play area. So, let's remove the default code from our GameScreen class, leaving just our SpriteBatch batch's clear screen calls:

public class GameScreen extends ScreenAdapter {

    private SpriteBatch batch;

    @Override
    public void show() {
        batch = new SpriteBatch();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }
}

Next, let's change the color that fills the screen from red to black. We can do this by updating the glClearColor method call to reference the r, g, b, a values of the black Color class:

Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);

If you run the project now, you will find that we are back to our black screen; however, this time, the screen is being cleared every render call. If you don't want to use black, check out the other default colors LibGDX provides, failing that you can define your own! You can do this by creating your own instance of Color with your own r, g, b, a values.

Note

We are very fortunate that there is now plenty of computing power available to do this. Many moons ago, game developers could only refresh sections of the screen that had changed to preserve CPU cycles.

Next we will add back calls in to our batch as we did earlier:

        batch.begin();
        //Our rending code will live here!
        batch.end();

Admittedly they won't do much right now, but our texture rendering code will sit in between them. Failing to call begin() before trying to call any other method for the SpriteBatch class will result in a java.lang.IllegalStateException exception being thrown.

Now, I think we are ready to start drawing our snake!

Giving the snake a face

We have two assets that we are going to use for drawing our snake, snakehead.png and snakebody.png. These assets will reside in a specific directory with in the project. As we are only using the desktop export of LibGDX at the moment, you will find an assets directory in the core project. However, when we start looking at the Android export, you will find the assets directory will move. The assets themselves are self-explanatory, one for the snakes head and one for the snakes body. Let's start with the head.

Back in our GameScreen class, add a texture object and call it snakeHead:

private Texture snakeHead;

Next, let's initialize the texture in the show() method:

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

Uh oh! Looks like something new cropped up here—Gdx.files.internal. LibGDX comes with some handy tools for handling files. Here we are just loading a file from within the project.

Now, let's render it in the render() method:

batch.draw(snakeHead,0,0);

Hopefully, when you run the project, you will get the following output:

Giving the snake a face

The default resolution that the DesktopLauncher parameter has is 640 x 480 pixels. Currently, this is what we are rendering to. Later on, we will discuss using a viewport to allow us to handle multiple different resolutions.

主站蜘蛛池模板: 建湖县| 达日县| 东阳市| 桃园县| 平山县| 肥乡县| 宁城县| 张北县| 西宁市| 扎囊县| 紫金县| 南部县| 弥渡县| 定日县| 佳木斯市| 石嘴山市| 红安县| 太仓市| 杂多县| 利辛县| 娱乐| 安义县| 石嘴山市| 抚州市| 沧源| 新干县| 津南区| 莱西市| 新绛县| 韶山市| 芜湖县| 马尔康县| 义乌市| 潜山县| 九江市| 岳西县| 漳平市| 宜城市| 临桂县| 易门县| 泽普县|