游戏的java计时器

时间:2010-03-11 18:31:33

标签: java timer

我用java创建了一个游戏,现在我只需要添加一个允许用户在60秒以下玩的计时器。我在互联网上搜索并找到了swing和util包的计时器。你能不能给我一个能在我的游戏中使用它的方法???

3 个答案:

答案 0 :(得分:3)

如果您想要互动内容,可以使用TimerTaskTimer类:

class ExpireTask extends TimerTask
{
  YourClass callbackClass;

  ExpireTask(YourClass callbackClass)
  {
    this.callbackClass = callbackClass;
  }

  public void run()
  {
    callbackClass.timeExpired()
  }
}

所以现在你有一个计时器可以激活另一个类的timeExpired。现在使用Timer,您可以安排它:

...
Timer timer = new Timer();
timer.schedule(new ExpireTask(callbackClass), 60000 /* 60 secs */);
...

答案 1 :(得分:2)

System.currentMilliSeconds(); 保存在游戏开始时。 然后比较一下: 如果(厘米≤(System.currentMilliSeconds()/ 1000年至1060年)){System.exit(0);}

答案 2 :(得分:0)

问题是,当您安排任务时,该任务在不同的线程上运行,您怎么可能用它来更改在另一个线程上运行的玩家回合,而无需在整个执行过程中进行变量检查就知道何时回调函数被调用。

我还添加了一些示例代码。

public class CustomTimerTask extends TimerTask {

private ServerGameManager gameManager;

public CustomTimerTask(ServerGameManager gameManager){
    this.gameManager= gameManager;

}
@Override
public void run() {
    gameManager.changeTurn();
}}

这是我自定义的Timer类,将调用gameManager方法,但是该方法不会停止游戏循环以将回合转给另一位玩家。

private void playGame() {
        while (true) {

            try {
                int turnDelay = 1000;
                int turnSeconds = 30;
                java.util.Timer timer = new java.util.Timer();
                timer.scheduleAtFixedRate(new CustomTimerTask(this), 0, turnDelay);

                try {
                    // Send player turn color to clients
                    //ToDo change method to return to start whenever the update method is called
                    toPlayerOne.writeObject(turn);
                    toPlayerTwo.writeObject(turn);

                    // Get turn from client.
                    if (playerOne.getColor() == turn) {
                        move = (Move) fromPlayerOne.readObject();
                        move.setStart(9 - move.getStart().x, 9 - move.getStart().y);
                        move.setEnd(9 - move.getEnd().x, 9 - move.getEnd().y);
                    } else {
                        move = (Move) fromPlayerTwo.readObject();
                    }

                    Move moveToPlayerOne = new Move(), moveToPlayerTwo = new Move();

                    // Register move on the board.
                    // If there is no piece at the end (normal move, no attack)
                    if (board.getSquare(move.getEnd().x, move.getEnd().y).getPiece() == null) {
                        Piece piece = board.getSquare(move.getStart().x, move.getStart().y).getPiece();

                        board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);
                        board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(piece);

                        // Rotate the move 180 degrees before sending.
                        moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                        moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                        moveToPlayerOne.setMoveColor(move.getMoveColor());
                        moveToPlayerOne.setStartPiece(null);
                        moveToPlayerOne.setEndPiece(piece);

                        moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                        moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                        moveToPlayerTwo.setMoveColor(move.getMoveColor());
                        moveToPlayerTwo.setStartPiece(null);
                        moveToPlayerTwo.setEndPiece(piece);
                    } else {
                        Piece attackingPiece = board.getSquare(move.getStart().x, move.getStart().y).getPiece();
                        Piece defendingPiece = board.getSquare(move.getEnd().x, move.getEnd().y).getPiece();

                        BattleOutcome outcome = attackingPiece.getPieceType().attack(defendingPiece.getPieceType());

                        moveToPlayerOne.setAttackMove(true);
                        moveToPlayerTwo.setAttackMove(true);

                        if (outcome == BattleOutcome.WIN) {
                            board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(board.getSquare(move.getStart().x, move.getStart().y).getPiece());
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(attackingPiece);
                            moveToPlayerOne.setAttackWin(true);
                            moveToPlayerOne.setDefendWin(false);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(attackingPiece);
                            moveToPlayerTwo.setAttackWin(true);
                            moveToPlayerTwo.setDefendWin(false);
                        } else if (outcome == BattleOutcome.LOSE) {
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(defendingPiece);
                            moveToPlayerOne.setAttackWin(false);
                            moveToPlayerOne.setDefendWin(true);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(defendingPiece);
                            moveToPlayerTwo.setAttackWin(false);
                            moveToPlayerTwo.setDefendWin(true);
                        } else if (outcome == BattleOutcome.DRAW) {
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);
                            board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(null);
                            moveToPlayerOne.setAttackWin(false);
                            moveToPlayerOne.setDefendWin(false);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(null);
                            moveToPlayerTwo.setAttackWin(false);
                            moveToPlayerTwo.setDefendWin(false);
                        }
                    }

                    GameStatus winCondition = checkWinCondition();

                    toPlayerOne.writeObject(moveToPlayerOne);
                    toPlayerTwo.writeObject(moveToPlayerTwo);

                    toPlayerOne.writeObject(winCondition);
                    toPlayerTwo.writeObject(winCondition);


                    // Change turn color.
                    //ToDo use this change colors in a daemon that has a timer, this daemon should be located in the client and notify the server whenever the turn has changed
                    if (turn == PieceColor.RED)
                        turn = PieceColor.BLUE;
                    else
                        turn = PieceColor.RED;

                    // Check win conditions.
                } catch (IOException | ClassNotFoundException e) {
                    System.out.println(session + "Error occured during network I/O");
                    return;
                }
            } catch (Exception e) {
                System.out.println("CATCH");
                if (turn == PieceColor.RED)
                    turn = PieceColor.BLUE;
                else
                    turn = PieceColor.RED;

            }
        }
    }

这是始终执行的游戏循环(我知道如果游戏暂停,我应该添加一个条件来停止)

这里的问题是如何执行changeTurn()方法,以在执行游戏时停止游戏循环。

我想到的第一种方法是使用异常,但这不会起作用,因为它们是两个单独的线程,因此异常在CustomTimerTask处停止。