如何实现用户点击1-10按钮猜隐藏钻石(限3次尝试)的功能?
猜钻石游戏(Tkinter)代码完善
需求说明
- 系统通过随机函数在10个按钮中隐藏钻石
- 用户有3次尝试机会找出目标按钮
- 点击「隐藏钻石」按钮:生成随机钻石位置,启用所有游戏按钮,同时禁用自身
- 用户点击按钮时:检查是否猜中,累计尝试次数;3次未中提示失败,猜中提示成功
现有代码问题
原代码存在多处语法错误、逻辑混乱:
- 全局变量未正确声明,导致无法修改
- 按钮点击事件未绑定,用户点击按钮无响应
- 随机数生成函数写法错误(错误使用
@staticmethod,重复导入random) checkGuess函数逻辑错误,缩进混乱,未正确对比用户选择和随机位置- 未使用tkinter消息框提示结果,而是用控制台输出
- 按钮状态切换逻辑冲突
完善后的完整代码
from tkinter import * import tkinter.messagebox as msgBox import random # 初始化主窗口 window = Tk() window.title("找钻石") window.configure(bg="gray", pady=100) window.geometry("720x420") window.resizable(0, 0) # 全局变量:记录尝试次数、钻石位置 number_of_guesses = 0 random_box_number = 0 def start_game(): """开始游戏:生成随机钻石位置,重置尝试次数,启用游戏按钮""" global number_of_guesses, random_box_number number_of_guesses = 0 random_box_number = random.randint(1, 10) # 启用所有游戏按钮 for btn in game_buttons: btn.configure(state=NORMAL) # 禁用「隐藏钻石」按钮 HideTheDiamond.configure(state=DISABLED) msgBox.showinfo("游戏开始", "钻石已隐藏!你有3次机会找到它。") def check_guess(box_num): """处理用户猜测:判断是否猜中,更新尝试次数,提示结果""" global number_of_guesses number_of_guesses += 1 if box_num == random_box_number: msgBox.showinfo("恭喜!", "你找到了钻石!游戏结束。") # 禁用所有游戏按钮 for btn in game_buttons: btn.configure(state=DISABLED) # 重新启用「隐藏钻石」按钮,允许新游戏 HideTheDiamond.configure(state=NORMAL) else: remaining = 3 - number_of_guesses if remaining > 0: msgBox.showwarning("猜错了", f"不对哦!你还有{remaining}次机会。") else: msgBox.showinfo("游戏结束", f"很遗憾,你没找到钻石!钻石在第{random_box_number}个按钮里。") # 禁用所有游戏按钮 for btn in game_buttons: btn.configure(state=DISABLED) # 重新启用「隐藏钻石」按钮 HideTheDiamond.configure(state=NORMAL) # 创建「隐藏钻石」按钮 HideTheDiamond = Button(window, text="隐藏钻石", bg="green", command=start_game) # 创建10个游戏按钮,并绑定点击事件 Button1 = Button(window, text="1", width=10, height=3, bg="yellow", state=DISABLED, command=lambda: check_guess(1)) Button2 = Button(window, text="2", width=10, height=3, bg="blue", state=DISABLED, command=lambda: check_guess(2)) Button3 = Button(window, text="3", width=10, height=3, bg="dark red", state=DISABLED, command=lambda: check_guess(3)) Button4 = Button(window, text="4", width=10, height=3, bg="green", state=DISABLED, command=lambda: check_guess(4)) Button5 = Button(window, text="5", width=10, height=3, bg="red", state=DISABLED, command=lambda: check_guess(5)) Button6 = Button(window, text="6", width=10, height=3, bg="purple", state=DISABLED, command=lambda: check_guess(6)) Button7 = Button(window, text="7", width=10, height=3, bg="brown", state=DISABLED, command=lambda: check_guess(7)) Button8 = Button(window, text="8", width=10, height=3, bg="orange", state=DISABLED, command=lambda: check_guess(8)) Button9 = Button(window, text="9", width=10, height=3, bg="dark blue", state=DISABLED, command=lambda: check_guess(9)) Button10 = Button(window, text="10", width=10, height=3, bg="pink", state=DISABLED, command=lambda: check_guess(10)) # 把游戏按钮存入列表,方便批量操作 game_buttons = [Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10] # 创建提示标签 label11 = Label(window, text="点击「隐藏钻石」按钮开始游戏。\n然后点击你认为藏有钻石的按钮。\n你有3次机会找到它。", bg="gray") # 布局所有组件 Button1.grid(row=1, column=1, padx=10, pady=10, sticky=W) Button2.grid(row=1, column=2, padx=10, pady=10, sticky=W) Button3.grid(row=1, column=3, padx=10, pady=10, sticky=W) Button4.grid(row=1, column=4, padx=10, pady=10, sticky=W) Button5.grid(row=1, column=5, padx=10, pady=10, sticky=W) Button6.grid(row=2, column=1, padx=10, pady=10, sticky=W) Button7.grid(row=2, column=2, padx=10, pady=10, sticky=W) Button8.grid(row=2, column=3, padx=10, pady=10, sticky=W) Button9.grid(row=2, column=4, padx=10, pady=10, sticky=W) Button10.grid(row=2, column=5, padx=10, pady=10, sticky=W) label11.grid(row=5, column=2, sticky=W) HideTheDiamond.grid(row=5, column=5) # 启动主循环 window.mainloop()
代码关键说明
- start_game函数:负责初始化游戏状态,生成1-10的随机数作为钻石位置,重置尝试次数,启用所有游戏按钮并禁用自身。
- check_guess函数:接收用户点击的按钮编号,对比钻石位置:
- 猜中:弹出成功提示,禁用所有游戏按钮,重新启用「隐藏钻石」按钮。
- 猜错:计算剩余次数,弹出提示;次数用完时弹出失败提示,同时显示钻石位置。
- 游戏按钮列表:将10个按钮存入列表,方便批量启用/禁用操作,简化代码。
- lambda表达式:给每个按钮绑定点击事件时,用lambda传递对应的按钮编号到
check_guess函数。
内容的提问来源于stack exchange,提问作者Natasha
相关产品推荐
相关产品推荐

