In this simple FXGL tutorial, we will develop a game called “Space Ranger”.
在这个简单的FXGL教程中,我们将开发一个名为“ Space Ranger”的游戏。
什么是FXGL? (What is FXGL?)
FXGL is a game engine built on top JavaFX, which is a GUI toolkit for desktop, mobile, and embedded systems.
FXGL是基于顶级JavaFX构建的游戏引擎,JavaFX是用于台式机,移动和嵌入式系统的GUI工具包。
为什么在JavaFX上使用FXGL? (Why use FXGL over JavaFX?)
On its own, JavaFX provides general-purpose rendering and UI capabilities. On top of it, FXGL brings real-world game development techniques and tools, making it easy to develop cross-platform games.
JavaFX本身提供了通用的呈现和UI功能。 最重要的是,FXGL带来了现实世界的游戏开发技术和工具,使开发跨平台游戏变得容易。
如何下载FXGL JAR? (How to Download FXGL JARs?)
FXGL can be downloaded as a Maven or Gradle dependency. For example, Maven coordinates are as follows, which can be used with Java 11+.
FXGL可以作为Maven或Gradle依赖项下载。 例如,Maven坐标如下,可与Java 11+一起使用。
<dependency>
<groupId>com.github.almasb</groupId>
<artifactId>fxgl</artifactId>
<version>11.8</version>
</dependency>
If you get stuck at any point, you can find the full source code link at the end of this tutorial.
如果您有任何困难,可以在本教程的结尾找到完整的源代码链接。
太空游侠游戏 (Space Rangers Game)
Our game idea is relatively simple. We have a bunch of enemies who are trying to get to our base and we have a single base protector — the player. Given this is an introduction tutorial, we will not use any assets, such as images, sounds, and other external resources. When completed, the game will look like this:
我们的游戏思路比较简单。 我们有一堆试图进入我们基地的敌人,而我们只有一个基地保护者-玩家。 鉴于这是一个入门教程,因此我们将不使用任何资产,例如图像,声音和其他外部资源。 完成后,游戏将如下所示:
Let us begin!
让我们开始吧!
所需进口 (Required Imports)
First, let’s take care of all the imports, so we can focus on the code aspect of the tutorial. For simplicity, all of the code will be in a single file, however you may wish to place each class in its own file.
首先,让我们处理所有导入,因此我们可以专注于本教程的代码方面。 为了简单起见,所有代码都在一个文件中,但是您可能希望将每个类放在自己的文件中。
Create a file SpaceRangerApp.jav