Tkinter圖形繪製——圖形版發牌程序

程序功能介紹canvas

機隨機將52張牌(不含大小王)發給四位牌手,在屏幕上顯示每位牌手的牌,Canvas繪製Tkinter模塊圖形創建簡單GUI(圖形用戶界面)遊戲界面。app

Canvas圖形繪製技術dom

Canvas爲Tkinter提供了繪圖功能,其提供的圖形繪製函數可繪製線形、圓形、橢圓、多邊形、圖片等。函數

採用create bitmap()方法能夠繪製圖形、圖像oop

參考代碼:spa

from tkinter import *
import random
n=52
def gen_pocker(n):
    x=100
    while(x>0):
        x=x-1
        p1=random.randint(0,n-1)
        p2=random.randint(0,n-1)
        t=pocker[p1]
        pocker[p1]=pocker[p2]
        pocker[p2]=t
    return pocker
pocker=[i for i in range(n)]
pocker=gen_pocker(n)
print(pocker)

(player1,player2,player3,player4)=([],[],[],[])
(p1,p2,p3,p4)=([],[],[],[])
root=Tk()
#建立一canvas,設置其背景顏色爲白色
cv=Canvas(root,bg='white',width=700,height=600)
imgs=[]
for i in range(1,5):
    for j in range(1,14):
        imgs.insert((i-1)*13+(j-1),PhotoImage(file='images\\'+str(i)+'-'+str(j)+'.gif'))
for x in range(13):
    m=x*4
    p1.append(pocker[m])
    p2.append(pocker[m+1])
    p3.append(pocker[m + 2])
    p4.append(pocker[m + 3])
p1.sort()
p2.sort()
p3.sort()
p4.sort()
for x in range(0,13):
    img=imgs[p1[x]]
    player1.append(cv.create_image((200+20*x,80),image=img))
    img = imgs[p2[x]]
    player1.append(cv.create_image((100,150+20*x), image=img))
    img = imgs[p3[x]]
    player1.append(cv.create_image((200 + 20 * x, 500), image=img))
    img = imgs[p4[x]]
    player1.append(cv.create_image((560,150+20*x), image=img))
print("player1",player1)
print("player2",player2)
print("player3",player3)
print("player4",player4)
cv.pack()
root.mainloop()

運行結果:blog

相關文章
相關標籤/搜索