import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
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.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;public class GoldMinerGame extends Application {
private int totalGold = 100;
private int currentGold = 0;
private int round = 1;
private int difficultyLevel = 1;
private int specialEventRound = 3;
private boolean specialEventOccurred = false;
private Label roundLabel;
private Label scoreLabel;
private Label goldLabel;
private TextField minedGoldField;
private Rectangle progressBar;public static void main(String[] args) {
launch(args);
}@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("黄金矿工游戏");GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));roundLabel = new Label("第 " + round + " 轮");
scoreLabel = new Label("得分:0");
goldLabel = new Label("当前金块数量:0");
minedGoldField = new TextField();
progressBar = new Rectangle(0, 0, 0, 20);
progressBar.setFill(Color.GREEN);Button mineButton = new Button("挖掘");
mineButton.setOnAction(e -> mineGold());HBox hbox = new HBox(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(mineButton);
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().addAll(roundLabel, scoreLabel, goldLabel, minedGoldField, progressBar, hbox);
grid.add(vbox, 0, 0);Scene scene = new Scene(grid, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}private void mineGold() {
int minedGold = Integer.parseInt(minedGoldField.getText());if (minedGold <= 0) {
System.out.println("挖掘数量必须大于零!");
return;
}if (minedGold > totalGold - currentGold) {
System.out.println("超过剩余金块数量!");
return;
}int score = minedGold * difficultyLevel;
currentGold += minedGold;if (round % specialEventRound == 0 && !specialEventOccurred) {
System.out.println("特殊事件发生!获得双倍分数!");
score *= 2;
specialEventOccurred = true;
}roundLabel.setText("第 " + round + " 轮");
scoreLabel.setText("得分:" + score);
goldLabel.setText("当前金块数量:" + currentGold);if (currentGold == totalGold) {
System.out.println("恭喜你,成功挖掘到所有金块!");
return;
}if (round % 5 == 0) {
difficultyLevel++;
System.out.println("难度级别提升!当前难度级别:" + difficultyLevel);
}round++;
// 更新进度条动画
double progress = (double) currentGold / totalGold;
animateProgressBar(progress);
}private void animateProgressBar(double progress) {
Timeline timeline = new Timeline();
KeyValue keyValue = new KeyValue(progressBar.widthProperty(), progress * 400);
KeyFrame keyFrame = new KeyFrame(Duration.seconds(1), keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
}
}
用Java写一个黄金矿工游戏
最新推荐文章于 2024-06-05 13:49:04 发布