用JAVAFX编写一个农夫过河游戏代码
时间: 2024-03-11 08:36:39 浏览: 152
Javafx实现国际象棋游戏
以下是一个简单的农夫过河游戏的 JavaFX 代码示例:
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FarmerCrossingRiver extends Application {
// 定义四个人物和船的位置,0 表示左岸,1 表示右岸
private int farmerPos = 0;
private int wolfPos = 0;
private int goatPos = 0;
private int cabbagePos = 0;
private int boatPos = 0;
// 定义游戏界面元素
private Label statusLabel;
private Button farmerButton;
private Button wolfButton;
private Button goatButton;
private Button cabbageButton;
private Button boatButton;
private Button resetButton;
@Override
public void start(Stage primaryStage) throws Exception {
// 初始化游戏界面元素
statusLabel = new Label("农夫要把狼、羊、菜和自己运到对岸。但船太小,只能装下农夫和一样东西。");
farmerButton = new Button("农夫");
wolfButton = new Button("狼");
goatButton = new Button("羊");
cabbageButton = new Button("菜");
boatButton = new Button("船");
resetButton = new Button("重新开始");
// 设置按钮点击事件
farmerButton.setOnAction(event -> move(farmerButton));
wolfButton.setOnAction(event -> move(wolfButton));
goatButton.setOnAction(event -> move(goatButton));
cabbageButton.setOnAction(event -> move(cabbageButton));
boatButton.setOnAction(event -> move(boatButton));
resetButton.setOnAction(event -> reset());
// 初始化游戏界面布局
HBox leftBox = new HBox(farmerButton, wolfButton, goatButton, cabbageButton);
HBox rightBox = new HBox();
VBox vbox = new VBox(statusLabel, leftBox, rightBox, boatButton, resetButton);
vbox.setSpacing(10);
vbox.setAlignment(Pos.CENTER);
// 设置游戏界面
Scene scene = new Scene(vbox, 400, 300);
primaryStage.setTitle("农夫过河");
primaryStage.setScene(scene);
primaryStage.show();
}
// 移动人物或船
private void move(Button button) {
String text = button.getText();
if (text.equals("农夫")) {
if (farmerPos == boatPos) {
farmerPos = 1 - farmerPos;
boatPos = 1 - boatPos;
setButtonPosition(farmerButton, farmerPos);
setButtonPosition(boatButton, boatPos);
} else {
setStatus("农夫必须在船上");
}
} else if (text.equals("狼")) {
if (wolfPos == farmerPos && wolfPos == boatPos) {
wolfPos = 1 - wolfPos;
boatPos = 1 - boatPos;
setButtonPosition(wolfButton, wolfPos);
setButtonPosition(boatButton, boatPos);
if (!checkStatus()) {
setStatus("狼吃了羊!");
}
} else {
setStatus("狼必须和农夫在同一岸边");
}
} else if (text.equals("羊")) {
if (goatPos == farmerPos && goatPos == boatPos) {
goatPos = 1 - goatPos;
boatPos = 1 - boatPos;
setButtonPosition(goatButton, goatPos);
setButtonPosition(boatButton, boatPos);
if (!checkStatus()) {
setStatus("羊吃了菜!");
}
} else {
setStatus("羊必须和农夫在同一岸边");
}
} else if (text.equals("菜")) {
if (cabbagePos == farmerPos && cabbagePos == boatPos) {
cabbagePos = 1 - cabbagePos;
boatPos = 1 - boatPos;
setButtonPosition(cabbageButton, cabbagePos);
setButtonPosition(boatButton, boatPos);
if (!checkStatus()) {
setStatus("菜被羊吃了!");
}
} else {
setStatus("菜必须和农夫在同一岸边");
}
} else if (text.equals("船")) {
if (boatPos == farmerPos) {
boatPos = 1 - boatPos;
setButtonPosition(boatButton, boatPos);
} else {
setStatus("农夫必须在船上");
}
}
}
// 设置人物或船的位置
private void setButtonPosition(Button button, int pos) {
HBox leftBox = (HBox) ((VBox) button.getParent()).getChildren().get(1);
HBox rightBox = (HBox) ((VBox) button.getParent()).getChildren().get(2);
if (pos == 0) {
leftBox.getChildren().add(button);
rightBox.getChildren().remove(button);
} else {
rightBox.getChildren().add(button);
leftBox.getChildren().remove(button);
}
}
// 检查游戏是否结束
private boolean checkStatus() {
if (wolfPos == 1 && goatPos == 1 && cabbagePos == 1) {
setStatus("恭喜你,过河成功!");
return true;
} else if (farmerPos != wolfPos && farmerPos == goatPos) {
return false;
} else if (farmerPos != cabbagePos && farmerPos == goatPos) {
return false;
} else {
return true;
}
}
// 设置状态信息
private void setStatus(String text) {
statusLabel.setText(text);
}
// 重新开始游戏
private void reset() {
farmerPos = 0;
wolfPos = 0;
goatPos = 0;
cabbagePos = 0;
boatPos = 0;
setButtonPosition(farmerButton, farmerPos);
setButtonPosition(wolfButton, wolfPos);
setButtonPosition(goatButton, goatPos);
setButtonPosition(cabbageButton, cabbagePos);
setButtonPosition(boatButton, boatPos);
setStatus("农夫要把狼、羊、菜和自己运到对岸。但船太小,只能装下农夫和一样东西。");
}
public static void main(String[] args) {
launch(args);
}
}
```
注意:本示例中没有对界面进行美化,只是简单的布局。如果需要美化界面,可以使用 CSS 样式表或 JavaFX 自带的皮肤。
阅读全文