python - 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()
解决方案
推荐阅读
- javascript - 如何计算日期和更改日期格式
- node.js - 将订单与产品关联
- azure-language-understanding - 无法使用“:”在 LUIS 中添加新意图
- oracle-adf - 如何在 Chrome 73.0 中的 af:inputText 中设置边框?
- r - R - 如何从“扩展”矩阵中获取每列的系数〜时间线?
- mysql - 如何在数据库中为电话号码添加 0
- html - 如何编辑 Web 表单的样式
- javascript - 查询完成后呈现我的 ejs 文件
- font-awesome - 我的字体真棒没有出现,我不知道为什么
- c++ - 如何用单链表编写多项式