结合以上两个功能,实现动画方式的侩子手游戏,当用户猜错7次,绞刑架上的人摆动。当一个单词完成后,用户使用回车键继续猜下一个单词。javafx
时间: 2024-03-10 08:46:52 浏览: 105
以下是基于 JavaFX 实现动画方式的 Hangman 游戏的示例代码:
```java
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
public class HangmanGame extends Application {
private static final int MAX_INCORRECT_GUESSES = 7;
private static final String[] WORDS = {"HELLO", "WORLD", "JAVA", "FX"};
private static final Font WORD_FONT = new Font(30);
private static final Font GUESS_FONT = new Font(18);
private static final int WORD_SPACING = 20;
private static final int GUESS_SPACING = 10;
private static final int PANE_WIDTH = 500;
private static final int PANE_HEIGHT = 500;
private String word;
private char[] wordChars;
private char[] guessChars;
private int incorrectGuesses;
private Label wordLabel;
private Label guessLabel;
private TextField guessField;
private Button guessButton;
private Pane hangmanPane;
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setTop(createWordPane());
root.setCenter(createGuessPane());
root.setBottom(createInputPane());
Scene scene = new Scene(root, PANE_WIDTH, PANE_HEIGHT);
primaryStage.setScene(scene);
primaryStage.setTitle("Hangman Game");
primaryStage.show();
startNewGame();
}
private Pane createWordPane() {
wordLabel = new Label();
wordLabel.setFont(WORD_FONT);
StackPane pane = new StackPane();
pane.getChildren().add(wordLabel);
return pane;
}
private Pane createGuessPane() {
guessLabel = new Label();
guessLabel.setFont(GUESS_FONT);
StackPane pane = new StackPane();
pane.getChildren().add(guessLabel);
return pane;
}
private HBox createInputPane() {
guessField = new TextField();
guessField.setPrefWidth(100);
guessField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
handleGuess();
}
});
guessButton = new Button("Guess");
guessButton.setOnAction(event -> handleGuess());
HBox pane = new HBox(GUESS_SPACING, guessField, guessButton);
pane.setAlignment(Pos.CENTER);
return pane;
}
private Pane createHangmanPane() {
hangmanPane = new Pane();
Circle head = new Circle(50, Color.TRANSPARENT);
head.setStroke(Color.BLACK);
head.setStrokeWidth(5);
head.setCenterX(250);
head.setCenterY(100);
Line body = new Line(250, 150, 250, 300);
body.setStroke(Color.BLACK);
body.setStrokeWidth(5);
Line leftArm = new Line(250, 200, 200, 250);
leftArm.setStroke(Color.BLACK);
leftArm.setStrokeWidth(5);
Line rightArm = new Line(250, 200, 300, 250);
rightArm.setStroke(Color.BLACK);
rightArm.setStrokeWidth(5);
Line leftLeg = new Line(250, 300, 200, 350);
leftLeg.setStroke(Color.BLACK);
leftLeg.setStrokeWidth(5);
Line rightLeg = new Line(250, 300, 300, 350);
rightLeg.setStroke(Color.BLACK);
rightLeg.setStrokeWidth(5);
hangmanPane.getChildren().addAll(head, body, leftArm, rightArm, leftLeg, rightLeg);
hangmanPane.setTranslateX(PANE_WIDTH / 2 - 250);
hangmanPane.setTranslateY(PANE_HEIGHT / 2 - 200);
return hangmanPane;
}
private void startNewGame() {
word = WORDS[(int) (Math.random() * WORDS.length)];
wordChars = word.toCharArray();
guessChars = new char[wordChars.length];
incorrectGuesses = 0;
wordLabel.setText(String.join(" ", String.valueOf(guessChars)));
guessLabel.setText("");
if (hangmanPane != null) {
hangmanPane.getChildren().clear();
}
guessField.setDisable(false);
guessButton.setDisable(false);
guessField.requestFocus();
}
private void handleGuess() {
String guessText = guessField.getText().toUpperCase();
if (guessText.length() == 1 && Character.isLetter(guessText.charAt(0))) {
char guess = guessText.charAt(0);
boolean found = false;
for (int i = 0; i < wordChars.length; i++) {
if (wordChars[i] == guess) {
guessChars[i] = guess;
found = true;
}
}
if (!found) {
incorrectGuesses++;
if (incorrectGuesses >= MAX_INCORRECT_GUESSES) {
guessField.setDisable(true);
guessButton.setDisable(true);
guessLabel.setText("You lose! The word was: " + word);
createHangmanPane();
RotateTransition rotate = new RotateTransition(Duration.seconds(1), hangmanPane);
rotate.setByAngle(30);
rotate.setCycleCount(6);
rotate.setAutoReverse(true);
rotate.setOnFinished(event -> startNewGame());
rotate.play();
}
} else if (String.valueOf(guessChars).equals(word)) {
guessField.setDisable(true);
guessButton.setDisable(true);
guessLabel.setText("You win! The word was: " + word);
startNewGame();
}
wordLabel.setText(String.join(" ", String.valueOf(guessChars)));
guessLabel.setText("Incorrect guesses: " + incorrectGuesses);
guessField.setText("");
guessField.requestFocus();
}
}
public static void main(String[] args) {
launch(args);
}
}
```
在该游戏中,创建了一个包含单词区、猜测区和输入区的 BorderPane。单词区和猜测区使用 Label 来显示单词和猜测结果,输入区使用 TextField 和 Button 来接收用户猜测。当用户猜错时,将在 hangmanPane 上绘制一个绞刑架,同时将 hangmanPane 在窗口中旋转。当用户猜中或猜错次数达到最大值时,将调用 startNewGame() 方法开始新游戏。
该游戏中使用了 JavaFX 动画库中的 RotateTransition 类来实现旋转绞刑架的效果。通过设置旋转角度、持续时间、重复次数等参数,可以非常方便地实现旋转动画效果。
阅读全文