Я делаю игру "Пиратская тральщик". Я проверяю окружающие блоки, чтобы определить, какое это число (или цвет на данный момент), но каждый квадрат в левом столбце выбирается как 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))
Здесь я создаю 2d список, который работает как карта доски / сетки:
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()