0

我正在尝试使用以下代码中的重绘方法在用户输入后更新屏幕。该游戏是一款纸牌游戏,用户必须点击两张牌才能显示他们的图片。如果图片匹配,卡片仍然可见,但如果图片不匹配,卡片翻转以再次隐藏图片。

The first card becomes visible after clicking it, however when the second card is selected either both cards become visible if a matching picture is selected or the first card just flips over without the second picture being revealed.

谢谢你的帮助。

addMouseListener(new MouseAdapter(){

        public void mouseClicked(MouseEvent e) {
                int row = e.getX() / (Card.SIZE*2);
                int col = e.getY() / (Card.SIZE*3);


                                    //OPEN means the picture is visible
                if(cards[row][col].getState() == Card.CLOSED)
                    cards[row][col].setState(OPEN);

                repaint();
                compareCards(row,col);

        }
    }); 
}


public void compareCards(int row, int col){

    if(clickNum == 1){
        r1 = row;
        c1 = col;
        clickNum++;

    }
    else if(clickNum == 2){
        r2 = row;
        c2 = col;

        //The OR accounts for clicking twice on the same tile
        if(cards[r1][c1].getNum() != cards[r2][c2].getNum() || (r1 == r2 && c1 == c2)){
            cards[r1][c1].setState(CLOSED);
            cards[r2][c2].setState(CLOSED);
        }
        clickNum = 1;
    }
}
4

1 回答 1

0

您的比较卡片功能将卡片状态设置为 CLOSED 太快了,因此它们没有显示。尝试使用:

public void compareCards(int row, int col){

try
{
  Thread.sleep(5000);//sleep for five seconds
}catch(Exception e){}

if(clickNum == 1){
    r1 = row;
    c1 = col;
    clickNum++;

}
else if(clickNum == 2){
    r2 = row;
    c2 = col;

    //The OR accounts for clicking twice on the same tile
    if(cards[r1][c1].getNum() != cards[r2][c2].getNum() || (r1 == r2 && c1 == c2)){
        cards[r1][c1].setState(CLOSED);
        cards[r2][c2].setState(CLOSED);
    }
    clickNum = 1;
}

}

这应该在翻转它们之前显示两张卡片大约五秒钟。如果您还没有卡片,您还必须实现一种方法来处理将卡片翻转回来的情况。我之所以这么说,是因为我在这里没有看到。

于 2013-05-16T23:38:40.527 回答