记忆力测试javafx代码

时间: 2023-07-09 11:09:02 浏览: 50
以下是一个简单的记忆力测试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 构建,包含一个网格布局和一些按钮。在游戏开始时,应用程序会将按钮上的颜色随机化,并在三秒钟内显示这些颜色。玩家需要记住它们并在随后的回合中点击相应的按钮。如果玩家成功记住所有颜色,他们会进入下一级。如果他们完成了五个级别,他们获胜。如果玩家犯错,则游戏结束。该应用程序还包括一个得分计数器和一个“分数”标签,以及一个“级别”标签。

相关推荐

最新推荐

recommend-type

JavaFX实现UI美观效果代码实例

主要介绍了JavaFX实现UI美观效果代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

JavaFX 教程 (中文).docx

主要介绍Scene Builder 下使用一些组件以及应用程序打包、部署
recommend-type

javafx实现五子棋游戏

主要为大家详细介绍了javafx实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

阿里巴巴六个盒子实践.pptx

阿里巴巴六个盒子实践.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

优化MATLAB分段函数绘制:提升效率,绘制更快速

![优化MATLAB分段函数绘制:提升效率,绘制更快速](https://ucc.alicdn.com/pic/developer-ecology/666d2a4198c6409c9694db36397539c1.png?x-oss-process=image/resize,s_500,m_lfit) # 1. MATLAB分段函数绘制概述** 分段函数绘制是一种常用的技术,用于可视化不同区间内具有不同数学表达式的函数。在MATLAB中,分段函数可以通过使用if-else语句或switch-case语句来实现。 **绘制过程** MATLAB分段函数绘制的过程通常包括以下步骤: 1.
recommend-type

SDN如何实现简易防火墙

SDN可以通过控制器来实现简易防火墙。具体步骤如下: 1. 定义防火墙规则:在控制器上定义防火墙规则,例如禁止某些IP地址或端口访问,或者只允许来自特定IP地址或端口的流量通过。 2. 获取流量信息:SDN交换机会将流量信息发送给控制器。控制器可以根据防火墙规则对流量进行过滤。 3. 过滤流量:控制器根据防火墙规则对流量进行过滤,满足规则的流量可以通过,不满足规则的流量则被阻止。 4. 配置交换机:控制器根据防火墙规则配置交换机,只允许通过满足规则的流量,不满足规则的流量则被阻止。 需要注意的是,这种简易防火墙并不能完全保护网络安全,只能起到一定的防护作用,对于更严格的安全要求,需要
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依