Как сделать победу в игре?
Делаю игру сапер и не понимаю как сделать победу?
Вот код:
from pygame import *
from queue import Queue
import random
import time as t
font.init()
windowsWidth, windowsHeight = 700, 800
windows = display.set_mode((windowsWidth, windowsHeight))
display.set_caption("Сапёр")
backgroundColor = "white"
ROWS, COLS = 3, 3
MINES = 1
NUM_FONT = font.SysFont('comicsans', 20)
LOST_FONT = font.SysFont("Arial", 100)
TIME_FONT = font.SysFont("comicsans", 50)
numberColors = {1 : "black",
2 : "green",
3 : "red",
4 : "orange",
5 : "yellow",
6 : "purple",
7 : "blue",
8 : "pink"}
rectColors = (200, 200, 200)
clickRectColor = (140, 140, 140)
flagColors = "green"
size = windowsWidth / ROWS
minesColor = "red"
class Main:
def __init__(self, windows, windowsWidth, windowsHeight, backgroundColor, ROWS, COLS, MINES, NUM_FONT, numberColors):
self.windows = windows
self.windowsWidth = windowsWidth
self.windowsHeight = windowsHeight
self.backgroundColor = backgroundColor
self.ROWS = ROWS
self.COLS = COLS
self.MINES = MINES
self.NUM_FONT = NUM_FONT
self.numberColors = numberColors
def get_neighbors(self, row, col, rows, cols):
neighbors = []
if row > 0:
neighbors.append((row - 1, col))
if row < rows - 1:
neighbors.append((row + 1, col))
if col > 0:
neighbors.append((row, col - 1))
if col < cols - 1:
neighbors.append((row, col + 1))
if row > 0 and col > 0:
neighbors.append((row - 1, col - 1))
if row < rows - 1 and col < cols - 1:
neighbors.append((row + 1, col + 1))
if row < rows - 1 and col > 0:
neighbors.append((row + 1, col - 1))
if row > 0 and col < cols - 1:
neighbors.append((row - 1, col + 1))
return neighbors
def create_mine_field(self, rows, cols, mines):
field = [[0 for _ in range(cols)] for _ in range(rows)]
mines_positions = set()
while len(mines_positions) < mines:
row = random.randrange(0, rows)
col = random.randrange(0, cols)
pos = row, col
if pos in mines_positions:
continue
mines_positions.add(pos)
field[row][col] = -1
for mine in mines_positions:
neighbors = self.get_neighbors(*mine, rows, cols)
for r, c in neighbors:
if field[r][c] != -1:
field[r][c] += 1
return field
def getGridPos(self, mousePos):
mouseX, mouseY = mousePos
row = int(mouseY // size)
col = int(mouseX // size)
return row, col
def uncover_from_pos(self, row, col, cover_field, field):
q = Queue()
q.put((row, col))
visited = set()
while not q.empty():
current = q.get()
neighbors = self.get_neighbors(*current, ROWS, COLS)
for r, c in neighbors:
if(r, c) in visited:
continue
value = field[r][c]
if value == 0 and cover_field[r][c] != -2:
q.put((r, c))
if cover_field[r][c] != -2:
cover_field[r][c] = 1
visited.add((r, c))
def draw1(self, windows, field, cover_field, currentTime):
windows.fill(backgroundColor)
timeText = TIME_FONT.render(f"Потраченое время: {round(currentTime)}", 1, "black")
windows.blit(timeText, (10, windowsHeight - timeText.get_height()))
for i, row in enumerate(field):
y = size * i
for j, value in enumerate(row):
x = size * j
is_covered = cover_field[i][j] == 0
is_flag = cover_field[i][j] == -2
is_mines = value == -1
if is_flag:
draw.rect(windows, flagColors, (x, y, size, size))
draw.rect(windows, "black", (x, y, size, size), 2)
continue
if is_covered:
draw.rect(windows, rectColors, (x, y, size, size))
draw.rect(windows, "black", (x, y, size, size), 2)
continue
else:
draw.rect(windows, clickRectColor, (x, y, size, size))
draw.rect(windows, "black", (x, y, size, size), 2)
if is_mines:
draw.circle(windows, minesColor, (x + size / 2, y + size / 2), size / 2 - 4)
if value > 0:
text = NUM_FONT.render(str(value), 1, numberColors[value])
windows.blit(text, (x + (size / 2 - text.get_width() / 2),
y + (size / 2 - text.get_height() / 2)))
display.update()
def drawLost(self, windows, text1, text2):
text1 = LOST_FONT.render(text1, 1, "red")
text2 = LOST_FONT.render(text2, 1, "green")
windows.blit(text1, (windowsWidth / 2 - text1.get_width() / 2,
windowsHeight / 2 - text1.get_height() / 2))
windows.blit(text2, (windowsWidth / 2 - text2.get_width() / 2 + 100,
windowsHeight / 2 - text2.get_height() / 2 + 100))
display.update()
def main(windows):
mainFun = Main(windows, windowsWidth, windowsHeight, backgroundColor, ROWS, COLS, MINES, NUM_FONT, numberColors)
field = mainFun.create_mine_field(ROWS, COLS, MINES)
cover_field = [[0 for _ in range(COLS)] for _ in range(ROWS)]
flags = MINES
clicks = 0
lost = False
startTime = 0
gameLoop = True
while gameLoop:
if startTime > 0:
currentTime = t.time() - startTime
else:
currentTime = 0
for e in event.get():
if e.type == QUIT:
gameLoop = False
if e.type == MOUSEBUTTONDOWN:
row, col = mainFun.getGridPos(mouse.get_pos())
if row >= ROWS or col >= COLS:
continue
mouse_pressed = mouse.get_pressed()
if mouse_pressed[0] and cover_field[row][col] != -2:
cover_field[row][col] = 1
if field[row][col] == -1:
lost = True
if clicks == 0 or field[row][col] == 0:
mainFun.uncover_from_pos(row, col, cover_field, field)
if clicks == 0:
startTime = t.time()
clicks += 1
elif mouse_pressed[2]:
if cover_field[row][col] == -2:
cover_field[row][col] = 0
flags += 1
else:
flags -= 1
cover_field[row][col] = -2
if lost:
mainFun.draw1(windows, field, cover_field, currentTime)
mainFun.drawLost(windows, "Ви проиграли!", "Рестарт игры....")
time.delay(5000)
field = mainFun.create_mine_field(ROWS, COLS, MINES)
cover_field = [[0 for _ in range(COLS)] for _ in range(ROWS)]
flags = MINES
clicks = 0
lost = False
mainFun.draw1(windows, field, cover_field, currentTime)
quit()
main(windows)