1. Writing your first game on a game engine

When implementing any computer game, there are three stages:

  1. Initializing the game. This includes various preparatory actions: setting the size of the playing field and drawing it, creating and setting up game objects in their initial position, as well as any other actions that need to be performed at the beginning of the game.
  2. Playing the game. This may include moving game objects, player actions, tallying points earned, as well as any other actions that must be performed at regular intervals or in response to button clicks and key presses.
  3. Ending the game. This could include stopping animations, a win/lose message, and any other actions that need to be performed at the end of the game.

We'll now walk through all three stages in order and see how the CodeGym game engine can help make this process easier.


2. Initializing the game

When you use the CodeGym game engine, game initialization consists of only two steps:

Step 1: Create the game's main class

To create your own game based on the CodeGym game engine, you need to create a class and make it extend the Game class (com.codegym.engine.cell.Game). This will give your class the ability to call methods on the game engine, and the engine will be able to call methods of your class. Example:

import com.codegym.engine.cell.Game;

public class MySuperGame extends Game {
  ...
}

Step 2: Override the initialize() method

In the initialize() method, you perform all the actions necessary to start the game: create the playing field, create all game objects, etc. You just need to declare this method in the class that inherits the Game class. Example:

import com.codegym.engine.cell.Game;

public class MySuperGame extends Game {
  @Override
  public void initialize() {
     // Here we perform all the actions to initialize the game and its objects
  }
}

The initialize() method is analogous to the main() method. It is the entry point from which all your game code begins to be executed.



3. Creating the playing field

Creating the playing field is also a two-step process:

Step 1: Divide the playing field into cells

The game engine divides the entire playing field into cells. The minimum size is 3×3, and the maximum is 100×100.

The size of the playing field is constant once created. The horizontal and vertical dimensions do not have to be the same. For example, a width of 7 and a height of 9:

Please note that cell numbering starts from the upper left corner.

To set the size of the playing field, use the void setScreenSize(int width, int height) method. It sets the size of the playing field. Its parameters are the number of cells in the horizontal (width) and vertical (height) dimensions. It is usually called once when the game starts. Example:

import com.codegym.engine.cell.Game;

public class MySuperGame extends Game {
   @Override    public void initialize()
   {
      // Set the field size to 7x9 cells
      setScreenSize(7, 9);
      ...
   }
}

When writing a game, you may need to get the current width and height of the playing field. The corresponding methods are int getScreenWidth() and int getScreenHeight().

Step 2: Turn the grid on/off (optional)

If you don't like having a black grid separating the cells of your playing field, then you can turn it off.

The void showGrid(boolean isShow) method enables/disables the grid that separates the cells. The grid is displayed by default. To turn it off, call this method and pass in false as the argument:

showGrid(false);

Result:

To turn the grid back on, call the method like this:

showGrid(true);

Result:



4. Primitive program

Let's write a very primitive game that uses the CodeGym game engine. It will do 3 things:

  1. It will divide the playing field into 9 cells: 3×3
  2. It will disable the grid (the lines between cells)
  3. The central cell will be painted blue and the letter X will be written in it.

The final code looks like this:

public class MySuperGame extends Game
{
   @Override
   public void initialize()
   {
      // Create a 3x3 playing field
      setScreenSize(3, 3);
      // Disable displaying the grid
      showGrid(false);
      // Change the background of the central cell to blue and display "X" in it
      setCellValueEx(1, 1, Color.BLUE, "Х", Color.ORANGE, 50);
   }
}

In this example, the playing field is set to 3x3, the grid is turned off, and an orange letter X at half of the cell height is put in the center cell with a blue background. This will be the first thing the player sees when the game starts.