Pyhton,Tkinter,剪刀石頭布小遊戲,視覺化
阿新 • • 發佈:2019-02-10
上篇部落格使用程式碼實現了剪刀石頭布小遊戲的基本功能,這次使用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()
效果如下圖: