首页 > 解决方案 > Tkinter 颜色瓷砖匹配游戏按钮错误

问题描述

我正在 python tkinter 中创建一个颜色匹配游戏。我最终将拥有它,以便它可以具有不同的尺寸,但目前正在努力进行匹配。我目前拥有它,因此颜色不会被打乱,因此更容易测试。但是,我目前的问题是选择两个与这些瓷砖锁定且无法再次使用的瓷砖时。他们没有提供有关单击它们的任何信息,也没有错误。

from tkinter import *
import random
root = Tk()
root.geometry("500x500")

#video with simple version of code
#https://www.youtube.com/watch?v=tlMPVGSEEDw


#size of board
size = [4]
length = random.choice(size)
game = True
#all the color options
colors =['snow','snow', 'peach puff', 'peach puff', 'azure', 'azure', 'navy', 'navy',
    'cyan', 'cyan', 'lawn green', 'lawn green', 'yellow', 'yellow', 'dark goldenrod', 'dark goldenrod',
    'salmon', 'salmon', 'purple', 'purple', 'gold', 'gold', 'red', 'red',
    'hot pink', 'hot pink', 'dark slate gray', 'dark slate gray', 'rosy brown', 'rosy brown',
     'aquamarine', 'aquamarine',]

count = 0
answer_list = []
answer_key = []
newcolors = colors[0:16]
#random.shuffle(newcolors)

#Check to see if match
def update(x,y,length):
    global count, answer_list, answer_key
    #collect the two tiles
    if board[x][y]["bg"] == "white" and count < 2:
        number = x * length + y
        print(number)
        board[x][y]["bg"] = newcolors[number]
        answer_list.append(newcolors[number])
        answer_key.append(number)
        print(answer_list)
        
        
        #Check to see if match
    if len(answer_list) == 2:
        if answer_list[0] == answer_list[1]:
            print("match")
            board[x][y]["state"] = DISABLED
            y = int(answer_key[0] % length)
            x = int((answer_key[0] - y) / length)            
            board[x][y]["state"] = DISABLED
            count = 0
            answer_list = []
            answer_key = []
        else:
            print("Those do not match")
            count = 0
            answer_list = []
            
            board[x][y]["bg"] = "White"
            y = int(answer_key[0] % length)
            x = int((answer_key[0] - y) / length)            
            board[x][y]["bg"] = "White"
            answer_key = []
    

#y = number % length
#x = (number - y)/length
board = [[Button(root,bg = "white", text = " ",height=3, width=4, command = (lambda x = x, y = y: update(x,y,length))) for y in range(length)] for x in range(length)]

for x in range(length):
    for y in range(length):
        board[x][y].grid(row=x,column=y)



root.mainloop()

标签: pythontkinter

解决方案


推荐阅读