Pyhton,Tkinter,剪刀石头布小游戏,可视化

上篇博客使用代码实现了剪刀石头布小游戏的基本功能,这次使用Tkinter将其可视化了一下,哈哈。。。

PS:博主的代码是自己查找一些资料然后自己编写调通的,实现了基本的功能,同时代码中可能存在一些或多或少的问题,望见谅。。另,博主的所有博客只是作为自己学习过程的一个记录,如果能对他人有帮助,不胜荣幸。

环境:window7,Python3.5

废话不多说,直接上代码

#coding=utf-8
from tkinter import *
import random

#初始化全局变量
global windows
global human
global number_total
global number_win
global number_lose
global number_draw
windows = 0
human = 0
number_total = 0
number_win = 0
number_lose = 0
number_draw = 0

#随机显示windows出的手势
def windows_function():

    global windows
    windows = random.randint(0, 2)

    if windows == 0:
        label_image_window.configure(image = image_Scissors)
    elif windows == 1:
        label_image_window.configure(image = image_Rock)
    else:
        label_image_window.configure(image = image_Paper)

#判断human和windows的胜负
def judgement(human,windows):
    global number_win
    global number_lose
    global number_draw
    global number_total

    if (human == 0 and windows == 2) or (human == 1 and windows == 0) or (human == 2 and windows == 1):
        number_win+=1
        label_down.configure(text='Congratulations, You Won !')
    elif human == windows:
        label_down.configure(text = 'Draw, Cheer up !')
        number_draw+=1
    else:
        number_lose+=1
        label_down.configure(text='Sorry, You Lost !')

    label_result.configure(text = 'Win: '+str(number_win)+
                               '\nLose: '+str(number_lose)+
                               '\nDraw: '+str(number_draw)+
                               '\n Total: '+str(number_total))

def hit_Scissors():
    global number_total
    number_total+=1
    label_image.configure(image = image_Scissors)
    windows_function()
    human = 0
    judgement(human,windows)

def hit_Rock():
    global number_total
    number_total+=1
    label_image.configure(image = image_Rock)
    windows_function()
    human = 1
    judgement(human,windows)

def hit_Paper():
    global number_total
    number_total+=1
    label_image.configure(image = image_Paper)
    windows_function()
    human = 2
    judgement(human,windows)

def pass_function():
    pass


top = Tk()
top.title('Heimu Python')
top.geometry('450x400')

############-------设置label--------##############

#label_human
label_human = Label(top,text = 'Human',
                    bg = 'green',
                    fg = 'Ghostwhite',
                    font = ('Arial',12)
                    )
label_human.place(x=70,y=172,ancho='nw')

#label_windows
label_windows = Label(top,text = 'windows',
                      bg = 'green',
                      fg = 'Ghostwhite',
                      font = ('Arial',12)
                      )
label_windows.place(x=332,y=172,ancho='nw')

#label_down
label_down = Label(top,text = 'Are You Ready? Go!Go!Go!!!',
                   bg = 'green',
                   fg = 'ghostwhite',
                   font = ('Arial',12),
                   width=30,height=2
                   )
label_down.place(x=90,y=330,ancho='nw')

#label_result
label_result = Label(top,text = 'Win: '+str(number_win)+
                               '\nLose: '+str(number_lose)+
                               '\nDraw: '+str(number_draw)+
                               '\n Total: '+str(number_total),
                     bg = 'green',
                     fg = 'Ghostwhite')
label_result.place(x=0,y=315,ancho='nw')

############------------设置显示图片的label-------------#############

#读取用到的图片,PNG格式
image_Scissors = PhotoImage(file='jiandao_100.png')
image_Rock = PhotoImage(file='shitou_100.png')
image_Paper = PhotoImage(file='bu_100.png')
image_kobe = PhotoImage(file='kobe_100.png')
image_kobe1 = PhotoImage(file='kobe1_100.png')
image_welcome = PhotoImage(file='welcome.gif')

#初始化human的图片显示kobe
label_image = Label(top,image=image_kobe1)
label_image.place(x=40,y=200,ancho='nw')

#初始化windows的图片显示kobe
label_image_window = Label(top,image=image_kobe1)
label_image_window.place(x=315,y=200,ancho='nw')

#显示welcome图片
label_welcome = Label(top,image=image_welcome)
label_welcome.place(x=0,y=40,ancho='nw')

#显示kobe,logo
label_kobe = Label(top,image=image_kobe)
label_kobe.place(x=220,y=40,ancho='nw')

############-------------设置button-----------------################

#button_main
button_main = Button(top,text = 'Heimu Game !',

                   bg = 'green',
                    fg = 'Ghostwhite',
                   command = pass_function,
                   font = ('Arial',12),

                   )
button_main.place(x=180,y=3,ancho='nw')

#button_Scissors
button_Scissors = Button(top,text = 'Scissors',
                        command = hit_Scissors,
                        bg = 'green',
                        fg = 'Ghostwhite',
                        font = ('Arial',12)
                        )
button_Scissors.place(x=200,y=190,ancho='nw')

#button_Rock
button_Rock = Button(top,text = 'Rock',
                       command = hit_Rock,
                       bg = 'green',
                       fg = 'Ghostwhite',
                       font = ('Arial',12)
                       )
button_Rock.place(x=211,y=230,ancho='nw')

#button_Paper
button_Paper = Button(top,text = 'Paper',
                   command = hit_Paper,
                   bg = 'green',
                   fg = 'Ghostwhite',
                   font = ('Arial',12)
                   )
button_Paper.place(x=207,y=270,ancho='nw')


top.mainloop()

效果如下图:

Pyhton,Tkinter,剪刀石头布小游戏,可视化_第1张图片
Pyhton,Tkinter,剪刀石头布小游戏,可视化_第2张图片
Pyhton,Tkinter,剪刀石头布小游戏,可视化_第3张图片
Pyhton,Tkinter,剪刀石头布小游戏,可视化_第4张图片
Pyhton,Tkinter,剪刀石头布小游戏,可视化_第5张图片

图中加入了蜗壳的图片,哈哈。。。。

蜗壳的8号和24号球衣马上就要同时退役了!历史第一人!!!!!!!!!

你可能感兴趣的:(python,tkinter,可视化,剪刀石头布,小游戏)