使用python的TK做一个可视化的猜拳游戏。
使用python3,源代码如下:
# coding=utf-8 __author__="沈老师" from tkinter import * import tkinter.messagebox as tm import random
def guess(): while 1: root = Tk() root.title("咱们来猜拳") Label(root, text="请输入:剪刀(0) 石头(1) 布(2):", font=("Arial", 35)).grid(row=0, column=0) num = Entry(root, width=20) num.grid(row=0, column=1) Button(root, text="比一比", command=root.quit, font=("Arial", 30)).grid(row=1, column=1) root.mainloop() player = int(num.get()) # player = input('请输入:剪刀(0) 石头(1) 布(2):') # player = int(player) computer = random.randint(0,2) # 用来进行测试 #print('player=%d,computer=%d',(player,computer)) if player==0: txt1="你出的剪刀," elif player==1: txt1 = "你出的石头," elif player==2: txt1 = "你出的布," if computer==0: txt2 = "电脑出的剪刀," elif computer==1: txt2 = "电脑出的石头," elif computer==2: txt2 = "电脑出的布," if ((player == 0) and (computer == 2)) or ((player ==1) and (computer == 0)) or ((player == 2) and (computer == 1)): tm.showinfo("提示", "%s\n你赢了,哈哈,你太厉害了!"%(txt1+txt2)) break elif player == computer: tm.showinfo("提示", "%s\n平局,再来一局" % (txt1 + txt2)) elif player not in (0,1,2): tm.showinfo("提示", "不要乱出拳哦,重新来!") else: tm.showinfo("提示", "%s\n输了,不要走,洗洗手接着来,决战到天亮" % (txt1 + txt2)) guess()
游戏规则:与电脑猜拳,只有赢过电脑,程序结束,否则,程序一直进行下去。