尝试使用 Tkinter 在 python 中创建扫雷游戏,但在查找瓷砖周围的邻居数量时遇到问题
Trying to create the game minesweeper in python using Tkinter but have a problem finding the number of neighbors around a tile
这些数字显示了该图块有多少个邻居,应该显示多少个邻居:每个角 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])
这些数字显示了该图块有多少个邻居,应该显示多少个邻居:每个角 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])