"Games" from CodeGym is a new section with big tasks that involve writing popular computer games. They are easier to create than it seems: each project is divided into twenty subtasks. Completing tasks step by step, you'll write your own game, and then you can add unique features and share it with friends.The games use the simple CodeGym game engine. In this article, we'll describe its key features and what the game-writing process looks like.

1. Introduction

For the developer, there are three stages to the implementation of a computer game:
  1. Game initialization — This stage consists of preparatory actions: setting the size of the playing field, drawing the playing field, creating game objects and putting them in their initial positions, and other actions that must be performed at the beginning of the game.

  2. Game process. This stage includes moving game objects, player actions, keeping score, and other actions that must be performed at a certain frequency or when buttons are pressed.

  3. Game completion. This stage includes stopping animation, reporting a win or loss, and other actions that must be performed at the end of the game.

2. Game initialization

Game initialization consists of only two steps: Step 1: Create the game's main class. To develop your own game using the CodeGym game engine, you need to create class that inherits the Game class (com.javarush.engine.cell.Game). This gives your class the ability to call the game engine's methods, and gives the engine the ability to call your methods. For example:
import com.javarush.engine.cell.Game;

public class MySuperGame extends Game {
    ...
}
Step 2: Override the initialize method(). All actions required to start the game will happen in this method: creating the playing field, creating game objects, etc. You simply need to declare this method in the class that inherits the Game class. For example:
import com.javarush.engine.cell.Game;

public class MySuperGame extends Game {

    @Override
    public void initialize() {
        // Here we perform all actions to initialize the game and its objects
    }
}
The initialize() method is analogous to the main() method: it is the starting point for all the code written for the game.

3. Creating a playing field

Creating a playing field also consists of only two steps. Step 1: Divide the playing field into cells. The entire playing field is divided into cells by the game engine. The minimum size is 3x3; the maximum is 100x100. The dimensions are of the game screen are constant. It can be divided into different numbers of cells. For example, 7 cells wide by 9 cells high: Note that cells are numbered starting from the top 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 represent the number of cells horizontally (width) and vertically (height). It is usually called once when the game is started. For example:
import com.javarush.engine.cell.Game;

public class MySuperGame extends Game {

    @Override
    public void initialize() {
       // Set the field size to 7 cells x 9 cells
       setScreenSize(7, 9);
        ...
    }
}
When writing a game, you may need to get the playing field's current width and height. To do this, the int getScreenWidth() and int getScreenHeight() methods will come in handy. Step 2: Turn the grid on or off (optional). If you don't like the black grid separating the cells on the playing field, you can turn it off. The void showGrid(boolean isShow) method turns the grid on and off. By default, the grid is displayed. To turn it off, call this method with false as the argument:
showGrid(false);
Result: To turn the grid back on, call:
showGrid(true);

4. A primitive program

Here's an example of a program:
public class MySuperGame extends Game {

    @Override
    public void initialize() {

        // Create a playing field that is 3 cells x 3 cells
        setScreenSize(3, 3);
        // Turn off the grid
        showGrid(false);
        // Change the background of the center cell to blue, and display an "X" in it.
        setCellValueEx(1, 1, Color.BLUE, "X", Color.ORANGE, 50);
    }
}
In this example, the game field is set to 3x3, the grid is turned off, and an orange "X" half the cell size is set on a blue background in the center cell. This will be the first thing the player sees when the game starts.

5. Working with cells of the playing field

The fact that we can divide the playing field into cells is great, but what we can do with the cells themselves? You can set the following properties for each cell of the playing field:
  • cell color (cell background color);
  • text (text or number);
  • text color;
  • text size as a percentage of the cell size.
Let's look at the methods for working with the cells of the playing field:
  1. void setCellColor(int x, int y, Color color) — sets the color of the cell with coordinates (x, y):

    setCellColor(0, 0, Color.RED);
    setCellColor(3, 6, Color.BLACK);
    setCellColor(6, 8, Color.NONE);

  2. Color getCellColor(int x, int y) — returns the color of the cell with coordinates (x, y):

    Color myColor = getCellColor(2, 0);

  3. void setCellValue(int x, int y, String value) — sets the text of the cell with coordinates (x, y) equal to the string in the value parameter:

    setCellValue(3, 3, "text");
    setCellValue(0, 8, "W");
    setCellValue(4, 1, "2222");
    setCellValue(6, 6, "");

  4. String getCellValue(int x, int y) — returns the text contained in the cell with coordinates (x, y):

    String s = getCellValue(3, 3);
    System.out.println(getCellValue(4, 1));

  5. void setCellTextSize(int x, int y, int size) — sets the size of the contents of the cell with coordinates (x, y). The size parameter is the text height as a percentage of the cell height:

    setCellTextSize(2 , 0, 70); // 70% of the cell height

  6. int getCellTextSize(int x, int y) — returns the size of the contents of the cell with coordinates (x, y):

    int size = getCellTextSize(2 , 0);

  7. void setCellNumber(int x, int y, int value) — sets the text of the cell with coordinates (x, y) equal to the number in the value parameter:

    setCellNumber(3, 3, 40);
    setCellNumber(0, 8, -8);
    setCellNumber(4, 1, 2222);
    setCellNumber(6, 6, 0);

  8. int getCellNumber(int x, int y) — returns the number contained in the cell with coordinates (x, y). If the cell doesn't contain a number, returns 0:

    int i = getCellNumber(3, 3);
    System.out.println(getCellNumber(4, 1));

  9. void setCellTextColor(int x, int y, Color color) — sets the color of the contents (text) of the cell with coordinates (x, y):

    setCellTextColor(2, 1, Color.GREEN);
    setCellTextColor(0, 1, Color.NONE);

  10. Color getCellTextColor(int x, int y) — returns the color of the contents (text) of the cell with coordinates (x, y):

    Color textColor = getCellTextColor(1, 3);
For convenience, there are several setCellValueEx() methods with different sets of parameters:
  1. void setCellValueEx(int x, int y, Color cellColor, String value) — sets the background color (cellColor) and contents (value) of the cell with coordinates (x, y):

    setCellValueEx(0, 2, Color.BLUE, "56");

  2. void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor) — sets the background color (cellColor), contents (value), and text color (textColor) of the cell with coordinates (x, y):

    setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN);

  3. void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor, int textSize) — sets the background color (cellColor), contents (value), text color (textColor), and text size (textSize) of the cell with coordinates (x, y):

    setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN, 70);

6. Working with color

The Color enum is responsible for colors in the game engine. It has unique values for 148 colors. It also has a special value (NONE) that represents no color. Here's an example of working with color:
Color myColor = Color.WHITE;  // The myColor variable is set to white.
Color redColor = Color.RED;  // The redColor variable is set to red.
Color blueColor = Color.BLUE;  // The blueColor variable is set to light blue.
Sometimes you may need to get an array of all existing colors. To do this, use the values() method. For example:
Color[] colors = Color.values();  // The colors variable is assigned an array containing all available colors.
It's very easy to get a color's index in the palette:
Color color = Color.RED;
int redIndex = color.ordinal();  // Index of red

int blueIndex = Color.BLUE.ordinal();  // Index of blue
You can also get a color by its index:
Color color = Color.values()[10];  // The color variable is assigned the color with index 10 in the Color enum.

7. Dialog boxes

At the end of the game, you need to report a win or loss to the player. To do this, there is a special method that displays a dialog box on the game screen:
void showMessageDialog(Color cellColor, String message, Color textColor, int textSize)
Here:
  • cellColor is the background color of the dialog box;
  • message is the text of the message;
  • textColor is the color of the message text;
  • textSize is the size of the message text.
The dialog box closes by itself if the user presses the space bar.

8. Utility methods

When writing games, random numbers are used a lot. To make it easier to get random numbers, you can use the following utility methods provided by the game engine:
  1. int getRandomNumber(int max) — returns a random integer from 0 to (max-1) inclusive.

  2. int getRandomNumber(int min, int max) — returns a random integer from min to (max-1) inclusive.

That's all for now! If you want to learn more about the "Games" section, here is some useful documentation that can help: