Java打開,使用Java FXGL構建太空游俠游戲

 2023-11-19 阅读 21 评论 0

摘要: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 fo

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?)

Java打開、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.

JAVA使用? 如果您有任何困難,可以在本教程的結尾找到完整的源代碼鏈接。

太空游俠游戲 (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!

opengl游戲有哪些、 讓我們開始吧!

所需進口 (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.java and place the following imports:

創建一個文件SpaceRangerApp.java并放置以下導入:


import com.almasb.fxgl.animation.Interpolators;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.components.ProjectileComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.Spawns;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;import static com.almasb.fxgl.dsl.FXGL.*;

opengl java,That’s it, our first step is done. Note that the last static import simplifies a lot of calls made to FXGL and is the recommended approach.

就是這樣,我們的第一步已經完成。 請注意,最后一次靜態導入簡化了對FXGL的許多調用,這是推薦的方法。

游戲代碼 (Game Code)

We will now begin writing some code. As with any other application, we start by specifying the entry point to the program. We will also provide some basic settings to FXGL, such as width, height and the title of our game.

現在,我們將開始編寫一些代碼。 與任何其他應用程序一樣,我們首先指定程序的入口點。 我們還將為FXGL提供一些基本設置,例如寬度,高度和游戲標題。


public class SpaceRangerApp extends GameApplication {@Overrideprotected void initSettings(GameSettings settings) {settings.setTitle("Space Ranger");settings.setWidth(800);settings.setHeight(600);}public static void main(String[] args) {launch(args);}
}

游戲對象/實體 (Game Objects / Entities)

Each game engine has its own terminology for in-game objects. In FXGL, which uses the Entity-Component model, game objects are called entities. Each entity typically has a type, for example, the player, an enemy, a projectile and so on. In our game we are going to have exactly these types just described:

Javalist、 每個游戲引擎都有自己的用于游戲中對象的術語。 在使用實體組件模型的FXGL中,游戲對象稱為實體。 每個實體通常具有一種類型,例如,玩家,敵人,彈丸等。 在我們的游戲中,我們將恰好描述了以下類型:


public enum EntityType {PLAYER, ENEMY, PROJECTILE
}

FXGL needs to know how to construct these types. To do that we create a factory. As mentioned earlier we are keeping all classes in the same file. At this point, if you prefer, you can move the class “SpaceRangerFactory” into its own file (if you do, just remove the “static” keyword).

FXGL需要知道如何構造這些類型。 為此,我們創建了一家工廠。 如前所述,我們將所有類保留在同一文件中。 此時,如果您愿意,可以將“ SpaceRangerFactory”類移到其自己的文件中(如果這樣做,只需刪除“ static”關鍵字)。


public class SpaceRangerApp extends GameApplication {public static class SpaceRangerFactory implements EntityFactory {}
}

There is a streamlined process for defining entities. Inside “SpaceRangerFactory”, we define a new method that accepts a SpawnData object, returns an Entity object and has a Spawns annotation:

有一個簡化的流程來定義實體。 在“ SpaceRangerFactory”內部,我們定義了一個新方法,該方法接受一個SpawnData對象,返回一個Entity對象,并具有一個Spawns注釋:


@Spawns("player")
public Entity newPlayer(SpawnData data) {var top = new Rectangle(60, 20, Color.BLUE);top.setStroke(Color.GRAY);var body = new Rectangle(25, 60, Color.BLUE);body.setStroke(Color.GRAY);var bot = new Rectangle(60, 20, Color.BLUE);bot.setStroke(Color.GRAY);bot.setTranslateY(40);return entityBuilder().type(EntityType.PLAYER).from(data).view(body).view(top).view(bot).build();
}

javagame模擬器?As readily seen from the annotation, this method spawns a player object (entity). We will now consider the method in detail.

從注釋中可以很容易地看出,該方法產生了一個播放器對象(實體)。 現在,我們將詳細考慮該方法。

First, we construct the three parts of the player view, which are standard JavaFX rectangles. Note that we are using the “var” syntax. Using the “entityBuilder()”, we specify the type, the data from which to set the position and views of the player entity. This fluent API allows us to build entities in a concise way.

首先,我們構造播放器視圖的三個部分,它們是標準的JavaFX矩形。 請注意,我們使用的是“ var”語法。 使用“ entityBuilder()”,我們指定類型,用于設置玩家實體位置和視圖的數據。 這種流暢的API使我們能夠以簡潔的方式構建實體。

Our next step is to define the projectiles in our game:

opengl游戲, 我們的下一步是在游戲中定義彈丸:


@Spawns("projectile")
public Entity newProjectile(SpawnData data) {var view = new Rectangle(30, 3, Color.LIGHTBLUE);view.setStroke(Color.WHITE);view.setArcWidth(15);view.setArcHeight(10);return entityBuilder().type(EntityType.PROJECTILE).from(data).viewWithBBox(view).collidable().zIndex(-5).with(new ProjectileComponent(new Point2D(1, 0), 760)).build();
}

The view is, again, a JavaFX rectangle with rounded corners. This time we call “viewWithBBox()”, rather than just “view()”. The former method automatically generates a bounding box based on the view. Neat!

該視圖再次是帶有圓角的JavaFX矩形。 這次我們稱“ viewWithBBox()”,而不僅僅是“ view()”。 前一種方法會根據視圖自動生成邊界框。 整齊!

Next, using “collidable()”, we mark our projectile as an entity that can collide with other entities. We will come back to collisions later on. We set the z-index to a negative value so that it gets drawn before the player (by default each entity has z-index of 0).

接下來,使用“ collidable()”,將射彈標記為可以與其他實體碰撞的實體。 稍后我們將回到沖突中。 我們將z-index設置為負值,以便在玩家之前繪制它(默認情況下,每個實體的z-index為0)。

Java怎么打開?Finally, we add a ProjectileComponent, with a speed equal to 760 and a directional vector (1, 0), which means 1 in the X-axis and 0 in the Y-axis, which in turn means to move to the right.

最后,我們添加一個ProjectileComponent,其速度等于760,方向矢量為(1,0),這意味著X軸為1,Y軸為0,這又意味著向右移動。

Our last entity to define is of type enemy:

我們最后定義的實體是敵人類型:


@Spawns("enemy")
public Entity newEnemy(SpawnData data) {var view = new Rectangle(80, 20, Color.RED);view.setStroke(Color.GRAY);view.setStrokeWidth(0.5);animationBuilder().interpolator(Interpolators.SMOOTH.EASE_OUT()).duration(Duration.seconds(0.5)).repeatInfinitely().animate(view.fillProperty()).from(Color.RED).to(Color.DARKRED).buildAndPlay();return entityBuilder().type(EntityType.ENEMY).from(data).viewWithBBox(view).collidable().with(new ProjectileComponent(new Point2D(-1, 0), FXGLMath.random(50, 150))).build();
}

We already covered the fluent API methods, such as “type()” and “collidable()”, so we will focus on the animation instead.

opengl開發的知名游戲。 我們已經介紹了流暢的API方法,例如“ type()”和“ collidable()”,因此我們將重點放在動畫上。

As you can see, the animation builder also follows a similar fluent API convention. It allows us to set various animation settings, such as duration and how many times to repeat.

如您所見,動畫構建器還遵循類似的流利API約定。 它允許我們設置各種動畫設置,例如持續時間和重復次數。

We can observe that the animation operates on the “fillProperty()” on the rectangular view, which we are using to represent the enemy. In particular, the fill is animated from RED to DARKRED every 0.5 seconds. Feel free to adjust the animation settings to see what works best for your game.

我們可以觀察到動畫在矩形視圖的“ fillProperty()”上運行,我們用它來表示敵人。 特別是,填充會每0.5秒從紅色變為暗紅色。 隨意調整動畫設置,以查看最適合您的游戲的動畫。

Java游戲模擬器。Our factory class is now complete and we will start bringing our code together.

我們的工廠課程現已完成,我們將開始將我們的代碼整合在一起。

輸入項 (Input)

All of the FXGL input is typically handled inside the “initInput” method, as follows:

通常,所有FXGL輸入都在“ initInput”方法內部進行處理,如下所示:


@Override
protected void initInput() {onKey(KeyCode.W, () -> getGameWorld().getSingleton(EntityType.PLAYER).translateY(-5));onKey(KeyCode.S, () -> getGameWorld().getSingleton(EntityType.PLAYER).translateY(5));onBtnDown(MouseButton.PRIMARY, () -> {double y = getGameWorld().getSingleton(EntityType.PLAYER).getY();spawn("projectile", 0, y + 10);spawn("projectile", 0, y + 50);});
}

The first two calls set up our player movement. More specifically, W and S keys are going to move the player up and down respectively. Our last call sets up the player action, which is shooting. When the primary mouse button is pressed, we spawn our projectiles, which we defined earlier. The two last arguments to the “spawn” function are the x and y values of where to spawn the projectile.

java game, 前兩個呼叫設置了我們的玩家移動。 更具體地說,W和S鍵將分別向上和向下移動播放器。 我們的最后一個通話設置了玩家的動作,即射擊。 當按下主鼠標按鈕時,我們會生成我們先前定義的彈丸。 “ spawn”函數的最后兩個參數是生成彈丸的位置的x和y值。

游戲邏輯 (Game Logic)

Before we can start the game, we need to initialize some game logic, which we can do as follows:

在開始游戲之前,我們需要初始化一些游戲邏輯,我們可以執行以下操作:


@Override
protected void initGame() {getGameScene().setBackgroundColor(Color.BLACK);getGameWorld().addEntityFactory(new SpaceRangerFactory());spawn("player", 0, getAppHeight() / 2 - 30);run(() -> {double x = getAppWidth();double y = FXGLMath.random(0, getAppHeight() - 20);spawn("enemy", x, y);}, Duration.seconds(0.25));
}

We set the game scene background to color black (you may choose a different color to suit your needs).

我們將游戲場景背景設置為黑色(您可以根據需要選擇其他顏色)。

java游戲源碼?Next, we add our entity factory — FXGL needs to know how to spawn our entities.

接下來,我們添加實體工廠-FXGL需要知道如何生成我們的實體。

Thereafter, we spawn our player on the left side of the screen since the x value is 0.

此后,由于x值為0,我們在屏幕左側生成了播放器。

Lastly, we set up a timer action that runs every 0.25 seconds. The action is to spawn an enemy at a random Y location.

太空rts游戲? 最后,我們設置了一個計時器動作,該動作每0.25秒運行一次。 動作是在一個隨機的Y位置生成一個敵人。

物理 (Physics)

Our physics code is trivial since there are not many things that will be colliding in our game.

我們的物理代碼是微不足道的,因為在我們的游戲中不會發生很多沖突。


@Override
protected void initPhysics() {onCollisionBegin(EntityType.PROJECTILE, EntityType.ENEMY, (proj, enemy) -> {proj.removeFromWorld();enemy.removeFromWorld();});
}

As can be seen above, the only two entity types we care about with respect to collisions are projectile and enemy. We set up a handler that is called when these two types collide, giving us the references to specific entities that collided. Note the order in which the types are defined. This is the order in which entity references are passed in. We want to remove both from the world when they collide.

從上面可以看出,關于碰撞,我們關心的僅有的兩種實體類型是射彈和敵人。 我們設置了當這兩種類型發生沖突時調用的處理程序,為我們提供了對發生沖突的特定實體的引用。 注意定義類型的順序。 這是實體引用傳遞的順序。當它們碰撞時,我們希望將兩者從世界中刪除。

更新資料 (Update)

Java程序?There are not many things in our game loop. We only want to know when an enemy has reached our base, i.e. the enemy’s X value is less than 0.

我們的游戲循環中沒有很多東西。 我們只想知道敵人何時到達我們的基地,即敵人的X值小于0。


@Override
protected void onUpdate(double tpf) {var enemiesThatReachedBase = getGameWorld().getEntitiesFiltered(e -> e.isType(EntityType.ENEMY) && e.getX() < 0);if (!enemiesThatReachedBase.isEmpty()) {showMessage("Game Over!", () -> getGameController().startNewGame());}
}

To achieve this, we query the game world to give us all entities whose type is enemy and whose X value is less than 0. If the list is not empty then we lost the game, so we show the appropriate message and restart.

為此,我們查詢游戲世界以提供所有類型為敵人且X值小于0的實體。如果列表不為空,則我們輸了游戲,因此我們顯示適當的消息并重新啟動。

結論 (Conclusion)

That is the end of this tutorial. You should now be able to run the game in your IDE. The FXGL game engine is fully open-source and the source is available at https://github.com/AlmasB/FXGL.

Java怎么編譯? 到此結束。 您現在應該可以在IDE中運行游戲。 FXGL游戲引擎是完全開源的,該資源可從https://github.com/AlmasB/FXGL獲得 。

The full source code of this tutorial is available at https://github.com/AlmasB/FXGLGames/tree/master/SpaceRanger.

本教程的完整源代碼可在https://github.com/AlmasB/FXGLGames/tree/master/SpaceRanger中找到 。

翻譯自: https://www.journaldev.com/40219/space-rangers-game-java-fxgl

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/182876.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息