python实现扑克牌交互式界面发牌程序
本文实例为大家分享了python实现扑克牌交互式界面发牌程序的具体代码,供大家参考,具体内容如下
注:图片自行在网上下载、替换即可
#coding=utf-8 classCard():#扑克牌类 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 returnrep classHand(): def__init__(self): self.cards=[] defadd(self,card): self.cards.append(card) def__str__(self): rep='' forcardinself.cards: rep+=str(card)+'\t' returnrep classPoke(Hand): defgenerate_poke(self): forpointinCard.points: forsuitinCard.suits: self.add(Card(point,suit)) defrandom_poke(self): importrandom random.shuffle(self.cards) defdeal(self,hands,limit_hand=13): forroundsinrange(limit_hand): forhandinhands: ifself.cards: top=self.cards[0] self.cards.remove(top) hand.add(top) defprint_poke(players): results=[player.__str__()forplayerinplayers] pk=[] forresultinresults: pk1=result.rstrip() pk2=pk1.split('\t') pk.append(pk2) image_name=[] foriinrange(len(pk)): forjinpk[i]: str_name='D:\pukepai\images\\{}.gif'.format(j) image_name.append(str_name) wj=[image_name[i:i+13]foriinrange(len(image_name))ifi%13==0] returnwj defrestart(): tk.messagebox.showinfo("hellopython","sdfg") players=[Hand(),Hand(),Hand(),Hand()] pockers=Poke() pockers.generate_poke() pockers.random_poke() pockers.deal(players,13) wj=print_poke(players) importtkinterastk win=tk.Tk() win.title("扑克牌程序") win.geometry('800x600') cv=tk.Canvas(win,bg='red',width=800,height=600) fromPILimportImage,ImageTk imgs=[] (p1,p2,p3,p4)=([],[],[],[]) foriinrange(4): forjinrange(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] forxinrange(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]) #添加重新发牌按钮 fromtkinter.messageboximport* 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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。