[翻译] 使用FXGL创建一个简单游戏 Pong (FXGL 11)

在本文中,我们将复刻经典的Pong游戏。要完成本教程,你首先需要获取FXGL要么通过Maven / Gradle,要么作为uber-jar。确保你使用FXGL 11 (例如11.3)。

本教程大部分是独立的,但是完成以前的基本教程将对一般理解非常有帮助。完整的源代码可在本页末尾找到。请注意,为简单起见,这里使用的代码是故意单一的和重复的。

与Pong教程不同,这里将向你介绍常用的FXGL概念。因此,重点是在这些概念上,而不是在游戏上。

游戏将如下所示:

引入包

创建文件PongApp.java让我们import以下这些内容,然后在本教程的其余部分中忘记它们。


注意: 最后一行import (static) 允许我们写入getInput()而不是FXGL.getInput(),这使得代码简洁。

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.UserAction;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import java.util.Map;

import static com.almasb.fxgl.dsl.FXGL.*;
复制代码

代码

本节将介绍每个方法,并解释代码的主要部分。

默认情况下,FXGL将游戏尺寸设置为800x600,这对我们的游戏是合适的。

你可以通过settings.setXXX()改变这些和其他各种设置。现在,我们只需设置标题并添加入口点--main()。

public class PongApp extends GameApplication {

    @Override
    protected void initSettings(GameSettings settings) {
        settings.setTitle("Pong");
    }

    public static void main(String[] args) {
        launch(args);
    }
}
复制代码

接下来,我们将定义一些常数,这些常数是不言自明的。

private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 100;
private static final int BALL_SIZE = 20;
private static final int PADDLE_SPEED = 5;
private static final int BALL_SPEED = 5;
复制代码

我们有三个游戏对象,分别是两个球拍和一个球。FXGL中的游戏对象称为Entity。因此,让我们定义我们的Entity:

private Entity paddle1;
private Entity paddle2;
private Entity ball;
复制代码

接下来,我们将initInput。与某些框架不同,无需手动查询输入状态。在FXGL中,我们通过定义动作 (游戏应该做什么) 并将它们绑定到输入触发器 onAction(当按下某物时) 来处理输入。例如:

@Override
protected void initInput() {
    getInput().addAction(new UserAction("Up 1") {
        @Override
        protected void onAction() {
            paddle1.translateY(-PADDLE_SPEED);
        }
    }, KeyCode.W);

   
FXGL 中,可以通过 `FXGL.getSceneService().pushScene(targetScene)` 方法来跳转到目标场景。为了方便起见,我们可以创建一个自定义的组件来实现场景跳转功能。 首先,创建一个名为 `SceneSwitchComponent` 的类,继承 `com.almasb.fxgl.entity.component.Component` 类,代码如下: ```java import com.almasb.fxgl.core.serialization.Bundle; import com.almasb.fxgl.entity.component.Component; public class SceneSwitchComponent extends Component { private String targetScene; public SceneSwitchComponent(String targetScene) { this.targetScene = targetScene; } @Override public void onUpdate(double tpf) { // 空实现,不需要实现 onUpdate 方法 } @Override public void onAdded() { // 点击实体时跳转到目标场景 entity.getViewComponent().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { FXGL.getSceneService().pushScene(targetScene); }); } @Override public void onRemoved() { // 移除事件监听器 entity.getViewComponent().removeEventHandler(MouseEvent.MOUSE_CLICKED, event -> { FXGL.getSceneService().pushScene(targetScene); }); } @Override public void write(Bundle bundle) { bundle.put("targetScene", targetScene); } @Override public void read(Bundle bundle) { targetScene = bundle.get("targetScene"); } } ``` 这个组件包含一个 `targetScene` 字段,表示要跳转的目标场景名称。在 `onAdded()` 方法中,我们为实体的 `ViewComponent` 添加了一个鼠标点击事件监听器,当用户点击实体时,就会自动跳转到目标场景。`onRemoved()` 方法用于在移除组件时移除事件监听器。 接下来,在需要使用场景跳转功能的实体上添加 `SceneSwitchComponent` 组件即可。例如,我们可以创建一个名为 `Button` 的实体,代码如下: ```java import com.almasb.fxgl.entity.Entities; import com.almasb.fxgl.entity.Entity; import javafx.geometry.Pos; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; public class Button extends Entity { public Button(String text, String targetScene) { Text textNode = new Text(text); textNode.setFont(Font.font(24)); textNode.setFill(Color.WHITE); setViewFromNode(textNode); setAlignment(Pos.CENTER); addComponent(new SceneSwitchComponent(targetScene)); } } ``` 这个实体包含一个用于显示文本的 `Text` 节点,并且添加了一个 `SceneSwitchComponent` 组件。创建实体时需要传入两个参数,分别是文本内容和目标场景名称。 最后,在游戏场景中添加这个 `Button` 实体,代码如下: ```java import com.almasb.fxgl.app.scene.GameScene; import com.almasb.fxgl.entity.Entity; import com.almasb.fxgl.entity.Entities; public class MyGameScene extends GameScene { public MyGameScene() { Entity button = Entities.builder() .at(400, 300) .viewFromNode(new Button("跳转到场景2", "scene2")) .build(); addEntities(button); } } ``` 这里创建一个名为 `MyGameScene` 的游戏场景,并且添加了一个 `Button` 实体。当用户点击这个实体时,就会自动跳转到名为 `scene2` 的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值