python實(shí)現(xiàn)撲克牌交互式界面發(fā)牌程序
本文實(shí)例為大家分享了python實(shí)現(xiàn)撲克牌交互式界面發(fā)牌程序的具體代碼,供大家參考,具體內(nèi)容如下
注:圖片自行在網(wǎng)上下載、替換即可
#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]) #添加重新發(fā)牌按鈕 from tkinter.messagebox import * bt1=tk.Button(win,text='重新發(fā)牌',width=60,height=40,command=restart) bt1.place(x=290,y=380,width=60,height=40) cv.pack() win.mainloop()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python list使用示例 list中找連續(xù)的數(shù)字
這篇文章主要介紹了list中找連續(xù)的數(shù)字的示例,大家參考使用吧2014-01-01Django之無名分組和有名分組的實(shí)現(xiàn)
這篇文章主要介紹了Django之無名分組和有名分組,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04Python黑魔法Descriptor描述符的實(shí)例解析
與迭代器和裝飾器等一樣,描述符也是Python編程中的一項(xiàng)高級技巧,這里我們就來講解Python黑魔法Descriptor描述符的實(shí)例解析:2016-06-06Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說明
這篇文章主要介紹了Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加)
今天小編就為大家分享一篇Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python數(shù)據(jù)庫的連接實(shí)現(xiàn)方法與注意事項(xiàng)
這篇文章主要介紹了Python數(shù)據(jù)庫的連接實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2016-02-02