当前位置:   article > 正文

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

python石头剪刀布游戏可视化

上篇博客使用代码实现了剪刀石头布小游戏的基本功能,这次使用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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190

效果如下图:

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/230048
推荐阅读
相关标签
  

闽ICP备14008679号