JavaRush /Java Blog /Random EN /New section JavaRush - "Games"

New section JavaRush - "Games"

Published in the Random EN group
We have great news! CodeGym has a new section - "Games" , which is now open to all users. Here, each of you can write a full-fledged game with graphics and feel like a real game developer. The section is currently in beta testing, so it's very important for us to hear your opinion on how to make it even cooler! Try, test, leave feedback and suggestions under this post! Briefly about the main features of the "Games" section, you will learn from our video. Enjoy!
Now we offer you to get acquainted with the possibilities of the new section in more detail and get answers to the main questions that may arise when writing tasks-games.

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. New section CodeGym - "Games" - 1There will be a button “ Write your solution ” – click it. New section CodeGym - "Games" - 2You will open WebIDE, in which you will start working on the first subtask of the game. Also, from now on, the subtasks of the game will become available to you in Intellij IDEA, through the plugin. New section CodeGym - "Games" - 3If 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". New section CodeGym - "Games" - 4Next, 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 in solving other tasks. Games can be written even in WebIDE, even in Intellij IDEA, where you like best. But Intellij IDEA is still more convenient. And more professional. The choice is yours.

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 . New section CodeGym - "Games" - 5If 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 CodeGym is not required for this. New section CodeGym - "Games" - 6You, as the creator, can admire the number of game sessions. Just like the number of views on YouTube. The bigger, the better.

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:

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:
  1. At the command line, enter the command:sudo apt-get install openjfx

  2. 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

  3. Click OK.

I have Java 11. I can't launch the game. What to do? Java 11 does not have JavaFX. Therefore, when starting the game, the compiler will not be able to compile the game, and there will be an error. To fix the problem, you need to add JavaFX to the project:
  1. Download the JavaFX Windows SDK from https://gluonhq.com/products/javafx/ .

  2. Unzip the downloaded archive to any folder (preferably in the lib folder of the Games project).

  3. Open IDEA.

  4. In IDEA, go to the menu File -> Project Structure (File -> Project Structure).

  5. Select the Libraries tab and click + -> Java.

    New section CodeGym - "Games" - 7
  6. Specify the path to the unpacked javafx-sdk folder and select the lib folder

    New section CodeGym - "Games" - 8
  7. Then click OK and in the new window add JavaFX to the Games module.

    New section CodeGym - "Games" - 9
  8. Now a new library should appear, click Apply -> OK.

    New section CodeGym - "Games" - 10
  9. To start correctly, open the Run-> Edit Configuration menu and enter the following command in the VM options field:

    New section CodeGym - "Games" - 11
    --module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base
    New section CodeGym - "Games" - 11
  10. Next, in the same tab, you need to add Application. To do this, click + -> Application

    New section CodeGym - "Games" - 12
    1. Select Games module
    2. Write the path to the main class (in this case - SnakeGame)
    3. Add VM options field
    4. New section CodeGym - "Games" - 13
    5. Write VM options in the same way as in point 9.

    Click Apply -> OK

    New section CodeGym - "Games" - 14
  11. Start the game.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION