我在做一个扫雷游戏。我正在检查周围的块以确定它是什么数字(或者现在是什么颜色),但是左列中的每个正方形都被选为0(灰色),即使它可能是1、2或3…
我在这里画网格
def grid(self):
for y in range(self.row):
for x in range(self.column):
pg.draw.line(win, (230, 230, 230), (x * width/self.column, 0), (x * width/self.column, height))
pg.draw.line(win, (230, 230, 230), (0, y * height/self.row), (width, y * height/self.row))
self.squares.append((x * width/self.column, y * height/self.row))
pg.draw.line(win, (230, 230, 230), (0, height), (width, height))
pg.draw.line(win, (230, 230, 230), (width, 0), (width, height))
在这里,我将创建一个二维列表,用作板/网格的地图:
for i in range(0, len(self.squares), self.column):
self.squaregrid.append(self.squares[i:i + self.column])
这里我随机选择地雷的位置。
def mine(self):
for i in range(self.mine_count):
index = rnd.randint(0, 399)
if self.squares[index] not in self.mines: # this causes the number of mines not to match up perfectly with the selected number, but it is necessary
self.mines.append(self.squares[index])
for pos in self.mines:
pg.draw.rect(win, (255, 0, 0), (pos, (20, 20)))
我把非地雷和地雷分开
def nums(self):
self.mine()
self.numbers = self.squares
for x in self.mines:
self.numbers.remove(x
这是我检查周围广场最重要的部分。
for number in self.numbers:
self.surrounding = []
for i, row in enumerate(self.squaregrid):
if number in row:
index = self.squaregrid[i].index(number)
if i >= 1:
for x in self.squaregrid[i - 1][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
for x in self.squaregrid[i][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
if i < 19:
for x in self.squaregrid[i + 1][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
这里我画了一个不同颜色的正方形,这取决于周围的地雷问题是左边的每一个正方形都被选为一个灰色正方形(相当于0)
if len(self.surrounding) == 0:
pg.draw.rect(win, (200, 200, 200), (number, (15, 15)))
if len(self.surrounding) == 1:
pg.draw.rect(win, (30, 30, 255), (number, (15, 15)))
if len(self.surrounding) == 2:
pg.draw.rect(win, (0, 153, 0), (number, (15, 15)))
if len(self.surrounding) == 3:
pg.draw.rect(win, (255, 0, 0), (number, (15, 15)))
if len(self.surrounding) == 4:
pg.draw.rect(win, (0, 0, 120), (number, (15, 15)))
if len(self.surrounding) == 5:
pg.draw.rect(win, (160, 0, 0), (number, (15, 15)))
if len(self.surrounding) == 6:
pg.draw.rect(win, (128, 169, 139), (number, (15, 15)))
if len(self.surrounding) == 7:
pg.draw.rect(win, (100, 0, 0), (number, (15, 15)))
这是我的全部密码
import pygame as pg
import random as rnd
pg.init()
height = 600
width = 600
win = pg.display.set_mode((width + 1, height + 1))
class Board:
def __init__(self):
self.row = 20
self.column = 20
self.mine_count = 65
self.mines = []
self.squares = []
self.squaregrid = []
self.numbers = []
self.surrounding = []
def grid(self):
for y in range(self.row):
for x in range(self.column):
pg.draw.line(win, (230, 230, 230), (x * width/self.column, 0), (x * width/self.column, height))
pg.draw.line(win, (230, 230, 230), (0, y * height/self.row), (width, y * height/self.row))
self.squares.append((x * width/self.column, y * height/self.row))
self.numbers = self.squares
pg.draw.line(win, (230, 230, 230), (0, height), (width, height))
pg.draw.line(win, (230, 230, 230), (width, 0), (width, height))
for i in range(0, len(self.squares), self.column):
self.squaregrid.append(self.squares[i:i + self.column])
def mine(self):
for i in range(self.mine_count):
index = rnd.randint(0, 399)
if self.squares[index] not in self.mines:
self.mines.append(self.squares[index])
for pos in self.mines:
pg.draw.rect(win, (255, 128, 0), (pos, (20, 20)))
def nums(self):
self.mine()
for x in self.mines:
self.numbers.remove(x)
for number in self.numbers:
self.surrounding = []
for i, row in enumerate(self.squaregrid):
if number in row:
index = self.squaregrid[i].index(number)
if i >= 1:
for x in self.squaregrid[i - 1][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
for x in self.squaregrid[i][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
if i < 19:
for x in self.squaregrid[i + 1][index - 1:index + 2]:
if x in self.mines:
self.surrounding.append(1)
if len(self.surrounding) == 0:
pg.draw.rect(win, (200, 200, 200), (number, (15, 15)))
if len(self.surrounding) == 1:
pg.draw.rect(win, (30, 30, 255), (number, (15, 15)))
if len(self.surrounding) == 2:
pg.draw.rect(win, (0, 153, 0), (number, (15, 15)))
if len(self.surrounding) == 3:
pg.draw.rect(win, (255, 0, 0), (number, (15, 15)))
if len(self.surrounding) == 4:
pg.draw.rect(win, (0, 0, 120), (number, (15, 15)))
if len(self.surrounding) == 5:
pg.draw.rect(win, (160, 0, 0), (number, (15, 15)))
if len(self.surrounding) == 6:
pg.draw.rect(win, (128, 169, 139), (number, (15, 15)))
if len(self.surrounding) == 7:
pg.draw.rect(win, (100, 0, 0), (number, (15, 15)))
def main(self):
self.grid()
self.nums()
board = Board()
class App:
def __init__(self):
self.run = True
self.pressed = 0
def draw_grid(self):
win.fill((30, 30, 30))
board.main()
pg.display.flip()
def draw_game(self):
win.fill((70, 70, 70))
pg.display.flip()
def main(self):
self.draw_grid()
while self.run:
for event in pg.event.get():
if event.type == pg.QUIT:
self.run = False
if self.pressed > 0:
self.draw_game()
app = App()
app.main()
最佳答案
问题是,如果index
等于0,那么index-1
就是-1。
这会导致在尝试访问self.squaregrid[i][index-1:index+2]
时出现意外行为,因为这与self.squaregrid[i][-1:2]
相同。
将下索引限制为0,以解决问题。例如。:
index = self.squaregrid[i].index(number)
index_min = max(0,index - 1)
if i >= 1:
for x in self.squaregrid[i - 1][index_min:index + 2]:
if x in self.mines:
self.surrounding.append(1)
for x in self.squaregrid[i][index_min:index + 2]:
if x in self.mines:
self.surrounding.append(1)
if i < 19:
for x in self.squaregrid[i + 1][index_min:index + 2]:
if x in self.mines:
self.surrounding.append(1)
但请注意,这可以简化为:
index = self.squaregrid[i].index(number)
for r in self.squaregrid[max(0,i-1):min(self.row,i+2)]:
for x in r[max(0,index-1):min(self.column,index+2)]:
if x in self.mines:
self.surrounding.append(1)
关于python - 检查周围的地雷minesweeper pygame?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58574198/