开发者社区> 问答> 正文

如何添加计时器到连接四?爪哇

有点非常规的问题,但我想知道是否有人对将计时器实现为四人连线游戏(2个玩家)的方式有任何想法。我目前有常规游戏的正常运行代码,但想添加一种方法,使用户有5秒钟的时间进行移动,否则计算机将为他们随机移动。是否有人对如何实现这一目标有任何想法?这是我处理转弯决策的主要功能。

public void handleAction (int x, int y)
    {
    if (gameOver)
    {
        JOptionPane.showMessageDialog (this, "Please Select Game...New to start a new game",
            "Game Over", JOptionPane.WARNING_MESSAGE);
        return;
    }

    int column = (x - BORDER_SIZE) / SQUARE_SIZE + 1;
    int row = findNextRow (board, column);

    if (row <= 0)
    {
        JOptionPane.showMessageDialog (this, "Please Select another Column",
            "Column is Full", JOptionPane.WARNING_MESSAGE);
        return;
    }

    animatePiece (currentPlayer, column, row);
    board [row] [column] = currentPlayer;

    int winner = checkForWinner (board, row, column);


    if (winner == BANANA)
    {
        gameOver = true;
        repaint ();
        backgroundMusic.stop();
        winBanana.play();
        JOptionPane.showMessageDialog (this, "Banana Wins!!!",
            "GAME OVER", JOptionPane.INFORMATION_MESSAGE);

    }
    else if (winner == STRAWBERRY)
    {
        gameOver = true;
        repaint ();
        backgroundMusic.stop();
        winStrawberry.play();
        JOptionPane.showMessageDialog (this, "Strawberry Wins!!!",
            "GAME OVER", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
        currentPlayer *= -1;
        if (currentPlayer == 1)
            addStrawberry.play();
        else
            addBanana.play();

    }   
    currentColumn = 3;

    repaint ();
}

展开
收起
垚tutu 2019-12-12 09:29:52 487 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载