Вот мой метод, который использует Thread.sleep():
Код: Выделить всё
public static void pause(int time) {
//makes java pause for a certain amount of time in ms
try {
Thread.sleep(time);
}
catch (InterruptedException e) {
}
}
Код: Выделить всё
public void drawFirstCards(){
//draw the initial two cards for the player and the dealer
hit(dealer);
pause(DRAW_DELAY);
hit(dealer);
pause(DRAW_DELAY);
hit(user);
pause(DRAW_DELAY);
hit(user);
}
Код: Выделить всё
public void hit(Player player){
//draw a card and add its value to the score. If the score is over 21, the player state is bust. If the score is 21, the player state is blackjack.
Card drawnCard = deck.drawCard();
player.addCard(drawnCard);
player.addScore(drawnCard.getValue());
user.revalidate();
user.repaint();
pause(DRAW_DELAY);
}
Кроме того, позже есть немного кода, где у меня есть цикл while, который также использует мой метод паузы:
Код: Выделить всё
public void dealersTurn() { //After the user stands or gets a blackjack, the dealer starts drawing cards
//draw cards until either the dealer busts or the dealer gets a score that is greater than or equal to the user
while (dealer.getScore() < BLACKJACK && dealer.getScore() < user.getScore()) {
hit(dealer);
pause(DRAW_DELAY);
}
Я думал, что программа вытянет карту, подождет, а затем вытянет другую карту, но вместо этого она вытягивает все карты, и вам приходится ждать общее время ожидания, прежде чем значки появятся в графическом интерфейсе.
Может ли кто-нибудь помочь мне лучше понять Threads.sleep(), чтобы я мог заставить его работать так, как я хочу? Или есть лучший способ делать небольшие паузы?
Подробнее здесь: https://stackoverflow.com/questions/797 ... teractions
Мобильная версия