石头、剪子、布小游戏图形化
也是之前编写了一个石头、剪子、布的小游戏,总感觉界面不够友好,AI时代到来,一切都无所不能,而且编程效率大大提高了。所以用AI大模型进行程序代码重构,再稍加修改,效果还不错。
源码如下:
import tkinter as tk
import random
'''
✊: \U0001F44A (对应Unicode码点:U+1F44A)
✌️: \U0000270C (对应Unicode码点:U+270C)
👋: \U0001F44B (对应Unicode码点:U+1F44B)
'''
# choices_dict = {1:'石头', 2:'剪刀', 3:'布'}
unicode_dict = {'石头':'\U0001F44A', '剪刀':'\U0000270C', '布':'\U0001F44B'}
def play_game(choice):
player_choice.set(f"玩家选择了:{choice} {unicode_dict[choice]}")
computer_choice = random.choice(['石头', '剪刀', '布'])
result_text.set(f"电脑选择了:{computer_choice} {unicode_dict[computer_choice]}")
if choice == computer_choice:
result.set("平局!")
elif (choice == '石头' and computer_choice == '剪刀') or \
(choice == '剪刀' and computer_choice == '布') or \
(choice == '布' and computer_choice == '石头'):
result.set("玩家获胜!")
else:
result.set("电脑获胜!")
# 创建应用窗口
root = tk.Tk()
root.title("石头剪刀布游戏")
# 创建变量来存储玩家选择和结果
player_choice = tk.StringVar()
result_text = tk.StringVar()
result = tk.StringVar()
# 创建标签来显示玩家选择
player_choice_label = tk.Label(root, textvariable=player_choice)
player_choice_label.pack()
# 创建按钮让用户选择石头、剪刀或布,并将其放在同一行
frame = tk.Frame(root)
frame.pack()
rock_button = tk.Button(frame, text="石头", command=lambda: play_game('石头'))
rock_button.pack(side=tk.LEFT)
paper_button = tk.Button(frame, text="布", command=lambda: play_game('布'))
paper_button.pack(side=tk.LEFT)
scissors_button = tk.Button(frame, text="剪刀", command=lambda: play_game('剪刀'))
scissors_button.pack(side=tk.LEFT)
# 创建标签来显示游戏结果
result_label = tk.Label(root, textvariable=result_text)
result_label.pack()
# 创建标签来显示游戏结果
result_label = tk.Label(root, textvariable=result)
result_label.pack()
# 运行应用程序
root.mainloop()
运行效果如下: