Basics of Game Development

Introduction

Everyone is fond of computer games and most of us wish to create games (professionally or even just for fun). Here, in this series of posts I will be explaining the Basics of Game Development, the approach and code from scratch for the first game I developed.

About the Game

Basics of Game Development

If you want to try what we will develop, then you can click on the above image, download the game and play it to have a good understanding. You can download the .exe file and straight away launch it to run.

This is a simple arcade game where you are facing an Alien Invasion and you are given a fighter plane to attack and kill all the Aliens. Also, with increasing levels you get special powers and helps and of course the alien ships also get powerful.

Prerequisites

To follow and learn through this tutorial, you just need to understand the basics of Java Programming language and any image editing tool (I prefer Paint.NET because that is easy to use).
Apart from that, if you want to develop any other game, you need a good enough story to engage the players.

This game is called Space Adventure and it has a story as well. Please notice that this game is for educational purpose only, it might not be the best architecture but yes it is scalable and easy to understand.

The Space Adventure Controls

When the game starts then you are given a basic fighter plane at the bottom of the screen and you see few enemy planes coming down from the top.
Movement
You can move your plane in any direction from left to right, bottom to top, diagonally and almost in any possible direction where the keys allow.
Attack
You can press the Space bar to shoot bullets.


The Space Adventure Story

You start playing at level one where you get very less enemy planes and they are easy to destroy. Once all the enemy planes in level one are destroyed, you enter level two where you face more number of enemy planes. The story continues.

In case you encounter a collision with one of the enemy planes, your health decreases and it can be seen at the Heads Up Display at the bottom of the screen.
When an enemy is killed your score is incremented and that is displayed in the HUD at the right hand corner.

When you reach a sufficiently higher level, you get health capsules dropping from the top, upon collecting the health capsules your health increases.

On a further higher level, you get different powers like plane upgrades and supporting fighter planes etc. This turns out to be a rigorous fight and your enemies now can shoot at you with powerful bullets as well.

The Initial Setup

We will start drawing a Canvas of a given size (WIDTH * HEIGHT). This is the stage of our game. If we want to show something in the game, we will add it to a queue named RenderQueue, if we want to hide something we will remove it from the RenderQueue. This RenderQueue will be rendered on the game screen or canvas.

The Game Loop

How do we force a program to always show something on the screen, always listen to a key press or a mouse event and always respond to a voice command?

In most of the cases, at the background we are running an infinite loop with some terminating condition. The terminating condition can we pressing the Close Icon or choosing the Quit option in the main menu.

This infinite loop is called the game loop. Other responsibility of the loop is to maintain a game clock which ticks a pre decided number of times per second. Mostly this is fixed somewhere around 60 ticks per second and you guessed it right.

When the game clock ticks, it updates the state of every object in the game. Depending on how much work you are doing in one iteration of the loop you might end up having millions or may be thousands of iteration of the loop per second.

Also, in each iteration of the game loop, it renders all the objects from the RenderQueue to the screen or Canvas.

Now it really doesn’t make much sense to tick the clock in every iteration of the loop, because if the game objects are updated in every iteration, it will increase the game speed and the screen will render the updated stuffs every iteration and it will be impossible to play due to the faster speed.

We will understand this nicely, when we have the game code ready and you can regulate the updates by ticking the game clock 30 times a second, 60 times a second and 100000 times a second.

Basic Code for the Game Loop

Here is the basic code structure and the game loop and we will build upon it in coming sections.


Understanding the Code

It is pretty simple to realize that we need the game to run in a different dedicated thread, hence we create a task to run and submit it to a thread. The Runnable task has a run method and the run method will contain the game loop, which will run infinitely, until we stop it.

Let us have a running flag which is true and the while loop runs until the flag turns false by some other piece of code. This is our infinite loop. There are two important tasks we need to preform inside the loop.

  • tick – which has the responsibility to update the state of all the objects in the game.
  • render – which has the responsibility to render the updated objects.

The rendering can also happen only 60 times a second or in each iteration of the loop. But an interesting aspect is when to update the states. As discussed above we will update only 60 times a second. Here we discuss how that is achieved.

Let us have two variables currentTime and lastTime and in each iteration find the difference of these two times and multiply by requiredFramesPerSecond add add it to the delta variable. When the delta is greater that 1 second we will invoke the tick method and update the state of all the associated objects and also reduce delta.

Note **We are measuring all the times in nano seconds.


Why not invoke tick in each iteration of the game loop

The game loop runs around 4 million times a second for an empty loop. If we do some graphics work it will come down to around 600 times a second. The tick() method updates the state of the object (say Player) every time it is invoked.

Imagine in every tick we increase the x co-ordinate of the Player by 1, then in one second the X co-ordinate will be increased by 600 and the user will not be able to see the player on the screen. That is why we cannot invoke tick in each iteration.

Experimenting with the game loop

You can run this code with different values of requiredFramesPerSecond if you want to invoke the tick method more or less number of times per second.

You can get a runnable code @github.

Conclusion

This post just touched the surface of Game Development. We will discuss more on the FSM nature of games and will learn about graphics and display in coming posts.

In case you have doubts understanding any part of this post, please comment back.

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