基本上,我想要一个事件,如果我单击一个单元格并且该单元格的值显示在一个单元格半径内它周围有零个地雷,它会自动检查是否有更多单元格周围也没有任何地雷,并且在同一回合中展现自己。 (不太熟悉扫雷,但我认为这也是真正的游戏的运作方式)
我正在尝试使用递归,但这是我第一次,我不知道我做得是否正确,并且由于某种原因,它最终根本没有在单元格上显示任何数字来显示附近有多少个地雷。我知道我的 if/for 语句有问题,但我无法准确指出问题所在。
我正在尝试使用递归,但这是我第一次,我不知道我做得是否正确,并且由于某种原因,它最终根本没有在单元格上显示任何数字来显示附近有多少个地雷。我知道我的 if/for 语句有问题,但我无法准确指出问题所在。
这是我的细胞类别:
from tkinter import Button
import random
import settings
class Cell:
all = [] # list of all cells
# x and y are the coordinates of the cell
def __init__(self, x, y, is_mine=False):
self.is_mine = is_mine
self.cell_button_object = None
self.x = x
self.y = y
Cell.all.append(self)
# creates a button object for the cell and binds the left and right click events
def create_button_object(self, location):
btn = Button(location, width = 12, height = 4)
btn.bind('<Button-1>', self.left_click)
btn.bind('<Button-3>', self.right_click)
self.cell_button_object = btn
#
def left_click(self, event):
# if the cell is a mine, show the mine
if self.is_mine:
self.show_mine()
# (Doesnt work but if it did) reveals zero mine value cells around the initailly clicked zero mine value cell
elif self.surrounded_cell_mines == 0:
def zeroCell(self):
for cell in self.surrounded_cells:
if cell.surrounded_cells_mines == 0:
cell.show_cell()
zeroCell(self)
else:
# reveals how many mines around cell
self.show_cell()
# returns the cell object based on the x and y coordinates
def get_cell_by_axis(self,x,y):
for cell in Cell.all:
if cell.x == x and cell.y == y:
return cell
@property
# returns a list of all the cells around the cell in a one cell radius
def surrounded_cells(self):
cells = [
self.get_cell_by_axis(self.x - 1, self.y - 1),
self.get_cell_by_axis(self.x -1, self.y),
self.get_cell_by_axis(self.x -1, self.y +1),
self.get_cell_by_axis(self.x, self.y -1),
self.get_cell_by_axis(self.x +1, self.y -1),
self.get_cell_by_axis(self.x +1,self.y),
self.get_cell_by_axis(self.x+1,self.y+1),
self.get_cell_by_axis(self.x, self.y+1)
]
cells = [cell for cell in cells if cell is not None]
return cells
@property
# returns the number of mines around the cell thorugh conter
def surrounded_cells_mines(self):
counter = 0
for cell in self.surrounded_cells:
if cell.is_mine:
counter +=1
return counter
# shows the number of mines around the cell
def show_cell(self):
self.cell_button_object.configure(text=self.surrounded_cells_mines)
def show_mine(self):
# change the color of the button to red if mine
self.cell_button_object.configure(bg = "red")
# right click event
def right_click(self, event):
print('right click')
# randomizes the mines in the game
@staticmethod
def randomize_mines():
picked_cells = random.sample(Cell.all, settings.mines_count)
for picked_cell in picked_cells:
picked_cell.is_mine = True
def __repr__(self):
return f"Cell({self.x}, {self.y})"
这样做:
def __init__(self, x, y, is_mine=False):
self.is_mine = is_mine
self.cell_button_object = None
self.x = x
self.y = y
self.revealed = False
Cell.all.append(self)
还有这个:
def left_click(self, event):
if self.is_mine:
self.show_mine()
else:
self.reveal_zero_mines_around() # this method will handle the recursive reveal
def reveal_zero_mines_around(self):
if self.revealed: # stop if the cell is already revealed
return
self.revealed = True # mark this cell as revealed
self.show_cell() # show the number of mines around it
if self.surrounded_cells_mines == 0:
for cell in self.surrounded_cells:
cell.reveal_zero_mines_around() # recursion