A New Game

Learn how to create a new game with the Kakara Engine.

Now that the engine is installed and ready to go, we can begin coding a game.

Main Game Class

To start off with we need to create a new class that represents the main class for our game. It is normal for us to call this class the name of the game. In this example it will be KakaraExample. This class needs to implement the Game interface.

No graphics can be used in this class. This class is meant for cross scene functionality.

import org.kakara.engine.Game;
import org.kakara.engine.GameHandler;
import org.kakara.engine.scene.Scene;

public class KakaraExample implements Game {

    @Override
    public void start(GameHandler gameHandler) throws Exception {
        
    }

    @Override
    public Scene firstScene(GameHandler gameHandler) throws Exception {
        return null;
    }

    @Override
    public void update() {

    }

    @Override
    public void exit() {

    }
}

This class allows you to control the behavior of the game through out the game's runtime.

Start Method

The start method is called when the game first starts. As the warning about states, nothing involving graphics can be called in this method.

This method is best used for storing an instance of the GameHandler.

@Override
public void start(GameHandler gameHandler) throws Exception {
    this.gameHandler = gameHandler;
}

Update Method

The update method is called every frame. It is meant for cross scene functionality. We will not do anything with this update method in our tutorial.

Exit Method

This method is called when the game is terminated by the user. This could be triggered by the user closing the window, killing the game in Task Manager, or the use of gameHandler.exit(). The GameHandler provides us with the ability to exit the game peacefully, so closing the game that way is recommended.

All of the Engine's systems have been cleaned up by the time this method has been called. It is not safe to call any of the Engine's methods.

@Override
public void exit() {
    System.out.println("The game has been terminated!");
}

Game Scene

The engine operates using a Scene (level) system. Scenes contain all data related to game items, lighting, models, etc. There must be an active scene at all times, so we must create a scene for our game. For now we will have the main game scene be the default scene. In the future we will go back and make a title scene.

To begin we need to create a new class called MainScene. That class will act as the scene our game will function in. MainScene then needs to extend AbstractGameScene.

import org.kakara.engine.GameHandler;
import org.kakara.engine.scene.AbstractGameScene;

public class MainScene extends AbstractGameScene {

    public MainScene(GameHandler gameHandler) {
        super(gameHandler);
    }

    @Override
    public void work() {

    }

    @Override
    public void loadGraphics(GameHandler gameHandler) throws Exception {

    }

    @Override
    public void update(float v) {

    }
}

Work Method

Graphics cannot be initialized in this method. Use the loadGraphics method instead.

This method is called before the initialization of the render system. In our tutorial we will leave this method blank.

Load Graphics Method

This is the important method of every scene class. This is where the graphics of the game are created. Textures, Game Items, Meshes, and UI Elements, are created in this method for the scene. For now we will leave this method blank so we can create an empty window.

Update Method

This method is called every frame for the game. Any code that needs to execute every frame that is not item specific is ran here. Code that is item specific (like player movement) is done through the Component system. We will also leave this method blank for now.

Settings the Default Scene

Now that we have our main scene created we need to tell the engine to use that scene by default. We do that by going back to our KakaraExample class and changing the firstScene() method to return an instance of our scene.

@Override
public Scene firstScene(GameHandler gameHandler) throws Exception {
    return new MainScene(gameHandler);
}

The Main Method

Now it is time to create the class that contains the Main method for the engine. We will put our main method in a new class called Main

import org.kakara.engine.GameEngine;

public class Main {
    public static void main(String[] args) {
        KakaraExample kakaraExample = new KakaraExample();
        GameEngine gameEngine = new GameEngine("Kakara Example Game", 1080, 720, true, kakaraExample);
        gameEngine.run();
    }
}

In the code above we are creating an instance of our KakaraExample class. We then construct the main game engine class with its parameters.

The first parameter is the title of the game. The second and third parameter are the width and height of the window respectively. The fourth parameter is if you want vsync enabled. The last parameter is the instance of your class that implements the Game interface.

Finally, the game is ran via gameEngine.run();.

Now when you run the code you should see the following pop up:

Debugging Problems

If you experience any issue be sure to check out the section on debugging. That section will tell you how to enable engine logs so you can figure out the problem.

Final Code

import org.kakara.engine.GameEngine;

public class Main {
    public static void main(String[] args) {
        KakaraExample kakaraExample = new KakaraExample();
        GameEngine gameEngine = new GameEngine("Kakara Example Game", 1080, 720, 
            true, kakaraExample);
        gameEngine.run();
    }
}

Last updated