Writing a Game Engine


This is the second post in the series and I suggest you to read the Basics of Game Development first.

There are two important things in this game.

  • Drawing the Game Canvas
  • Writing a Game Engine (The Game FSM – Finite State Machine)

In this post we will discuss about both.

The Game Canvas

The Game Canvas is a rectangular area on which we will draw all our game objects. It will have a predefined size (WIDTH and HEIGHT). The best way to make our game canvas is to extend the java.awt.Canvas class.

Now let us create a frame and add our game canvas to the it and also set the defaults as follow:

If you run the game now, you can see the blank game window of specified size. You can also see that the window cannot be resized. If you click on the cross button on the top right corner, it will close and the game stops.

Also, we would like to bring our focus on the game window as soon as the game launches, this can be done using requestFocusInWindow(); method of the Canvas class.

Understanding BufferStrategy

Drawing an image on a screen is a complex task. The process is to have a buffer which has the content to be drawn on the screen. Once the buffer is drawn, it is filled with the content to be drawn next and so on. This helps in speeding up the rendering process.

Now, the good thing is that we can have multiple buffers to speed up further, lets say two. In such a scenario when the content of the first buffer is rendered the second buffer is in the queue. And by the time the second buffer is being rendered the first buffer will be filled by the next content.

This is called double buffering.

Similarly we can have triple buffering in this game. We can keep increasing this number but at some point we will land up in a situation where the cost of maintaining these many buffers will slow down the game and hence we will not achieve that speed up.

In the render method of our game we will create a buffer strategy if it doesn’t already exist. The buffer strategy will create a graphics context for this buffer strategy as explained below.


Understanding the Graphics Object

The Graphics object contains all the draw methods. Anything which we draw on the screen, we will need the graphics context of the existing buffer strategy.

After the drawing is complete, the graphics context must be disposed and the buffer must be replenished with the next available buffer.
Let us draw an image on our canvas.

The Game FSM (Finite State Machine)

Ideally any game system is a Finite State Machine. There are many terms for this FSM, people also call it the game engine. The idea is to create a robust game engine which manages different states and new states are easily pluggable.

Now you can define the states as granular as possible. Here is what I think can be a good set of states:

  • The Start State
  • The Menu State
  • The Play State
  • The Pause State
  • The Over State
  • The Save State
  • The Quit State

We can define it more granular like all the menu items can represent individual states. This will help in easy menu navigation. We will create a GameFSM class and a StateName enum.

The StateName enum

The GameFSM class
The Game FSM is the game engine, which basically manages the state transition. Let the first state be the Start State which basically shows the flash screen of the game.

All these states inherit the BaseState which declares the on() and off() method for each of the states. Also, each of the states has a tick() and a render(Graphics g) method. The tick method is synced up with the game’s tick method and the render method is synced up with the game’s render method.

This GameFSM is initialized from the Game class and the only state it currently has is the StartState. Initialize the GameFSM in the Game class’ init() method.

Sync up the tick methods and the render methods from the Game class to the GameFSM.

Now if you sync up the code from github repository and run the game, you will see a black game screen and in the console, you can see tick and render messages from the StartState.


We learnt a but about the Graphics, BufferStrategy and the Game Engine(FSM). Next we will explore the other states and try to load background images in the game and will get ready with a complete menu.

If you faced issues understanding this post, please add a comment and I would try to add more details.

Don’t forget to subscribe to TechieMe to get updates on latest posts.