1. How to write games in JavaRush
As you already understand, JavaRush has a unique opportunity - writing games . They are significantly larger than ordinary tasks, and much more interesting. It’s interesting not only to write them, but also, um, to test them. If you know what we mean ;) Work in the JavaRush office was literally paralyzed for several days when we began the testing phase of game tasks :) Each game task is a project: a large task with two dozen subtasks. As you write the game, you will need to complete them sequentially. When the last subtask is done, your game is ready. It will use a very simple game engine called JavaRush . Working with it is no more difficult than with the console. A description of the game engine and examples of working with it can be found in this document .2. Game engine capabilities
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 a certain color and some text can be written in it. Also, for each cell the size and color of the text are set. The engine allows you to write handler methods for events such as “mouse button presses” and “keyboard presses”. Another interesting thing to note is the ability to work with a timer. You will learn more about this in the “Working with the timer” section. This “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 on the JavaRush website, select the one you like and go to its page. There will be a button “ Write your solution ” - click it. Your WebIDE will open and you will start working on the first subtask of the game. Also, from this moment on, the game’s subtasks will become available to you in Intellij IDEA, through the plugin. If you are using Intellij IDEA , you will need to update the plugin. You can download the latest version of the plugin here . After updating the plugin, open the task list and select "Java Games Quest". Next, click on the available subtask: the Java Games module should appear in your project, and in it - the engine library and the code of your subtask. Further actions are the same as when solving other problems. Games can be written in either WebIDE or Intellij IDEA, whichever you prefer. But Intellij IDEA is still more convenient. And more professional. The choice is yours.4. Publishing games in the application directory
When you finish writing a game, you can publish it in the Games-and-Apps directory on JavaRush. Just click the “Publish” button, and after half a minute your game will be added to the “Published Games” section . If you want to share your game implementation with friends or colleagues, nothing could be easier. Just send them a link to the personal page of the published game and let them play. Registration on JavaRush is not required for this. You, as the creator, can admire the number of gaming sessions. Just like the number of views on YouTube. The bigger, the better.5. Game customization
Once you've 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 as you wish. You can add something new altogether. For example, in the game Snake, the snake can slow down if it eats an apple while it is still fresh (within the first 5 seconds after it appears). At the same time, the apple can change color from red to green, or become a pear. Or suddenly your snake loves rabbits more than apples... In Minesweeper you can add a second life to the player, or, for example, an atomic bomb, which will “illuminate” cells within a radius of several cells. But remember: if you add files or graphics to your game other than through the game engine, it may not be published in the application directory. Not everything can be run in a browser, you know.6. Useful documentation
So that you can get acquainted with the work of the JavaRush game engine (initializing the game, creating the playing field and working with graphics), event processing (working with the mouse, keyboard and timer), as well as refresh or learn the basic theory of Java, which will be useful in writing tasks - games (the first and second JavaRush quest), we have prepared several detailed documents:- Section "Games" on JavaRush: Description of the game engine
- Section "Games" on JavaRush: Event Handling
- Section "Games" on JavaRush: Useful theory
7. Common problems
I'm on Linux and using 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 does not have it installed by default. This needs to be fixed:
-
At the command prompt, enter the command:
sudo apt-get install openjfx
-
After that, go to the project settings (alt+ctrl+shift+s) -> SDK's -> Classpath and click on the plus sign on the right. Select the jfxrt.jar file. It is located in the installed JDK at the path: <JDK_PATH>/jre/lib/ext/jfxrt.jar
-
Click OK.
-
Download the JavaFX Windows SDK from https://gluonhq.com/products/javafx/ .
-
Unpack the downloaded archive into any folder (preferably into the lib folder of the Games project).
-
Open IDEA.
-
In IDEA, go to the File -> Project Structure menu.
-
Select the Libraries tab and click + -> Java.
-
Specify the path to the unpacked javafx-sdk folder and select the lib folder
-
Next, click OK and in a new window, add JavaFX to the Games module.
-
Now a new library should appear, click Apply -> OK.
-
To launch correctly, open the Run-> Edit Configuration menu and enter the 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 the Games module
- Enter the path to the main class (in this case -
SnakeGame
) - Add a VM options field
- Enter VM options in the same way as in point 9.
Click Apply -> OK
-
Launch the game.
GO TO FULL VERSION