ZBLOG

能写一个弹窗游戏吗?不要在控制台输入

好的,以下是一个简单的弹窗游戏示例,使用 JavaFX 实现:

  1. import javafx.application.Application;
  2. import javafx.geometry.Pos;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.layout.VBox;
  7. import javafx.stage.Stage;
  8. public class PopupGame extends Application {
  9. private int score = 0; // 记录得分
  10. @Override
  11. public void start(Stage primaryStage) {
  12. Button startButton = new Button("开始游戏");
  13. Label scoreLabel = new Label("得分:0");
  14. Label resultLabel = new Label(); // 显示结果
  15. VBox root = new VBox(10, startButton, scoreLabel, resultLabel);
  16. root.setAlignment(Pos.CENTER);
  17. Scene scene = new Scene(root, 300, 200);
  18. startButton.setOnAction(e -> { // 点击开始按钮事件
  19. PopupPane popupPane = new PopupPane();
  20. popupPane.setOnScoreChange(score -> { // 监听得分变化
  21. this.score += score; // 更新总得分
  22. scoreLabel.setText("得分:" + this.score); // 更新显示的总得分
  23. if (score > 0) { // 猜对了,显示“恭喜你!”
  24. resultLabel.setText("恭喜你!");
  25. } else { // 猜错了,显示“很遗憾”
  26. resultLabel.setText("很遗憾");
  27. }
  28. popupPane.hide(); // 隐藏弹窗
  29. });
  30. popupPane.show(); // 显示弹窗
  31. });
  32. primaryStage.setScene(scene);
  33. primaryStage.setTitle("弹窗游戏");
  34. primaryStage.show();
  35. }
  36. public static void main(String[] args) {
  37. launch(args);
  38. }
  39. }
  40. class PopupPane extends Stage {
  41. private int answer;
  42. PopupPane() {
  43. this.answer = (int)(Math.random() * 100) + 1; // 随机生成答案
  44. Label promptLabel = new Label("请输入一个1-100之间的整数:");
  45. Label resultLabel = new Label(); // 显示结果
  46. Button submitButton = new Button("提交");
  47. VBox root = new VBox(10, promptLabel, submitButton, resultLabel);
  48. root.setAlignment(Pos.CENTER);
  49. Scene scene = new Scene(root, 300, 200);
  50. setScene(scene);
  51. setTitle("猜数字游戏");
  52. submitButton.setOnAction(e -> { // 点击提交按钮事件
  53. try {
  54. int guess = Integer.parseInt(promptLabel.getText().substring(8)); // 获取输入框中的数字
  55. if (guess == answer) { // 猜对了
  56. resultLabel.setText("恭喜你猜对了!");
  57. onScoreChange(1); // 分数加一
  58. } else { // 猜错了
  59. if (guess > answer) {
  60. resultLabel.setText("你猜的数字太大了!");
  61. } else {
  62. resultLabel.setText("你猜的数字太小了!");
  63. }
  64. onScoreChange(-1); // 分数减一
  65. }
  66. } catch (NumberFormatException ex) { // 输入不是整数
  67. resultLabel.setText("请输入一个有效的整数!");
  68. }
  69. promptLabel.setText("请输入一个1-100之间的整数:"); // 清空输入框
  70. answer = (int)(Math.random() * 100) + 1; // 随机生成新的答案
  71. });
  72. }
  73. void onScoreChange(int score) {} // 定义得分变化监听器,由 PopupGame 类实现
  74. }

在上面的代码中,PopupPane 类继承了 Stage 类,用于显示弹窗。当点击 开始游戏 按钮时,会创建一个新的 PopupPane 实例,并显示出来。玩家需要在输入框中输入一个整数,如果猜对了,则分数加一;否则分数减一。每次猜完后,程序会随机生成一个新的答案。

希望这个示例能够帮助您完成您自己的弹窗游戏。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5319

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?