尝试使用tkinter在python中创建游戏扫雷器,但在查找瓷砖周围的邻居数量时遇到问题

fae0ux8s  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(213)
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()

数字显示了平铺有多少个邻居,并且应该显示:每个角上有3个邻居,所有四条边上有5个邻居,其他所有8个邻居。但是出于某种原因,右边缘显示的是6而不是5。
这是我刚刚写的一个图像:图像
据我所知,问题来自于witch中的第42行,我试图从邻居列表中删除任何位于板外的瓷砖。

f87krz0w

f87krz0w1#

你让自己更难受了。我建议明确,避免添加和删除不必要的邻居:

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])

相关问题