我用JavaFx编写了一个战争纸牌游戏应用程序,该应用程序显示了游戏的进程(显示玩家玩的纸牌,每个副牌中的纸牌数量)。 我添加了一个Thread.sleep(2000)在两个回合之间延迟2秒。 但是有一个问题:延迟有效(我已经在控制台中调试了游戏的发展),但是图像和标签却没有刷新。 显示指令仅在我的编稿末尾执行。
我尝试使用另一种方法来创建在论坛上看到的延迟,例如runLater()。
也许问题出在显示卡片和标签的函数中,但我看不到错误,并且仅在程序结束时起作用。
战争阶级:
package sample;
/**
* War card game entity
*/
public class War {
/**
* Player1
*/
private Player player1;
/**
* Player2
*/
private Player player2;
/**
* Carpet
*/
private Carpet carpet;
/**
* Card box
*/
private CardBox cardBox;
private Controller controller;
/**
* War card game constructor
* @param player1 a player
* @param player2 a player
*/
public War(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
carpet = new Carpet();
cardBox = new CardBox();
controller = Controller.getInstance();
}
/**
* Start the game
*/
public void start() {
/////
System.out.println("------ WAR CARD GAME ------\n");
/////
// Check there are all the cards in the card box
this.cardBox.init();
// Deal cards
this.cardBox.shuffle();
dealCards(player1, 3);
dealCards(player2, 3);
// Start game
int numberTurn = 1;
do {
// Shuffle decks every 5 turns to avoid infinity loop
if (numberTurn % 5 == 0) {
this.player1.shuffleDeck();
this.player2.shuffleDeck();
controllerDisplayShuffledDeck(true);
controllerDisplayShuffledDeck(true);
System.out.println("Decks shuffled\n");
}
// Players decks
this.player1.showHand();
this.player2.showHand();
controllerDisplayNumberOfPlayersCard();
// Players reveal their card
controllerDisplayCardsPlayer();
this.carpet.revealCard(player1.play());
this.carpet.revealCard(player2.play());
controller.player1DisplayDeck(this.player1.numberOfCards() != 0);
controller.player2DisplayDeck(this.player2.numberOfCards() != 0);
// See game
this.carpet.display();
// Result
switch (this.carpet.compareCards()) {
case DRAW:
System.out.println("Draw\n");
break;
case PLAYER1_WIN:
this.player1.winTurn(carpet.takeAllCards());
break;
case PLAYER2_WIN:
this.player2.winTurn(carpet.takeAllCards());
break;
}
numberTurn ++;
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {}
} while (this.player1.numberOfCards() != 0 && this.player2.numberOfCards() != 0);
controllerDisplayNumberOfPlayersCard();
if (this.player1.numberOfCards()==0) {
controller.setStateGamePlayer1("Sniff :'(");
controller.setStateGamePlayer2("Ouiii :)");
}
else {
controller.setStateGamePlayer2("Sniff :'-(");
controller.setStateGamePlayer1("Ouiii :)");
}
}
/**
* Deal cards to a player
* @param player a player
* @param nbCards a number of cards
*/
private void dealCards(Player player, int nbCards) {
for (int i = 0; i < nbCards; i++) {
player.pickRandomCard(this.cardBox);
}
}
private void controllerDisplayCardsPlayer() {
controller.setPlayedCardPlayer1(this.player1.getNextCard().getCardValue(), this.player1.getNextCard().getCardSuit());
controller.setPlayedCardPlayer2(this.player2.getNextCard().getCardValue(), this.player2.getNextCard().getCardSuit());
}
private void controllerDisplayNumberOfPlayersCard() {
controller.setNbCardPlayer1(this.player1.numberOfCards());
controller.setNbCardPlayer2(this.player2.numberOfCards());
}
private void controllerDisplayShuffledDeck(boolean b) {
if (b) {
controller.setStateGamePlayer1("Deck\nshuffled");
controller.setStateGamePlayer2("Deck\nshuffled");
}
else {
controller.setStateGamePlayer1("");
controller.setStateGamePlayer2("");
}
}
}
控制器:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private ImageView deckPlayer1;
@FXML
private ImageView deckPlayer2;
@FXML
private ImageView playedCardPlayer1;
@FXML
private ImageView playedCardPlayer2;
@FXML
private AnchorPane window;
@FXML
private Label namePlayer1;
@FXML
private Label namePlayer2;
@FXML
private Label nbCardPlayer1;
@FXML
private Label nbCardPlayer2;
@FXML
private Label stateGamePlayer1;
@FXML
private Label stateGamePlayer2;
@FXML
private Button button;
private Player player1 = new Player("Thomas");
private Player player2 = new Player("Théo");
private static Controller instance;
public Controller() {
instance = this;
}
public static Controller getInstance() {
return instance;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
initDeckImage();
initPlayerLabel();
button.setText("Jouez !");
}
@FXML
public void newGame() {
button.setVisible(false);
War war = new War(player1, player2);
war.start();
}
private void initDeckImage() {
try {
FileInputStream input = new FileInputStream("imgCard/red_back.png");
Image image = new Image(input);
deckPlayer1.setImage(image);
deckPlayer2.setImage(image);
}
catch (IOException e) {
e.printStackTrace();
}
}
private void initPlayerLabel() {
namePlayer1.setText(player1.getName());
namePlayer2.setText(player2.getName());
nbCardPlayer1.setText("26");
nbCardPlayer2.setText("26");
stateGamePlayer1.setText("");
stateGamePlayer2.setText("");
}
public void setStateGamePlayer1(String stateGamePlayer1) {
this.stateGamePlayer1.setText(stateGamePlayer1);
}
public void setStateGamePlayer2(String stateGamePlayer2) {
this.stateGamePlayer2.setText(stateGamePlayer2);
}
public void setNbCardPlayer1(int nbCardPlayer1) {
this.nbCardPlayer1.setText(String.valueOf(nbCardPlayer1));
}
public void setNbCardPlayer2(int nbCardPlayer2) {
this.nbCardPlayer2.setText(String.valueOf(nbCardPlayer2));
}
public void setPlayedCardPlayer1(int rank, String suit) {
try {
FileInputStream input = new FileInputStream("imgCard/"+ rank + suit +".png");
Image image = new Image(input);
this.playedCardPlayer1.setImage(image);
}
catch (IOException e) {
e.printStackTrace();
}
}
public void setPlayedCardPlayer2(int rank, String suit) {
try {
FileInputStream input = new FileInputStream("imgCard/"+ rank + suit +".png");
Image image = new Image(input);
this.playedCardPlayer2.setImage(image);
}
catch (IOException e) {
e.printStackTrace();
}
}
public void player1DisplayDeck(boolean b) {
if (!b) {
this.deckPlayer1.setImage(null);
}
else {
try {
FileInputStream input = new FileInputStream("imgCard/red_back.png");
Image image = new Image(input);
this.deckPlayer1.setImage(image);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void player2DisplayDeck(boolean b) {
if (!b) {
this.deckPlayer2.setImage(null);
}
else {
try {
FileInputStream input = new FileInputStream("imgCard/red_back.png");
Image image = new Image(input);
this.deckPlayer2.setImage(image);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
主要:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Bataille");
primaryStage.setScene(new Scene(root, 300, 300));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
例如,我还有另一个用于Player和Deck的文件,但我认为问题出在上述文件中。
我希望卡片和标签能出现/刷新,并且之间要间隔几秒钟才能欣赏游戏。