Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit)

Time to code !

Basic setup

First of all, we'll modify the DesktopLauncher.java to choose a resolution and a name for the game window. As I still don't have a name for my project, I'll simply go with "Life in Space", and I chose a resolution of 960 * 640 for the development.

Here is my DesktopLauncher.java :

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "Life in Space";
        config.width = 960;
        config.height = 640;
        new LwjglApplication(new MyGdxGame(), config);
    }
}

Then I modify the MyGdxGame.java by extending Game.java, so I can use screens.

Here is the MyGdxGame.java :

public class MyGdxGame extends Game implements ApplicationListener{
    public SpriteBatch batch;
    public AssetManager assets;
    
    @Override
    public void create () {
        batch = new SpriteBatch();
        assets = new AssetManager();
        
        this.setScreen(new GameScreen(this));
    }


    @Override
    public void render () {
        super.render();
    }
}

You can see that I created a SpriteBatch, that I'll use to draw all the graphics of the game, and an AssetManager, that will be useful later to load the assets, when I'll have assets. Then you can see that in the create() I call this.setScreen(new GameScreen(this));, thus the game display the game screen. Later I'll have more screens, like a LoadingScreen that I'll display when I load the assets, and a MainMenuScreen.

Important : In the render(), don't forget to do super.render(); !

And here is what the freshly created GameScreen class look like.

GameScreen.java :

public class LoadingScreen implements Screen{
    final MyGdxGame game;
    
    public LoadingScreen(final MyGdxGame game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stub      
    }

}

Now let's run this project !


This class displays absolutely nothing for now !

What am I waiting to make this class displaying an amazing game ?

Well... it's not that simple. My project will involve physics, thus I'll use Box2D, and to create the levels, I'll use Tiled. Therefore, before having fun with creating level and game mechanics, I need to integrate Box2D and Tiled to the project.

Integrating Box2D and Tiled to the project

Tiled

Tiled is trully an amazing level editor. Not only you draw beautiful maps using your tileset, but you can also draw shapes, like rectangles, ellipses, polygones, lines that Box2D can convert into bodies to integrate in the game.

Here is an illustration of the logic :


So where do we start ?

I'll go in the order of the logic exposed above, thus I'll start by drawing a quick map with Tiled, and then we'll see code. A LOT.

Here is the map, called Level 1 :

In this screenshot, in the right column, at the top, I created 3 layers : Objects, Spawn and Tile Layer 1.

  • Objects : This is an object layer. In object layers you can draw polygons and put images. In this layer I draw the polygons that will be converted to bodies by Box2D.
  • Spawn : This is also an object layer. In this layer I put an image from my tileset, that will represent the hero spawn points. For now my tileset only contains this red dot as I didn't draw anything yet. If I click on the red dot, I can give it a name (right column top) I call it Tom. That'll be important later.
We are done with Tiled for now. Now let's create bodies with Box2D !