好吧,我正在为学校做一个连接4的项目,重点是使用顶部的按钮设置标签的颜色,颜色取决于转弯。我很难过,除非我将n值设置为[n + 35],否则我无法在底部设置颜色。到目前为止,这是我的代码。此外,我仍然没有到位的胜利条件,我会稍后再做。现在我正在寻找的是如何正确设置标签的帮助。
提前感谢您的帮助。 (对不起,到目前为止是一个编码菜鸟。)
from Tkinter import *
from functools import partial
top = Tk()
top.title("Juan's Zany Super Crazy Connect Four!")
turn=1
def click(n):
global turn
if turn==1:
board[35+n].config(state=DISABLED, bg='blue')
turn=turn+1
elif turn==2:
board[35+n].config(state=DISABLED, bg='red')
turn=turn-1
top = Tk()
r=0
count=0
col=0
buttonList = list()
for i in range(7):
buttonList.append(Button(text=str(i), font='Helvetica 48', command=partial(click, i)))
buttonList[-1].grid(row=r,column=col, sticky='NESW')
count+=1
col+=1
r=1
col=0
count=0
board = list()
for i in range(42):
board.append(Label(text=(i), font='Helvetica 15', bg='grey80'))
board[-1].grid(row=r,column=col, sticky='NESW', padx=2, pady=2)
count+=1
col+=1
if count==7:
r=r+1
count=0
col=0
top.mainloop()
更新(2016年4月5日):
from Tkinter import *
from functools import partial
import time
#time.sleep()
top = Tk()
top.title("Juan's Zany Super Crazy Connect Four!")
turn=1
nextS=[35,36,37,38,39,40,41]
def buttonStuff(z):
global turn
if turn==1:
board[nextS[z]].config(bg='blue')
nextS[z]=nextS[z]-7
turn=turn+1
if nextS[z]<0:
buttonList[z].config(state=DISABLED)
elif turn==2:
board[nextS[z]].config(bg='red')
nextS[z]=nextS[z]-7
turn=turn-1
if nextS[z]<0:
buttonList[z].config(state=DISABLED)
def checkWin():
for h in range (0,4):
if board[35+h].cget('bg')==board[36+h].cget('bg')==board[37+h].cget('bg')==board[38+h].cget('bg')!=bg=='yellow':
for q in range (0,4):
board[35+h].config(bg='green')
board[36+h].config(bg='green')
board[37+h].config(bg='green')
board[38+h].config(bg='green')
time.sleep(0.5)
if turn==1:
board[35+h].config(bg='blue')
board[36+h].config(bg='blue')
board[37+h].config(bg='blue')
board[38+h].config(bg='blue')
if turn==2:
board[35+h].config(bg='red')
board[36+h].config(bg='red')
board[37+h].config(bg='red')
board[38+h].config(bg='red')
top = Tk()
r=0
count=0
col=0
buttonList = list()
for z in range(7):
buttonList.append(Button(text=str(z), font='times 48', command=partial(buttonStuff, z)))
buttonList[-1].grid(row=r,column=col, sticky='NESW')
count+=1
col+=1
r=1
col=0
count=0
board = list()
for z in range(42):
board.append(Label(text='', font='Helvetica 15', bg='grey80'))
board[-1].grid(row=r,column=col, sticky='NESW', padx=2, pady=2, ipadx=2, ipady=25)
count+=1
col+=1
if count==7:
r=r+1
count=0
col=0
top.mainloop()
我想要完成的是现在只需要一行(底行),我希望它根据转弯/连续'捕获'那4个的玩家闪蓝/红色。只是挣扎了一下,所以如果你们其中一个人可以快速看一下。另外,请保持基本状态,因为我想知道我做错了什么。
提前谢谢。
答案 0 :(得分:1)
更改click
功能,如下所示:
def click(n):
global turn
offset = 0
while board[35 + n - offset]['bg'] in {'blue', 'red'}:
offset += 7
if turn==1:
board[35+n-offset].config(state=DISABLED, bg='blue')
turn=turn+1
elif turn==2:
board[35+n-offset].config(state=DISABLED, bg='red')
turn=turn-1
如果您需要更多帮助,请随时提问!