-
-
Notifications
You must be signed in to change notification settings - Fork 559
Basic Game Example
In this tutorial we are going to create a very basic game. I assume that you have completed Setting up FXGL one way or another and have a Java project in your IDE that has access to the latest version of the FXGL library.
First and foremost, let's define some requirements for our simple game:
- A 600x600 window.
- There is a player on the screen, represented by a blue rectangle.
- The user can move the player by pressing W, S, A or D on the keyboard.
- UI is represented by a single line of text.
- When the player moves, the UI text updates to show how many pixels the player has moved during his lifetime.
Although it may not sound like a game, it will help you understand the basic features of FXGL. After you have finished this tutorial, you should be able to build a variety of simple games.
Now that we have a rough idea of what we are expecting from the game, we can go back to the IDE and create a package for our game. (Note: the directory structure is similar to the Maven directory structure, however, if you don't know what this is, don't worry. We will cover the structure at a later stage. At this point having "src" as the main source directory is sufficient). I'm going to use "com.myname.mygame" as the package name, where myname can optionally be replaced with your username and mygame with the game name.
- Create package "com.myname.mygame" in your IDE.
- Create a Java class name BasicGameApp in that package.
It is quite common to append "App" to the class where your main()
is. This allows other developers to easily identify where the main entry point to your game / application is.
In order to use the FXGL library you need to extend GameApplication and override its abstract methods. The most straightforward way is to make your main class (BasicGameApp that we created) extend it as follows:
public class BasicGameApp extends GameApplication {
@Override
protected void initSettings(GameSettings settings) {}
@Override
protected void initInput() {}
@Override
protected void initAssets() {}
@Override
protected void initGame() {}
@Override
protected void initPhysics() {}
@Override
protected void initUI() {}
@Override
protected void onUpdate(double tpf) {}
}
Most IDEs will generate the overridden methods automatically, as soon as you extend GameApplication. Now we want to be able to start the application. To do that simply add the following:
public static void main(String[] args) {
launch(args);
}
If you've done any JavaFX programming before, then you'll notice that it is the exact same signature that we use to start a JavaFX application. In a nutshell, FXGL is a JavaFX application with game development features, nothing more. At this point you should already be able to run your game, but first let's tweak some settings.
@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(600);
settings.setHeight(600);
settings.setTitle("Basic Game App");
settings.setVersion("0.1");
settings.setIntroEnabled(false); // turn off intro
settings.setMenuEnabled(false); // turn off menus
}
As you can see all the settings are changed within initSettings()
. Once they are set, the settings cannot be changed during runtime (we'll talk about them in more detail some other time). Since this is supposed to be a very basic game, we are going to turn off intro and menus. Ok, you can now click run in your IDE, which should start the game with a 600x600 window and Basic Game App as a title. So we now achieved our requirement 1. Next step is to add a player and show him on the screen. We are going to do this in initGame()
. In short, this is where you set up all the stuff that needs to be ready before the game starts.
private GameEntity player;
@Override
protected void initGame() {
player = Entities.builder()
.at(300, 300)
.viewFromNode(new Rectangle(25, 25, Color.BLUE))
.buildAndAttach(getGameWorld());
}
If you are not familiar with fluent API, then this might be quite a lot to take in one go. So we'll start slowly. There is an instance level field named player of type GameEntity. A game entity is basically a game object.
TODO...