0

ゲーム/シミュレーターに取り組んでおり、ゲーム開始からの経過時間を示すタイマーが必要です。

シミュレーションのビジュアルを表示するために、SimulationWindow と呼ばれるウィンドウとして "SimpleWindow" クラスを使用しています。メイン クラス (名前付き Simulate) がほとんどの計算を行います。

メイン クラスではなく、SimulationWindow に表示されるクロックが必要です。私が言いたいのは、System.out.println を使用してメイン クラス Simulation に結果を表示したくないということです (その方法は知っています) が、SimulationWindow 内で毎秒自動的に結果を更新する必要があります。

SimulationWindow クラスで関数を作成しようとしていますが、機能しません。SimulationeWindow が開いたらすぐにカウントを開始する必要があります (ゲームの開始時)。

メイン クラスである Simulation クラスのコード:

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

助けてくれてありがとう!

4

1 に答える 1