The project setup

Ok, new game, new project. We will start with a blank canvas; so go through and create a project like we did in the previous example.

Using the LibGDX Project Generator, call the game FlappeeBee and give it a game class name of FlappeeBeeGame.

The package can be anything you like, and then set the destination to wherever you wish to keep your code. Finally, uncheck every checkbox except Desktop.

Once this is done, click on Generate and then import the project into your choice of IDE.

Setting up the GameScreen class

With this being a fresh project, hot off the generator. It will have the default Hello World! game inside. Just bear this in mind for the moment.

Next, we create our own GameScreen class. Remember, when you create this, we just need it to extend ScreenAdaptor:

public class GameScreen extends ScreenAdapter {
}

The preceding code is what you should have in your GameScreen class for now. So, next we need to populate it with a few things that you might recognize from the Snake game.

First, let's give the game a world to live in. As FlappeeBee will be a portrait game, we can just switch the values we used before:

  private static final float WORLD_WIDTH = 480;
  private static final float WORLD_HEIGHT = 640;

Add the preceding code to your GameScreen class.

Next, let's set up the objects that are going to be doing the heavy lifting of rendering and view handling:

  private ShapeRenderer shapeRenderer;
  private Viewport viewport;
  private Camera camera;
  private SpriteBatch batch;

Add the preceding members to the class, and then let's create the following methods as well:

  @Override
  public void resize(int width, int height) {
    viewport.update(width, height);
  }

  @Override
  public void show() {
    camera = new OrthographicCamera();
    camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
    camera.update();
    viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
    shapeRenderer = new ShapeRenderer();
    batch = new SpriteBatch();
  }

  @Override
  public void render(float delta) {
    clearScreen();
    batch.setProjectionMatrix(camera.projection);
    batch.setTransformMatrix(camera.view);
    batch.begin();
    batch.end();
  }

  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);
  }

Now, our GameScreen class is ready to rock and roll!

Our final setup task before we dive into making FlappeeBee is to update the FlappeeBeeGame class. If you remember how the SnakeGame class looked, this will look very familiar to you, especially as it makes the class very simple.

First, change the parent class to Game and then remove all the code from the body of the class and add the following code:

  @Override
  public void create() {
    setScreen(new GameScreen());
  }

Excellent! With this added, we can now start! But before we go, I will just give a recap of what we have set up here.

Our GameScreen class uses the screens functionality in LibGDX to help make managing the lifecycle of a game easier along with logically splitting the game up.

The Viewport parameter helps with projecting our game world onto the screen of the player. The Camera parameter is maintained by the Viewport parameter and we use it to update the SpriteBatch parameter so that our world is projected and transformed correctly on to the players' screen. Finally, we have the ShapeRenderer class; this will be useful to debug our collisions and placeholders for our game objects before we use the art.

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

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