【问题标题】:Adding a clock with elapsed time to a game (Java)在游戏中添加带有经过时间的时钟(Java)
【发布时间】:2017-03-28 15:38:59
【问题描述】:

我正在开发一个游戏/模拟器,我需要一个计时器来显示游戏开始后经过的时间。

我使用“SimpleWindow”类作为窗口,称为 SimulationWindow,用于显示模拟的视觉效果。主类(命名模拟)执行大部分计算。

我希望在 SimulationWindow 中显示时钟,而不是在主类中。我的意思是我不想使用 System.out.println 在主类 Simulation 中显示结果(我知道该怎么做),但是结果需要在 SimulationWindow 中每秒自动更新。

我正在尝试在 SimulationWindow 类中创建一个函数,但无法使其工作。需要在 SimulationeWindow 一打开(游戏开始时)就开始计数。

来自主类,模拟类的代码:

import java.util.ArrayList;
import java.util.Random;

public class Simulate {
public static void main(String[] args)
        throws InterruptedException {

    SimulationWindow w = new SimulationWindow();
    Random rand = new Random();

    ArrayList<Player> player = new ArrayList<Player>();
    ArrayList<Player> hPlaying= new ArrayList<Player>();

    // long startTime = System.nanoTime();
    int gameOn = 1;

    // Adds all players to the game before it starts.

    for (int i = 1; i < 11; i++) {

        int randNbr = rand.nextInt(3);

        if (randNbr == 0) {
            player.add(new BlueM(w, i, randNbr));
        } else if (randNbr == 1) {
            player.add(new RedM(w, i, randNbr));
        } else if (randNbr == 2) {
            player.add(new BlueS(w, i));
        } else if (randNbr == 3) {
            player.add(new RedS(w, i));
        } else if (randNbr == 4) {
            player.add(new BlueJ(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 5) {
            player.add(new RedJ(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 6) {
            player.add(new BlueA(w, i,
                    rand.nextInt(50)));
        } else if (randNbr == 7) {
            player.add(new RedA(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 8) {
            player.add(new BlueT(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 9) {
            player.add(new RedT(w, i,
                    rand.nextInt(50)));
        }

    }

    // Makes all players alive.
    for (Player h : player) {
        hPlaying.add(h);
    }

    // Starts the game.
    while (gameOn > 0) {

        long startTime = System.nanoTime();

        for (Player h : player) {

            // If the player is alive, continue
            if (hPlaying.contains(h)) {
                h.Step();
            }
        }
        SimulationWindow.delay(10);

        long currentTime = System.nanoTime() - startTime;
        SimulationWindow.timer(currentTime);
    }

    // long currentTime = System.nanoTime() - startTime;

}

}

但我不知道如何将主类中的 currentTime 发送到应返回时间的 SimulationWindow 中的函数,以便它可以显示在此 SimulationWindow 中并每秒更新一次(或每次玩家使用步骤,没关系)。

这是我在 SimulationWindow 中的;

import se.lth.cs.pt.window.SimpleWindow;

public class SimulationWindow extends SimpleWindow {
public static final int X_START_POS_BLUE = 790;
public static final int X_END_POS_BLUE = 10;
public static final int Y_LINE_START_BLUE = 10;
public static final int Y_LINE_END_BLUE = 790;
public static final int X_START_POS_RED = 10;
public static final int X_END_POS_RED = 790;
public static final int Y_LINE_START_RED = 790;
public static final int Y_LINE_END_RED = 10;

public SimulationWindow() {
    super(800, 690, "game x");
    setUp();
}

private void setUp() {
    super.moveTo(X_START_POS_BLUE - 2,
            Y_LINE_START_BLUE + 2);
    super.writeText("1");
    super.moveTo(X_START_POS_RED - 2,
            Y_LINE_START_RED + 2);
    super.writeText("2");
    super.moveTo(X_START_POS_BLUE - 2,
            Y_LINE_START_BLUE + 0);
    super.writeText("3");
    super.moveTo(X_START_POS_RED - 2,
            Y_LINE_START_RED + 0);
    super.writeText("4");
    super.moveTo(X_START_POS_BLUE - 0,
            Y_LINE_START_BLUE + 2);
    super.writeText("5");
    super.moveTo(X_START_POS_RED - 0,
            Y_LINE_START_RED + 2);
    super.writeText("6");
    super.moveTo(X_START_POS_BLUE - 0,
            Y_LINE_START_BLUE + 4);
    super.writeText("7");
    super.moveTo(X_START_POS_RED - 0,
            Y_LINE_START_RED + 4);
    super.writeText("8");
    super.moveTo(X_START_POS_BLUE - 4,
            Y_LINE_START_BLUE + 0);
    super.writeText("9");
    super.moveTo(X_START_POS_RED - 4,
            Y_LINE_START_RED + 0);
    super.writeText("10");

    super.moveTo(395, 780);
    super.println("Time passed: "+ timer())
}

public static int getStartXPos(int startNbr) {
    if (startNbr == 1 || startNbr == 3 || startNbr == 5
            || startNbr == 7 || startNbr == 9) {
        return X_START_POS_BLUE;
    } else {
        return X_START_POS_RED;
    }
}

public static int getStartYPos(int startNbr) {
    if (startNbr == 1 || startNbr == 3 || startNbr == 5
            || startNbr == 7 || startNbr == 9) {
        return Y_LINE_START_BLUE + startNbr * 20;
    } else {
        return Y_LINE_START_RED + startNbr * 20;
    }

}

public static long timer(long time) {
    return time;

}

}

在 SimulationWindow 类中,这是我无法工作的代码:

super.writeText("Time passed: "+ timer())

感谢您的帮助!

【问题讨论】:

  • 为什么不能调用 SimulationWindow.timer(currentTime); ?我错过了什么吗?当然,你需要将 timer() 方法标记为静态
  • 我忘记了静态部分,非常感谢!但是当函数现在看起来像这样时: public static long timer(long currentTime) { long time = currentTime;返回时间;我似乎无法调用它: super.writeText("Time passed: "+ timer(long))
  • 你能添加 SimulationWindow 和 Simulation 类的完整代码吗?

标签: java timer simulator elapsed


【解决方案1】:

正如我的 cmets 中给出的,您可以将timer() 方法设为static,然后将其称为SimulationWindow.timer(currentTime);

静态定时器方法:

public static long timer(long time); {
    return time;
}

【讨论】:

  • 到目前为止我得到了它,现在已经更正了。谢谢你。但是我似乎无法让它在 SimulationWindow 中工作以显示经过的时间。我正在尝试这个: super.moveTo(395, 780); super.println("时间过去:"+ timer()) 很抱歉我不知道如何格式化注释中的代码...:/
  • 你能添加 SimulationWindow 和 Simulation 类的完整代码吗?更新你的问题
  • super.println("Time passed: "+ timer()) 错误,它说:“SimulationWindow 类型中的方法 timer(long) 不适用于参数。”
猜你喜欢
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多