python - 尝试使用 Tkinter 在 python 中创建游戏扫雷,但在查找图块周围的邻居数量时遇到问题
问题描述
这些数字显示了瓷砖有多少邻居并且应该显示:每个角落有 3 个邻居,所有四个边缘上都有 5 个,其他所有地方都是 8。但由于某种原因,右边缘显示为 6 而不是 5。
这是我刚刚完成的图像:
from tkinter import *
from random import choice
root = Tk()
root.geometry("544x544")
# tile class
class Tile:
def __init__(self, x, y, state=0):
self.x = x
self.y = y
self.state = state
self.button = Button(root,command=self.button_command, image=empty_block, height=28, width=28)
self.listOfNeighbors = []
self.neighbors = 0
def button_command(self):
print(self.x, self.y)
def findNeighbors(self):
self.listOfNeighbors.append(board[self.y-1][self.x])
self.listOfNeighbors.append(board[self.y][self.x-1])
self.listOfNeighbors.append(board[self.y-1][self.x-1])
try: self.listOfNeighbors.append(board[self.y+1][self.x])
except: pass
try: self.listOfNeighbors.append(board[self.y+1][self.x-1])
except: pass
try: self.listOfNeighbors.append(board[self.y-1][self.x+1])
except: pass
try: self.listOfNeighbors.append(board[self.y+1][self.x+1])
except: pass
try: self.listOfNeighbors.append(board[self.y][self.x+1])
except: pass
self.sortNeighbors()
def sortNeighbors(self):
for i in self.listOfNeighbors:
if self.y == 0:
if i.y == 15: self.listOfNeighbors.remove(i);print(self.x, self.y," ", i.x, i.y)
elif self.x == 0:
if i.x == 15: self.listOfNeighbors.remove(i);print(self.x, self.y," ", i.x, i.y)
self.neighbors = len(self.listOfNeighbors)
self.button.config(image=neighbors_images[self.neighbors])
#variable
empty_block = PhotoImage(file="images/empty-block.png")
bomb_unclicked = PhotoImage(file="images/unclicked-bomb.png")
bomb_clicked = PhotoImage(file="images/bomb-at-clicked-block.png")
neighbors_images = [
PhotoImage(file="images/0.png"),
PhotoImage(file="images/1.png"),
PhotoImage(file="images/2.png"),
PhotoImage(file="images/3.png"),
PhotoImage(file="images/4.png"),
PhotoImage(file="images/5.png"),
PhotoImage(file="images/6.png"),
PhotoImage(file="images/7.png"),
PhotoImage(file="images/8.png"),
]
board = []
for y in range(16):
temp = []
for x in range(16):
temp.append(Tile(x, y))
temp[-1].button.grid(row=y, column=x)
board.append(temp)
for i in range(40):
choice(choice(board)).state = 1
for y in board:
for x in y:
x.findNeighbors()
root.mainloop()
据我所知,问题来自line 42
于我试图从邻居列表中删除任何在棋盘之外的 Tile。
解决方案
你让自己变得更难了。我建议明确并避免添加然后删除不必要的邻居:
def findNeighbors(self):
NEIGHBOURS = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1)
]
for dx, dy in NEIGHBOURS:
if 0 <= self.x + dx < len(board[0]) and 0 <= self.y + dy < len(board):
self.listOfNeighbors.append(board[self.y + dy][self.x + dx])
self.sortNeighbors()
def sortNeighbors(self):
self.neighbors = len(self.listOfNeighbors)
self.button.config(image=neighbors_images[self.neighbors])
推荐阅读
- z3 - 如何在 z3py 中连接正则表达式?
- discord - awaitReactions 遇到问题
- python - 使用 Functions Framework for Python 时如何在 Google Cloud Buildpacks 中设置运行时版本?
- algorithm - 二进制搜索以查找布尔数组中的最高真实索引
- prisma - 生成迁移 README 时,Prisma 的名字来自哪里?
- swift - 快速为牛津词典 API 构建数据模型的问题
- google-cloud-platform - 在特定域上执行 cloud_api web_detection 搜索
- python - 计算文档中的标记
- reactjs - React - 根据类外的变量更新类中的值
- python - 使用python从excel表中提取超链接URL