2016-11-14 95 views
0

我正在製作遊戲/模擬器,我需要一個計時器來顯示遊戲開始以來的經過時間。在遊戲中添加一個經過時間的時鐘(Java)

我使用的是「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()) 

感謝您的幫助!

+2

爲什麼不打電話給SimulationWindow。計時器(currentTime的); ?我錯過了什麼嗎?當然,你需要將標記timer()方法作爲靜態 – developer

+0

我忘了靜態部分,非常感謝! 但是當函數現在看起來是這樣的: \t公共靜態長定時器(長currentTime的){ \t \t時間長= currentTime的; \t \t返回時間; 我似乎不能稱之爲: \t \t super.writeText(「時間的流逝:」 +定時器(長)) – djokerndthief

+0

您可以添加完整代碼都SimulationWindow和模擬類? – developer

回答

0

正如我在註釋中給出,可以使timer()方法static,然後將其稱爲SimulationWindow.timer(currentTime);

靜態計時方法:

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

我知道了,現在已經糾正了。謝謝。但是我似乎無法在SimulationWindow中使用它來顯示已用時間。我想這樣的: \t \t super.moveTo(395,780); \t \t超。println(「Time passed:」+ timer()) 對不起,我不知道如何格式化代碼的評論...:/ – djokerndthief

+0

您可以爲SimulationWindow和Simulation類添加完整的代碼嗎?更新你的問題 – developer

+0

它現在更新! :) – djokerndthief