I'm making a pygame minesweeper game. I'm checking the surrounding blocks to determine what number it is (or color for now), but every square in the left column gets chosen as a 0 (gray color) even though it might be a 1, 2 or 3...
Here I am drawing the grid
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))
Here I'm creating a 2d list that works as a map of the board/grid:
for i in range(0, len(self.squares), self.column):
self.squaregrid.append(self.squares[i:i + self.column])
Here I'm randomly selecting the positions of the mines.
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)))
Here I'm separating the non-mines from the mines
def nums(self):
self.mine()
self.numbers = self.squares
for x in self.mines:
self.numbers.remove(x
This is the most important part where I'm checking the surrounding squares.
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)
Here I'm drawing a different colored squared depending on the surrounding mines. The problem is that every square in the left is being selected a gray square (which is equivalent to a 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)))
This is my entire code
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()
Aucun commentaire:
Enregistrer un commentaire