1. Working with the cells of the playing field
It's great that we can divide the playing field into cells. But what can we do with the cells themselves?
For each cell of the playing field, we can set:
- cell color (the cell's background color);
- text (this can be text or a number);
- text color;
- text size as a percentage of the cell size.
Let's consider methods for working with the cells of the playing field:
void setCellColor(int x, int y, Color color)
sets the color of the cell with coordinates (x, y)
equal to color
.
Examples:
setCellColor(0, 0, Color.RED);
setCellColor(3, 6, Color.BLACK);
setCellColor(6, 8, Color.NONE);
Color getCellColor(int x, int y)
returns the color of the cell with coordinates (x, y)
.
Example:
Color myColor = getCellColor(2, 0);
void setCellValue(int x, int y, String value)
assigns the text in String value
to the cell with coordinates (x, y)
.
Examples:
setCellValue(3, 3, "text");
setCellValue(0, 8, "W");
setCellValue(4, 1, "2222");
setCellValue(6, 6, "");
String getCellValue(int x, int y)
returns the text contained in the cell with coordinates (x, y)
.
Examples:
String s = getCellValue(3, 3);
System.out.println(getCellValue(4, 1));
void setCellTextSize(int x, int y, int size)
sets the size of the text in the cell with coordinates (x, y)
, where size
is the text height as a percentage of the cell height.
Example:
setCellTextSize(2, 0, 70); // 70% of the cell height
int getCellTextSize(int x, int y)
returns the size of the content in the cell with coordinates (x, y)
.
Example:
int size = getCellTextSize(2 , 0);
void setCellNumber(int x, int y, int value)
assigns the number int value
to the cell with coordinates (x, y)
.
Examples:
setCellNumber(3, 3, 40);
setCellNumber(0, 8, -8);
setCellNumber(4, 1, 2222);
setCellNumber(6, 6, 0);
int getCellNumber(int x, int y)
returns the number contained in the cell with coordinates (x, y)
. If the cell does not contain a number, it returns 0.
Examples:
int i = getCellNumber(3, 3);
System.out.println(getCellNumber(4, 1));
void setCellTextColor(int x, int y, Color color)
sets the color of the content (text) of the cell with coordinates (x, y)
.
Examples:
setCellTextColor(2, 1, Color.GREEN);
setCellTextColor(0, 1, Color.NONE);
Color getCellTextColor(int x, int y)
returns the color of the content (text) of the cell with coordinates (x, y)
.
Example:
Color textColor = getCellTextColor(1, 3);
For your convenience, there are multiple setCellValueEx()
methods with different sets of parameters:
void setCellValueEx(int x, int y, Color cellColor, String value)
sets the background color and text of the cell with coordinates (x, y)
equal to cellColor
and value
, respectively.
Example:
setCellValueEx(0, 2, Color.BLUE, "56");
void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor)
sets the background color, text, and text color of the cell with coordinates (x, y)
equal to cellColor
, value
, and textColor
, respectively.
Example:
setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN);
void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor, int textSize);
sets the background color, text, text color, and text size of the cell with coordinates (x, y)
equal to cellColor
, value
, textColor
, and textSize
, respectively.
Example:
setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN, 70);
2. Working with color
The CodeGym game engine has a special Color
type that contains unique values for 148 colors. It also has a special NONE
value that represents the absence of color.
Examples of working with color
Color myColor = Color.WHITE; // The color white is assigned to the myColor variable.
Color redColor = Color.RED; // The color red is assigned to the redColor variable.
Color blueColor = Color.BLUE; // The color blue is assigned to the blueColor variable.
You can color a cell red using the command:
setCellColor(0, 2, Color.RED);
You can check whether a cell is a certain color with a command like:
if (getCellColor(0,2) == Color.GREEN)
{
}
Sometimes you may need to get an array of every possible color. To do this, use the values()
method.
Example:
// An array containing every available color is assigned to the colors variable.
Color[] colors = Color.values();
Getting a color's index in the color palette is very easy to do — just use the ordinal()
method:
Color color = Color.RED;
int redIndex = color.ordinal(); // Index of the color red
int blueIndex = Color.BLUE.ordinal(); // Index of the color blue
You can also easily get a color by its index:
// The color whose index is 10 in the Color enum is assigned to the color variable.
Color color = Color.values()[10];
3. Dialog boxes
At the end of the game, we need to let the player know if he or she won or lost. For this and other occasions, the CodeGym game engine has the special void showMessageDialog(Color cellColor, String message, Color textColor, int textSize)
method, which displays a dialog box with the message
message.
The parameters of this method are:
cellColor
is the dialog box's background colormessage
is the text of the messagetextColor
is the color of the text of the messagetextSize
is the size of the text of the message
The dialog box closes itself if the user presses the space bar or clicks on the dialog box with the mouse.
Example of calling this method:
// Display a dialog box with a message
showMessageDialog(Color.BLACK, "EPIC FAIL", Color.RED, 80);
4. Utility methods
When writing games, you'll use random numbers often. To make it easier to get random numbers, you can use the game engine's utility methods:
int getRandomNumber(int max)
returns a random number from 0
to (max–1)
inclusive.
int getRandomNumber(int min, int max)
returns a random number from min
to (max–1)
inclusive.
5. JDK 11+
When running your program from IntelliJ IDEA, a class that inherits the Game class may generate the following error:
Error: JavaFX runtime components are missing, and are required to run this application
In this case, for each such class, you need to perform these steps once:
- Open Run → EditConfiguration
- For the value of VM options, enter the following:
--module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base
ATTENTION:
In recent versions of IntelliJ IDEA, the "VM options" field is not shown by default. To display it, press ALT+V
- Press: Apply → OK
- Run the game.
GO TO FULL VERSION