python可视化石头剪刀布界面

 python是一个简单高效的编程语言,其易于扩展和丰富的库深受人们的欢迎。它让编程变得更简单,易懂。现在它也可以用来写一个一个简单的小游戏,无需网络能玩一天!代码如下:

import random
import tkinter
from tkinter import messagebox
from tkinter import *

user=['石头','剪刀','布']
p_score=0
c_score=0

window = tkinter.Tk()
window.title("猜拳大王")
window.geometry("600x460+500+200")
window["background"] = "#EDEDED"

label1 = tkinter.Label(window,bg="#EDEDED",fg="#F00000",text="猜拳游戏",font=('黑体',15,'bold')).pack()

label2 = tkinter.Label(window,bg="#EDEDED",fg="#F00000",text="游戏规则:\n使用剪刀石头布对决!",font=('黑体',15,'bold')).pack()       

label3 = tkinter.Label(window,bg="#EDEDED",fg="#F00000",text="  请输入你的选择(石头、剪刀、布):",font=('黑体',15,'bold')).pack()


text = tkinter.Entry(window,width=30,bd=5,bg="#EDEDED")
text.pack()

def guess_game():
    global p_score
    global c_score
    compute = random.choice(user)
    guess=text.get()
    if guess not in user:
        tkinter.messagebox.showerror('输入手势不合法!!!')
    else:
        if(guess=="石头"and compute=="剪刀")or(guess=="剪刀"and compute=="布")\
            or(guess=="布"and compute=="石头"):
            tkinter.messagebox.showinfo("胜利","你赢了!")
            p_score+=1
            c_score-=1
        elif guess==compute:
            tkinter.messagebox.showinfo("平局",'我们平了')
            p_score+=0
            c_score+=0
        else:
            tkinter.messagebox.showinfo("失败","我赢了,你输了")
            p_score-=1
            c_score+=1
       
        tkinter.messagebox.showinfo("分数展示","你的得分为%d"%p_score+"分\n计算机的得分为%d"%c_score+"分")

    if p_score>=0:
            tkinter.messagebox.showinfo('获胜!',"祝贺你,在此次比赛中获胜!")
    else:
            tkinter.messagebox.showinfo("你输了","很不幸,你在比赛中与冠军失之交臂")

def guess_over():
    tkinter.messagebox.showinfo("游戏结束了,下次见!")
    window.quit()
    
button1=tkinter.Button(window,text="开始游戏",command=guess_game,width=10,height=1,font=("等线",12))
button1.place(x=200,y=370)

button2=tkinter.Button(window,text="游戏结束",command=guess_over,width=10,height=1,bg="yellow",font=("等线",12))
button2.place(x=320,y=370)

window.mainloop()

本代码使用了python的tkinter库实现了可视化以及使用了random模块来形成随机的手势,并且有一个独立的窗口如下:python可视化石头剪刀布界面_第1张图片

你可能感兴趣的:(python,python小游戏,tkinter,python,numpy,开发语言)