在基于文本的游戏中处理时间(Java)

een*_*lam 6 java system-clock text-based

我正在尝试编写一个基于文本的基本游戏,因为我正在学习Java.我希望能够在游戏中计算轮次,以此来管理某些事件的节奏.例如,更换房间可以限制为每轮一次(在测试代码中为一秒).小型生物可能以更高的速率攻击或更换房间,而较大的房间可能更麻烦.目前很好?大.

所以,我把它煮熟了,立刻意识到每次while循环等待玩家输入命令时我都会碰到一个块.码:

private void startPlaying() {
    //declare clock/round variables.
    int lastRound = 0;
    int currentRound = 0;
    long lastTime = System.currentTimeMillis();
    long currentTime;

    while (player.getIsPlaying()){
        //Clocking
        currentTime = System.currentTimeMillis();
        if ((lastTime + 1000) < currentTime) {
            lastTime = currentTime;
            lastRound = currentRound;
            currentRound++;
            System.out.println("Current round:\t" + currentRound + "\tCurrent time:\t" + currentTime); //EDIT:NOTE: This is just a test line to observe the loop.
        }//end if (Clocking)

        Command command = new Command();
        String[] commandString = command.setAll(); //Array gets parsed elsewhere.
        player.doCommand(commandString);  
        //Class Player extends Pawn extends Actor; Pawn has most command methods.
    }
    finishPlaying();
}//END STARTPLAYING()
Run Code Online (Sandbox Code Playgroud)

所以,这是我的问题:

有没有办法我可以使用一个单独的方法来记录时间/轮并发到while循环向玩家提供命令提示符?

如果没有,是否有不同的方法使这个问题成为我尚未看到/实践的问题呢?

Jea*_*tti 4

看一下并发教程。使用线程,您可以等待用户输入而无需停止其他进程(并使这些其他进程同时执行 - 不是并行,而是分时)。