在好友的邀请下,尝试用tkinter做一个卡牌的普通界面显示,正好练习下python的写法。
花了两天学习,写了两天代码,做了个最基本的demo。显示如下:
其中需要引入的第三方库主要有,PIL、PILLOW,其它的都是直接引入即可。
1.1、这里贴下下载地址,以WINDOWS系统为例:
PILwin32:http://files.cnblogs.com/files/pcat/PILwin32.zip
PILwin64:http://files.cnblogs.com/files/pcat/PILwin64.zip
1.2、windows安装Pillow
pip install Pillow
或者在http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow 下载wheel文件使用pip安装
程序中用到的图片主要有:
需要图片资源的小伙伴可以自行上git上下载:
https://github.com/ishikota/PyPokerGUI
pypokergui--server--static
贴上程序的源码,仅供参考:
# -*- coding: utf-8 -*-
from Tkinter import *
from PIL import Image,ImageTk import Image,math,random,time,sched
import threading as thd
class card2:
#定义全局变量
#总牌数
num_list = ['', '', '', '', '', '', '','T', 'J','Q','K', 'A','']
# 全局变量纪录玩家当前要打出的牌数字
#当前牌面字符串
str_num = '';
#当前牌面字符串在num_list中的位置
index_num = -1;
# 中间桌面上牌的大小
mid_num = -2;
#判断是否会生成重复的牌
check_list = [];
#记录当前回合数
round_num = 1;
#当前玩家手中牌位置
cur_position = 0;
#定义玩家当前手中牌的集合
player_card_list = [];
player1_card_list = [];
player2_card_list = [];
player3_card_list = [];
player4_card_list = []; #变量判断玩家身份,默认为玩家
cur_identify = 1; def __init__(self):
self.tk = Tk()
#tk.geometry('1200x1600')
self.tk.title('A Easy Card Game') self.img = Image.open('images/poker_pot.png')
self.w_box = 120
self.h_box = 120
self.w, self.h = self.img.size
# w,h = img2.size
# 缩放图像让它保持比例,同时限制在一个矩形框范围内
self.img_resized = self.resize(self.w, self.h, self.w_box, self.h_box, self.img)
self.photo = ImageTk.PhotoImage(self.img_resized)
# frm_TL = Frame(bg = 'WHITE',width = 300, height = 200)
self.frm_T = Frame(bg='white', width=300, height=200)
self.frm_L = Frame(bg='white', width=300, height=400)
self.frm_M = Frame(bg='white', width=200, height=400)
self.frm_R = Frame(bg='white', width=300, height=400)
self.frm_B = Frame(bg='white', width=400, height=200)
# frm_TL.grid(row=0,column=0)
self.frm_T.grid(row=0, column=1)
self.frm_L.grid(row=1, column=0)
self.frm_M.grid(row=1, column=1)
self.frm_R.grid(row=1, column=2)
self.frm_B.grid(row=2, column=1)
# Top
Label(self.frm_T, text="Player3", font=('Arial', 16)).grid(column=1, sticky=N)
Button(self.frm_T, text="重新发牌", command=self.replay, width=8, height=1,
font=('Arial', 16)).grid(row=0, column=3)
self.frm_T_label_L = Label(self.frm_T, image=self.photo)
self.frm_T_label_L.image = self.photo
self.frm_T_label_L.grid(row=1, column=0, sticky=E)
# frm_T_label_L.grid(row=1,column=1,columnspan=2,rowspan=2,sticky=W+E+N+S,padx=5,pady=5)
self.frm_T_label_M = Label(self.frm_T, image=self.photo)
self.frm_T_label_M.image = self.photo
self.frm_T_label_M.grid(row=1, column=1,padx=10, sticky=E)
self.frm_T_label_R = Label(self.frm_T, image=self.photo)
self.frm_T_label_R.image = self.photo
self.frm_T_label_R.grid(row=1, column=2, sticky=E) # Left
Label(self.frm_L, text="Player4", font=('Arial', 16)).grid(row=1, column=0, sticky=N)
self.frm_L_label_T = Label(self.frm_L, image=self.photo)
self.frm_L_label_T.image = self.photo
self.frm_L_label_T.grid(row=0, column=1, sticky=E)
self.frm_L_label_T = Label(self.frm_L, image=self.photo)
self.frm_L_label_T.image = self.photo
self.frm_L_label_T.grid(row=1, column=1, sticky=E)
self.frm_L_label_T = Label(self.frm_L, image=self.photo)
self.frm_L_label_T.image = self.photo
self.frm_L_label_T.grid(row=2, column=1, sticky=E) # Middle
self.frm_M_label_T = Label(self.frm_M, image=self.photo)
self.frm_M_label_T.image = self.photo
self.frm_M_label_T.grid(row=1, column=0, sticky=E)
self.frm_M_text = Text(self.frm_M, width=30, height=6, bg='Green', font=('Verdana', 15)) #self.frm_M_text.insert('1.0', 'this is '+str(self.round_num)+' round\n')
#self.frm_M_text.insert(END, 'last round')
self.frm_M_text.grid(row=2, column=0, columnspan=2) # Right frm_R
self.frm_R_label_T = Label(self.frm_R, image=self.photo)
self.frm_R_label_T.image = self.photo
self.frm_R_label_T.grid(row=0, column=0, sticky=E)
self.frm_R_label_T = Label(self.frm_R, image=self.photo)
self.frm_R_label_T.image = self.photo
self.frm_R_label_T.grid(row=1, column=0, sticky=E)
self.frm_R_label_T = Label(self.frm_R, image=self.photo)
self.frm_R_label_T.image = self.photo
self.frm_R_label_T.grid(row=2, column=0, sticky=E)
Label(self.frm_R, text="Player2", font=('Arial', 16)).grid(row=1, column=1, sticky=N) # 初始化玩家手中牌
self.load_sys() self.main_start() self.frm_B_label_T_4 = Label(self.frm_B, image=self.photo)
self.frm_B_label_T_4.image = self.photo
self.frm_B_label_T_4.grid(row=0, column=3, sticky=E)
Label(self.frm_B, text="Player1", font=('Arial', 16)).grid(row=1, column=1, sticky=N)
Button(self.frm_B, text="出牌", command=self.discard, width=6, height=1,
font=('Arial', 16)).grid(row=1, column=3)
Button(self.frm_B, text="PASS", command=self.player_pass, width=6, height=1,
font=('Arial', 16)).grid(row=1, column=4) def resize(self,w, h, w_box, h_box, pil_image):
'''''
resize a pil_image object so it will fit into
a box of size w_box times h_box, but retain aspect ratio
对一个pil_image对象进行缩放,让它在一个矩形框内,还能保持比例
''' f1 = 1.0 * w_box / w # 1.0 forces float division in Python2
f2 = 1.0 * h_box / h
factor = min([f1, f2])
# print(f1, f2, factor) # test
# use best down-sizing filter
width = int(w * factor)
height = int(h * factor)
return pil_image.resize((width, height), Image.ANTIALIAS)
def replay(self):
self.load_sys() #初始化玩家手中牌
def load_sys(self):
# 初始化当前玩家手中牌集合,先重置
self.player1_card_list = []
self.player2_card_list = []
self.check_list = []
self.cur_identify = 1
for i in range(3):
num = random.randint(0,12)
#判断是否生成重复的牌,去重
while self.check_list.__contains__(num):
num = random.randint(0, 12)
cur_str = self.num_list[num]
photo_current = ImageTk.PhotoImage(self.resize(self.w, self.h, self.w_box, self.h_box, Image.open('images/card_C' + str(cur_str) + '.png')))
frm_B_label_T_1 = Label(self.frm_B, image=photo_current)
frm_B_label_T_1.image = photo_current
frm_B_label_T_1.grid(row=0, column=i,sticky=E)
frm_B_label_T_1.bind('<ButtonRelease-1>', self.handlerAdaptor(self.choose, cur_str,i))
self.check_list.append(num)
self.player1_card_list.append(cur_str)
#生成玩家2手中的牌
num2 = random.randint(0, 12)
# 判断是否生成重复的牌,去重
while self.check_list.__contains__(num2):
num2 = random.randint(0, 12)
cur_str2 = self.num_list[num2]
self.check_list.append(num2)
self.player2_card_list.append(cur_str2) print(self.player1_card_list)
print(self.player2_card_list)
#记录当前轮数并递增
self.frm_M_text.insert('1.0', 'this is ' + str(self.round_num) + ' round\n')
self.round_num+=1 # 清空桌面上的牌
frm_M_label_T = Label(self.frm_M, image=self.photo)
frm_M_label_T.image = self.photo
frm_M_label_T.grid(row=1, column=0, sticky=E)
# 重置玩家手中当前牌
frm_B_label_T_4 = Label(self.frm_B, image=self.photo)
frm_B_label_T_4.image = self.photo
frm_B_label_T_4.grid(row=0, column=3, sticky=E)
#重置桌面上牌的大小
self.mid_num = -2
# Bottom
def choose(self,event,a,position):
self.frm_M_text.insert('1.0','Now Your Card Is C'+str(a)+'\n')
photo_cur = ImageTk.PhotoImage(self.resize(self.w, self.h, self.w_box, self.h_box, Image.open('images/card_C' + str(a) + '.png')))
frm_B_label_T_4 = Label(self.frm_B, image=photo_cur)
frm_B_label_T_4.image = photo_cur
frm_B_label_T_4.grid(row=0, column=3, sticky=E)
self.str_num = a
self.cur_position = position
def handlerAdaptor(self,fun, *kwds):
'''''事件处理函数的适配器,相当于中介,此处接收2个参数,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧'''
return lambda event,fun=fun,kwds=kwds: fun(event, *kwds)
def discard(self):
if self.cur_identify == 1:
if self.str_num != '':
self.index_num = self.num_list.index(self.str_num)
if self.index_num == -1:
self.frm_M_text.insert('1.0','请选择一张牌\n')
#return '请选择一张牌'
#判断要打出的牌与桌面上的牌大小
elif self.index_num <= self.mid_num:
self.frm_M_text.insert('1.0', '请选择一张大牌\n')
# return '请选择一张大牌'
else:
photo_mid = ImageTk.PhotoImage(self.resize(self.w, self.h, self.w_box, self.h_box, Image.open('images/card_C' + str(self.str_num) + '.png')))
frm_M_label_T = Label(self.frm_M, image=photo_mid)
frm_M_label_T.image = photo_mid
frm_M_label_T.grid(row=1, column=0, sticky=E)
#清空玩家手中当前牌
frm_B_label_T_4 = Label(self.frm_B, image=self.photo)
frm_B_label_T_4.image = self.photo
frm_B_label_T_4.grid(row=0, column=3, sticky=E)
#frm_B_label_T_4.grid_forget()
#给当前桌面牌赋值
self.mid_num = self.index_num
#重置玩家手中牌
frm_B_label_T_X = Label(self.frm_B, image=self.photo)
frm_B_label_T_X.image = self.photo
frm_B_label_T_X.grid(row=0, column=self.cur_position, sticky=E)
self.frm_M_text.insert('1.0', 'Player1 Play The Card!'+ str(self.str_num) +'\n')
self.player1_card_list.remove(self.str_num)
if self.player1_card_list.__len__() == 0:
self.frm_M_text.insert('1.0', 'Congratulations! Player1 Win The Game!\n')
return
#当玩家打出牌后,切换到电脑2,调用电脑出牌方法
self.cur_identify +=1
self.discard_pc(self.cur_identify)
else:
self.frm_M_text.insert('1.0', '请等待其它玩家出牌\n') #没有可出的牌,切换到下一个对手
def player_pass(self):
# 重置中间牌的大小
self.mid_num = -2
#切换玩家
self.cur_identify += 1
self.discard_pc(self.cur_identify) # def player_pc(self,iden):
# if iden == 2:
# self.player_card_list = self.player2_card_list
#
# if iden ==3:
# self.cur_identify = 1 def discard_pc(self,iden):
card_list_pc = []
if iden == 2:
for i in range(self.player2_card_list.__len__()):
#将当前牌转化成索引号进行比较大小
self.index_pc_num = self.num_list.index(self.player2_card_list[i]) if self.index_pc_num > self.mid_num:
card_list_pc.append(self.player2_card_list[i])
card_list_pc.sort()
if len(card_list_pc) > 0:
self.index_pc_num_temp = self.num_list.index(card_list_pc[0])
self.mid_num = self.index_pc_num_temp
photo_mid = ImageTk.PhotoImage(self.resize(self.w, self.h, self.w_box, self.h_box,
Image.open('images/card_C' + str(card_list_pc[0]) + '.png')))
frm_M_label_T = Label(self.frm_M, image=photo_mid)
frm_M_label_T.image = photo_mid
frm_M_label_T.grid(row=1, column=0, sticky=E)
self.frm_M_text.insert('1.0', 'Player2 Play The Card!' + str(card_list_pc[0]) + '\n')
self.player2_card_list.remove(card_list_pc[0]) if self.player2_card_list.__len__() == 0:
self.frm_M_text.insert('1.0', 'Congratulations! Player2 Win The Game!\n')
return
# 当玩家打出牌后,切换到电脑2,调用电脑出牌方法
self.cur_identify += 1
if self.cur_identify == 3:
self.cur_identify = 1
elif len(card_list_pc) == 0:
self.frm_M_text.insert('1.0', '没有大过玩家的牌,请玩家出牌\n')
#重置中间牌的大小
self.mid_num = -2
self.cur_identify = 1 # # 实例化一个sched对象
# schedule = sched.scheduler(time.time, time.sleep)
def show_time(self):
cur_time = time.strftime('%Y-%m-%d %X',time.localtime())
top_time_label = Label(self.frm_T, text=cur_time, font=('Arial', 16))
top_time_label.grid(column=2, sticky=N)
while True:
cur_time = time.strftime('%Y-%m-%d %X', time.localtime())
top_time_label.configure(text=cur_time)
time.sleep(1)
def main_start(self):
thd.Timer(0, self.show_time).start()
def main():
d = card2()
mainloop() if __name__ == '__main__':
main()
注意:调整图片大小需要用到resize()方法,PNG图片需要用到ImageTk.PhotoImage(self.img_resized)转化方可显示。
增加了定时任务显示当前时间,每秒切换一次,注意不要占用主线程。
提出几个方向以待大神们能完善:
2.1、程序中只启用了player2与玩家互动,可以尝试启用4个。
2.2、只选择了一组牌进行显示,可以尝试加入4组牌,用file文件引入数据。
2.3、不同花色的相同数字比较可以用3.1、3.2、3.3、3.4浮点数比较大小。
2.4、图片显示可以增加更多,遗留的问题是图片重叠显示,感觉用tkinter的grid方式无法实现,可能自己水平有限。