记忆力测试javafx代码
时间: 2023-07-09 15:09:02 浏览: 119
记忆力测试程序源代码
以下是一个简单的记忆力测试JavaFX代码示例:
```java
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.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;
public class MemoryTest extends Application {
private ArrayList<Button> buttons = new ArrayList<>();
private ArrayList<String> colors = new ArrayList<>();
private int numButtons = 6;
private int currentLevel = 1;
private int buttonCounter = 0;
private int score = 0;
private boolean playerTurn = false;
private boolean gameOver = false;
@Override
public void start(Stage primaryStage) {
// Set up colors
colors.add("red");
colors.add("blue");
colors.add("green");
colors.add("yellow");
colors.add("purple");
colors.add("orange");
// Set up UI
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 400, 400);
Label levelLabel = new Label("Level " + currentLevel);
grid.add(levelLabel, 0, 0);
Button startButton = new Button("Start");
startButton.setOnAction(e -> {
startGame(grid);
});
grid.add(startButton, 1, 0);
StackPane scorePane = new StackPane();
Label scoreLabel = new Label("Score: " + score);
scorePane.getChildren().add(scoreLabel);
StackPane.setAlignment(scoreLabel, Pos.CENTER);
grid.add(scorePane, 2, 0);
// Set up buttons
for (int i = 0; i < numButtons; i++) {
Button button = new Button();
button.setMinSize(50, 50);
button.setMaxSize(50, 50);
button.setOnAction(e -> {
if (playerTurn && !gameOver) {
checkButton(button);
}
});
buttons.add(button);
}
// Add buttons to grid
int row = 1;
int col = 0;
for (int i = 0; i < numButtons; i++) {
grid.add(buttons.get(i), col, row);
col++;
if (col == 3) {
col = 0;
row++;
}
}
primaryStage.setScene(scene);
primaryStage.show();
}
private void startGame(GridPane grid) {
// Disable start button
Button startButton = (Button) grid.getChildren().get(1);
startButton.setDisable(true);
// Shuffle colors and assign to buttons
Collections.shuffle(colors);
for (int i = 0; i < numButtons; i++) {
buttons.get(i).setStyle("-fx-background-color: " + colors.get(i));
}
// Wait for 3 seconds
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Hide colors and enable buttons
for (int i = 0; i < numButtons; i++) {
buttons.get(i).setStyle("");
}
playerTurn = true;
}
private void checkButton(Button button) {
// Increment button counter and update score label
buttonCounter++;
score++;
StackPane scorePane = (StackPane) button.getParent().getChildrenUnmodifiable().get(2);
Label scoreLabel = (Label) scorePane.getChildren().get(0);
scoreLabel.setText("Score: " + score);
// Check if player has clicked all buttons
if (buttonCounter == numButtons) {
playerTurn = false;
buttonCounter = 0;
currentLevel++;
Label levelLabel = (Label) button.getParent().getChildrenUnmodifiable().get(0);
levelLabel.setText("Level " + currentLevel);
// Check if player has won game
if (currentLevel > 5) {
gameOver = true;
Label gameOverLabel = new Label("Game Over!");
StackPane.setAlignment(gameOverLabel, Pos.CENTER);
StackPane.setMargin(gameOverLabel, new Insets(0, 0, 100, 0));
StackPane gamePane = (StackPane) button.getParent().getParent();
gamePane.getChildren().add(gameOverLabel);
} else {
startGame((GridPane) button.getParent());
}
}
}
public static void main(String[] args) {
launch(args);
}
}
```
该应用程序使用 JavaFX 构建,包含一个网格布局和一些按钮。在游戏开始时,应用程序会将按钮上的颜色随机化,并在三秒钟内显示这些颜色。玩家需要记住它们并在随后的回合中点击相应的按钮。如果玩家成功记住所有颜色,他们会进入下一级。如果他们完成了五个级别,他们获胜。如果玩家犯错,则游戏结束。该应用程序还包括一个得分计数器和一个“分数”标签,以及一个“级别”标签。
阅读全文