|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
取子游戏,玩家与电脑轮流取子,从右侧取子,取得最左侧子为赢家。
两个标签,轮流显示取子方、取子数。设置电脑思考时间2-5秒,
最后显示赢家,8秒后关闭程序。from tkinter import *
import random
root=Tk()
root.title("尼姆棋")
cv=Canvas(root,width=640,height=480,bg='#008B8B')
rect1=0
Num_chess=18
rects=[]
xx=0
def LoadChess():
for i in range(Num_chess):
rect1 = cv.create_rectangle(30+32*i, 220, 58+32*i, 248, fill='#FF7F50')
rects.append(rect1)
label1=Label(root, text=str(i+1), font=("Helvetica", 8), bg='white')
label1.place(x=35+32*i,y=180)
def play(event):
global rect1,Num_chess,label2
xx = round((event.x - 40) / 32) +1
if xx<0:
return None
elif xx> Num_chess:
return None
else:
ss=Num_chess-xx+1
for rect1 in rects:
if rect1 >= xx:
cv.delete(rect1)
for i in range(ss):
rects.pop()
label2['text']=f'玩家取子数:{ss}'
Num_chess=xx-1
if Num_chess <= 0:
label1['text'] = '玩家胜利!'
root.after(8000, root.destroy())
label1['text'] = '电脑思考中...'
wait_time_ms = random.randint(2000, 5000)
root.after(wait_time_ms, computer_move())
def computer_move():
global Num_chess, xx
k = random.randint(1, 3)
for i in range(k):
if Num_chess> 0:
cv.delete(rects[-1])
rects.pop()
Num_chess =Num_chess - 1
label2['text'] = f'电脑取子数:{k}'
if Num_chess<= 0:
label1['text'] = '电脑胜利!'
root.after(8000, root.destroy())
label1['text'] = '该你了'
LoadChess()
cv.bind('<Button-1>',play)
cv.pack()
label1=Label(root,fg='red',bg='white',text='该你了', font=("华文行楷", 20))
label1.place(x=200,y=400)
label2=Label(root,fg='blue',bg='white',text='取子数', font=("kaiti", 20))
label2.place(x=200,y=50)
root.mainloop()
当然可以。我们可以为表示取子方和取子数的标签分别创建两个`Label`控件,并在游戏进行中动态更新它们的内容。
下面是修正后的代码,它包含两个标签,一个用于显示取子方,一个用于显示取子数:
from tkinter import *
import random
import time
root = Tk()
root.title("尼姆棋")
cv = Canvas(root, width=640, height=480, bg='#008B8B')
cv.pack()
# 初始化一些全局变量和控件
rects = []
Num_chess = 18
label_player = Label(root, text="当前取子方:玩家", font=("Helvetica", 14), bg='white')
label_player.pack(pady=10)
label_count = Label(root, text="取子数:0", font=("Helvetica", 14), bg='white')
label_count.pack(pady=10)
def LoadChess():
for i in range(Num_chess):
rect1 = cv.create_rectangle(30 + 32 * i, 220, 58 + 32 * i, 248, fill='#FF7F50')
rects.append(rect1)
label1 = Label(root, text=str(i + 1), font=("Helvetica", 8), bg='white')
label1.place(x=35 + 32 * i, y=180)
def play(event):
global Num_chess, rects
xx = round((event.x - 40) / 32) + 1
if xx > Num_chess:
return
# 玩家取子
ss = Num_chess - xx + 1
for i in range(ss):
cv.delete(rects.pop())
label_player.config(text="当前取子方:玩家")
label_count.config(text=f"取子数:{ss}")
Num_chess -= ss
# 检查是否游戏结束
if Num_chess == 0:
label_player.config(text="玩家获胜!")
root.after(8000, root.quit) # 8秒后关闭程序
return
# 电脑取子
root.after(random.randint(2000, 5000), computer_turn)
def computer_turn():
global Num_chess, rects
k = random.randint(1, min(3, Num_chess)) # 假设电脑每次最多取1到3个
for i in range(k):
cv.delete(rects.pop())
label_player.config(text="当前取子方:电脑")
label_count.config(text=f"取子数:{k}")
Num_chess -= k
# 检查是否游戏结束
if Num_chess == 0:
label_player.config(text="电脑获胜!")
root.after(8000, root.quit) # 8秒后关闭程序
return
cv.bind("<Button-1>", play)
LoadChess()
root.mainloop()
希望这能帮助你完成游戏需求!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|