为什么我检查井字游戏赢家的方法不起作用?

2024-04-27 04:02:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我看了一段视频,一个家伙在玩他的游戏,效果很好。我正在用同样的方法寻找赢家,但什么也没发生。我用图像代替字母,但这真的有区别吗。有什么建议吗?你知道吗

from tkinter import*

#Window stuff   
window = Tk()
window.title("Tic Tac Toe")
window.configure(background = "black")
window.geometry("400x400")

#Variables
global clickable
playerXturn = True
ticker = 0 

#Display X or O
def buttonClicked(c) : 
    global playerXturn
    if playerXturn == True :
        buttonList[c]["image"] = picX
        buttonList[c]["state"] = DISABLED
        playerXturn = False
        labelTurn ["text"] = "O's turn"

    elif clickable[c] == "" : 
        buttonList[c]["image"] = picO
        buttonList[c]["state"] = DISABLED
        playerXturn = True
        labelTurn ["text"] = "X's turn"
#Info for user
    global ticker
    ticker += 1
    if ticker == 9 :
        labelNewGame["text"] = "GAME OVER"
        labelNewGame["bg"] = "red" 
        labelTurn["text"] = ""
        labelTurn["bg"] = "black"

#Three in a row
    elif (button1["image"] == picX and button2["image"] == picX and button3["image"] == picX or
          button4["image"] == picX and button5["image"] == picX and button6["image"] == picX or
          button7["image"] == picX and button8["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button4["image"] == picX and button7["image"] == picX or
          button2["image"] == picX and button5["image"] == picX and button8["image"] == picX or
          button3["image"] == picX and button6["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button5["image"] == picX and button9["image"] == picX or
          button3["image"] == picX and button5["image"] == picX and button7["image"] == picX) :
        print ("X wins")

    elif (button1["image"] == picO and button2["image"] == picO and button3["image"] == picO or
          button4["image"] == picO and button5["image"] == picO and button6["image"] == picO or
          button7["image"] == picO and button8["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button4["image"] == picO and button7["image"] == picO or
          button2["image"] == picO and button5["image"] == picO and button8["image"] == picO or
          button3["image"] == picO and button6["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button5["image"] == picO and button9["image"] == picO or
          button3["image"] == picO and button5["image"] == picO and button7["image"] == picO) :
        print ("O wins") 


#Images
picX = PhotoImage (file = "x.gif") 
picO = PhotoImage (file = "o.gif")
picBlank = PhotoImage (file = "sw.gif") 

#Buttons
button1 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(0))     
button1.grid (row = 0, column = 0)
#button1["state"] = DISABLED 
button2 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(1))    
button2.grid (row = 0, column = 1)
#button2["state"] = DISABLED 
button3 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(2))  
button3.grid (row = 0, column = 2)
#button3["state"] = DISABLED 
button4 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(3))   
button4.grid (row = 1, column = 0)
#button4["state"] = DISABLED 
button5 = Button (window, text = "", image = picBlank,  command = lambda: buttonClicked(4)) 
button5.grid (row = 1, column = 1)
#button5["state"] = DISABLED 
button6 = Button (window, text = "", image = picBlank,  command = lambda: buttonClicked(5))  
button6.grid (row= 1, column = 2)
#button6["state"] = DISABLED 
button7 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(6)) 
button7.grid (row = 2, column = 0)
#button7["state"] = DISABLED 
button8 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(7))   
button8.grid (row = 2, column = 1)
#button8["state"] = DISABLED 
button9 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(8))  
button9.grid (row = 2, column = 2)
#button9["state"] = DISABLED 

#Lists
buttonList = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
clickable = ["", "", "", "", "", "", "", "", ""]

#Extra labels and buttons
labelMenu = Label (window, text = "Menu and Info", height = 2, width = 24, bg = "yellow") 
labelMenu.grid (row = 4, column = 4) 
labelTurn = Label (window, text = "X goes first", height = 2, width = 10)  
labelTurn.grid (row = 6, column = 4)
labelNewGame = Label (window, text = "Continue Playing", height = 2, width = 14,)     
labelNewGame.grid (row = 5, column = 4)

window.mainloop() 

Tags: orandtextimagecolumnwindowgridrow
1条回答
网友
1楼 · 发布于 2024-04-27 04:02:09

看来你的问题是

buttonList[c]["image"] = picX

buttonList[c][“image”]采用的真值是字符串“pyimage1”,而picX是tkinter.PhotoImage公司. 有鉴于此,它应该解决你的问题,如果不是比较的东西,如

button1["image"] == picX

你应该这样做

 buttonN["image"] == str(picX)

对picO也一样。你知道吗

相关问题 更多 >