用javafx实现农夫过河这个游戏(通过按钮的点击来使得物品过河并且实现过河这个动画)
时间: 2024-03-06 21:51:33 浏览: 60
Java语言解决农夫过河问题
4星 · 用户满意度95%
好的,这次我将为您提供一个实现农夫过河游戏的完整代码,包括按钮点击事件和实现过河动画。
```java
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private boolean farmerOnLeft = true;
private boolean sheepOnLeft = true;
private boolean wolfOnLeft = true;
private boolean cabbageOnLeft = true;
private StackPane farmerPane;
private StackPane sheepPane;
private StackPane wolfPane;
private StackPane cabbagePane;
private TranslateTransition farmerTransition;
private TranslateTransition sheepTransition;
private TranslateTransition wolfTransition;
private TranslateTransition cabbageTransition;
@Override
public void start(Stage primaryStage) throws Exception{
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(20);
gridPane.setVgap(20);
// 创建物品的 Pane 和 Rectangle
farmerPane = new StackPane();
Rectangle farmerRect = new Rectangle(50, 50);
farmerPane.getChildren().add(farmerRect);
sheepPane = new StackPane();
Rectangle sheepRect = new Rectangle(50, 50);
sheepPane.getChildren().add(sheepRect);
wolfPane = new StackPane();
Rectangle wolfRect = new Rectangle(50, 50);
wolfPane.getChildren().add(wolfRect);
cabbagePane = new StackPane();
Rectangle cabbageRect = new Rectangle(50, 50);
cabbagePane.getChildren().add(cabbageRect);
// 设置物品的初始位置
farmerPane.setTranslateX(50);
farmerPane.setTranslateY(100);
sheepPane.setTranslateX(150);
sheepPane.setTranslateY(100);
wolfPane.setTranslateX(250);
wolfPane.setTranslateY(100);
cabbagePane.setTranslateX(350);
cabbagePane.setTranslateY(100);
// 创建过河动画
farmerTransition = new TranslateTransition(Duration.seconds(1), farmerPane);
sheepTransition = new TranslateTransition(Duration.seconds(1), sheepPane);
wolfTransition = new TranslateTransition(Duration.seconds(1), wolfPane);
cabbageTransition = new TranslateTransition(Duration.seconds(1), cabbagePane);
// 设置过河动画的目标位置
farmerTransition.setToX(300);
sheepTransition.setToX(300);
wolfTransition.setToX(300);
cabbageTransition.setToX(300);
// 将物品添加到界面上
gridPane.add(farmerPane, 0, 0);
gridPane.add(sheepPane, 1, 0);
gridPane.add(wolfPane, 2, 0);
gridPane.add(cabbagePane, 3, 0);
// 创建按钮并添加点击事件
Button farmer = new Button("农夫");
farmer.setOnAction(e -> {
if (sheepOnLeft == farmerOnLeft && wolfOnLeft == farmerOnLeft && cabbageOnLeft == farmerOnLeft) {
farmerOnLeft = !farmerOnLeft;
farmerTransition.play();
}
});
Button sheep = new Button("羊");
sheep.setOnAction(e -> {
if (sheepOnLeft == farmerOnLeft) {
sheepOnLeft = !sheepOnLeft;
sheepTransition.play();
} else {
System.out.println("游戏结束");
}
});
Button wolf = new Button("狼");
wolf.setOnAction(e -> {
if (wolfOnLeft == farmerOnLeft) {
wolfOnLeft = !wolfOnLeft;
wolfTransition.play();
} else {
System.out.println("游戏结束");
}
});
Button cabbage = new Button("白菜");
cabbage.setOnAction(e -> {
if (cabbageOnLeft == farmerOnLeft) {
cabbageOnLeft = !cabbageOnLeft;
cabbageTransition.play();
} else {
System.out.println("游戏结束");
}
});
gridPane.add(farmer, 0, 1);
gridPane.add(sheep, 1, 1);
gridPane.add(wolf, 2, 1);
gridPane.add(cabbage, 3, 1);
Scene scene = new Scene(gridPane, 500, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
上面的代码中,我们使用了 TranslateTransition 类来实现物品过河的动画。在按钮点击事件中,我们根据当前状态更新布尔值,并播放对应的过河动画。在动画结束后,物品将自动移动到目标位置。
这份代码可能还有一些可以改进的地方,比如添加游戏胜利的判断等等。但是,这份代码可以帮助您了解如何使用 JavaFX 实现农夫过河游戏以及如何实现过河动画。
阅读全文