python实现扑克牌交互式界面发牌程序

所属分类: 脚本专栏 / python 阅读数: 1929
收藏 0 赞 0 分享

本文实例为大家分享了python实现扑克牌交互式界面发牌程序的具体代码,供大家参考,具体内容如下

注:图片自行在网上下载、替换即可

#coding=utf-8

class Card(): #扑克牌类
  points=['1','2','3','4','5','6','7','8','9','10','11','12','13']
  suits=['1','2','3','4'] #花色
  def __init__(self,points,suits):
    self.points=points
    self.suits=suits
  def __str__(self):
    rep=self.suits+'-'+self.points
    return rep
class Hand():
  def __init__(self):
    self.cards=[]
  def add(self,card):
    self.cards.append(card)
  def __str__(self):
    rep=''
    for card in self.cards:
      rep+=str(card)+'\t'
    return rep

class Poke(Hand):
  def generate_poke(self):
    for point in Card.points:
      for suit in Card.suits:
        self.add(Card(point,suit))
  def random_poke(self):
    import random
    random.shuffle(self.cards)
  def deal(self,hands,limit_hand=13):
    for rounds in range (limit_hand):
      for hand in hands:
        if self.cards:
          top=self.cards[0]
          self.cards.remove(top)
          hand.add(top)

def print_poke(players):
  results=[player.__str__() for player in players]
  pk=[]
  for result in results:
    pk1=result.rstrip()
    pk2=pk1.split('\t')
    pk.append(pk2)
  image_name=[]
  for i in range(len(pk)):
    for j in pk[i]:
      str_name='D:\pukepai\images\\{}.gif'.format(j)
      image_name.append(str_name)
  wj=[image_name[i:i+13] for i in range(len(image_name)) if i%13==0]
  return wj

def restart():
  tk.messagebox.showinfo("hello python","sdfg")

players=[Hand(),Hand(),Hand(),Hand()]
pockers=Poke()
pockers.generate_poke()
pockers.random_poke()
pockers.deal(players,13)
wj=print_poke(players) 

import tkinter as tk
win=tk.Tk()
win.title("扑克牌程序")
win.geometry('800x600')
cv=tk.Canvas(win,bg='red',width=800,height=600)
from PIL import Image,ImageTk
imgs=[]
(p1,p2,p3,p4)=([],[],[],[])
for i in range(4):
  for j in range(13):
    img=Image.open(wj[i][j])
    imgs.insert(i*13+j,ImageTk.PhotoImage(img))
p1=imgs[0:13]  
p2=imgs[13:26]
p3=imgs[26:39]
p4=imgs[39:52]
for x in range(0,13):
  cv.create_image((200+20*x,80),image=p1[x])
  cv.create_image((100,150+20*x),image=p2[x])
  cv.create_image((200+20*x,500),image=p3[x])
  cv.create_image((560,150+20*x),image=p4[x])

#添加重新发牌按钮
from tkinter.messagebox import *
bt1=tk.Button(win,text='重新发牌',width=60,height=40,command=restart)
bt1.place(x=290,y=380,width=60,height=40)

cv.pack()
win.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多