1. How to write games in CodeGym
As you already understood, CodeGym has a unique opportunity - writing games . They are significantly larger than ordinary tasks, and much more interesting. It is interesting not only to write them, but also, um, to test them. If you understand what we mean ;) Work in the CodeGym office was literally paralyzed for several days when we started the testing phase of task-games :) Each task-game is a project: a large task with two dozen subtasks. In the process of writing a game, you will need to execute them sequentially. When the last subtask is done, your game is ready. It will use a very simple CodeGym game engine . Working with it is no more difficult than with the console. Description of the game engine and examples of working with it can be found in this document .2. Features of the game engine
The playing field is divided by the game engine into cells. The minimum size is 3x3, the maximum is 100x100. Each cell can be painted in a certain color and write some text in it. Also for each cell set the size and color of the text. The engine allows you to write handler methods for events such as "pressing mouse buttons" and "pressing keys on the keyboard." Another interesting feature is the ability to work with a timer. You will learn more about this in the "Working with the timer" section. Such an "obvious engine" allows you to create very interesting games, as you will see for yourself. Want to check it out? Then read the next paragraph and start writing games.3. Gaining access to the game
To get access to writing games, you need to go to the "Games" section of the CodeGym website, select the one you like and go to its page.



4. Publish games in the application catalog
Once you've finished writing your game, you can publish it to the Games-and-Apps directory on CodeGym. Just click the "Publish" button, and after half a minute your game will be added to the "Published Games" section .

5. Game customization
After you have finished writing the game, you can modify it. Want 2048 on a 5x5 field? Please. You are a programmer - you have a keyboard in your hands. Change the game however you want. You can add anything new at all. For example, in the game Snake, a snake can slow down if it eats an apple while it is still fresh (within the first 5 seconds after it appears). An apple can change color from red to green, or become a pear. Or suddenly your snake loves rabbits more than apples... In "Sapper" you can add a second life to the player, or, for example, an atomic bomb that will "illuminate" cells within a radius of several cells. But be aware that if you add file or graphic manipulation to a game outside of the game engine, it may not be published to the application catalog. Not everything can be run in a browser, you know.6. Helpful Documentation
So that you can get acquainted with the work of the CodeGym game engine (game initialization, creation of the playing field and working with graphics), event handling (working with the mouse, keyboard and timer), as well as refresh or learn the basic theory of Java, which is useful in writing tasks - games (the first-second CodeGym quest), we have prepared several detailed documents:- Section "Games" on CodeGym: Description of the game engine
- Section "Games" on CodeGym: Handling events
- Games Section on CodeGym: Useful Theory
7. Common problems
I have Linux and use OpenJDK. When starting the game, the compiler throws an error:Error:(6, 8) java: cannot access javafx.application.Application
class file for javafx.application.Application not found
What to do? Our game engine uses JavaFX, and OpenJDK doesn't have it installed by default. This needs to be fixed:
-
At the command line, enter the command:
sudo apt-get install openjfx
-
After that go to project settings (alt+ctrl+shift+s) -> SDK's -> Classpath and click on the plus on the right. Select the jfxrt.jar file. It is located in the installed JDK along the path: <JDK_PATH>/jre/lib/ext/jfxrt.jar
-
Click OK.
-
Download the JavaFX Windows SDK from https://gluonhq.com/products/javafx/ .
-
Unzip the downloaded archive to any folder (preferably in the lib folder of the Games project).
-
Open IDEA.
-
In IDEA, go to the menu File -> Project Structure (File -> Project Structure).
-
Select the Libraries tab and click + -> Java.
-
Specify the path to the unpacked javafx-sdk folder and select the lib folder
-
Then click OK and in the new window add JavaFX to the Games module.
-
Now a new library should appear, click Apply -> OK.
-
To start correctly, open the Run-> Edit Configuration menu and enter the following command in the VM options field:
--module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base
-
Next, in the same tab, you need to add Application. To do this, click + -> Application
-
- Select Games module
- Write the path to the main class (in this case -
SnakeGame
) - Add VM options field
- Write VM options in the same way as in point 9.
Click Apply -> OK
-
Start the game.
GO TO FULL VERSION