- 编程
发牌程序
- 2023-12-17 16:11:07 @
from tkinter import * # 从tkinter模块中导入所有内容
import random # 导入random模块
# 定义全局变量n,表示牌的数量为52张
n = 52
# 定义一个函数gen_pocker,用于生成牌堆
def gen_pocker(n):
x = 100 # 初始化循环次数为100
while x > 0: # 当循环次数大于0时,继续循环
x = x - 1 # 每次循环将循环次数减1
p1 = random.randint(0, n - 1) # 生成一个0到n-1之间的随机整数,作为第一个牌的索引
p2 = random.randint(0, n - 1) # 生成一个0到n-1之间的随机整数,作为第二个牌的索引
t = pocker[p1] # 交换两个牌
pocker[p1] = pocker[p2]
pocker[p2] = t # 交换两个牌的值
return pocker # 返回生成的牌堆
# 初始化一个包含52张牌的列表
pocker = [i for i in range(n)]
# 生成牌堆
pocker = gen_pocker(n)
# 打印生成的牌堆
print(pocker)
# 初始化四个玩家的手牌
(player1, player2, player3, player4) = ([], [], [], [])
(p1, p2, p3, p4) = ([], [], [], [])
# 创建一个tkinter的主窗口
root = Tk()
root.geometry("1000x600")
def yellow():
a1.config(bg="yellow");a2.config(bg="yellow");a3.config(bg="yellow");
def red():
a1.config(bg="red");a2.config(bg="red");a3.config(bg="red");
def blue():
a1.config(bg="blue");a2.config(bg="blue");a3.config(bg="blue");
anniu = Frame(root,bg='blue', width=300, height=600)
a1=Button(anniu,text="blue", width=10, height=10,command=blue)
a2=Button(anniu,text="yellow",width=10, height=10, command=yellow)
a3=Button(anniu,text="red",width=10, height=10, command=red)
anniu.pack(side = "left")
a1.pack(side = "left")
a2.pack(side = "left")
a3.pack(side = "left")
# 创建一个画布,用于显示游戏界面
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'))
imgs.insert(52, PhotoImage(file='images\\rear.gif'))
imgs.insert(53, PhotoImage(file='images\\s1.gif'))
imgs.insert(54, PhotoImage(file='images\\s2.gif'))
imgs.insert(55, PhotoImage(file='images\\white.gif'))
# 将牌堆分成四组,每一组包含13张牌
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()
def sss(item):
return (item)%13
p1.sort(key=sss)
p2.sort(key=sss)
p3.sort(key=sss)
p4.sort(key=sss)
# 在游戏画布上显示每个玩家的手牌
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]]
player2.append(cv.create_image((100, 150 + 20 * x), image=img))
img = imgs[p3[x]]
player3.append(cv.create_image((200 + 20 * x, 500), image=img))
img = imgs[p4[x]]
player4.append(cv.create_image((560, 150 + 20 * x), image=img))
# 打印每个玩家的手牌
print("player1:", player1)
print("player2:", player2)
print("player3:", player3)
print("player4:", player4)
# 显示游戏画布
cv.pack(side=RIGHT)
# 开始主循环
root.mainloop()
0 条评论
目前还没有评论...