扫雷
2026-01-11 11:23:06
发布于:浙江
import pygame
import random
# Initialize Pygame
pygame.init()
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (180, 180, 180)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Constants
CELL_SIZE = 30
MINE_COLOR = RED
FLAG_COLOR = BLUE
TEXT_COLOR = BLACK
FONT_SIZE = 20
class Minesweeper:
def __init__(self, rows, cols, mines):
self.rows = rows
self.cols = cols
self.mines = mines
self.board = [[0 for _ in range(cols)] for _ in range(rows)]
self.revealed = [[False for _ in range(cols)] for _ in range(rows)]
self.flags = set()
self.game_over = False
self.create_board()
self.font = pygame.font.Font(None, FONT_SIZE)
def create_board(self):
mine_positions = set()
while len(mine_positions) < self.mines:
x, y = random.randint(0, self.rows - 1), random.randint(0, self.cols - 1)
if (x, y) not in mine_positions:
mine_positions.add((x, y))
self.board[x][y] = -1
for x, y in mine_positions:
for i in range(max(0, x-1), min(self.rows, x+2)):
for j in range(max(0, y-1), min(self.cols, y+2)):
if self.board[i][j] != -1:
self.board[i][j] += 1
def draw_cell(self, screen, row, col):
rect = pygame.Rect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, GRAY, rect, 1)
if self.revealed[row][col]:
if self.board[row][col] == -1:
pygame.draw.circle(screen, MINE_COLOR, rect.center, CELL_SIZE // 3)
elif self.board[row][col] > 0:
text = self.font.render(str(self.board[row][col]), True, TEXT_COLOR)
text_rect = text.get_rect(center=rect.center)
screen.blit(text, text_rect)
elif (row, col) in self.flags:
pygame.draw.polygon(screen, FLAG_COLOR, [(rect.left + 5, rect.top + 5),
(rect.right - 5, rect.top + 5),
(rect.centerx, rect.bottom - 5)])
def reveal_cells(self, row, col):
if self.revealed[row][col]:
return
self.revealed[row][col] = True
if self.board[row][col] > 0:
return
for i in range(max(0, row-1), min(self.rows, row+2)):
for j in range(max(0, col-1), min(self.cols, col+2)):
self.reveal_cells(i, j)
def check_win(self):
safe_cells = self.rows * self.cols - self.mines
revealed_cells = sum(row.count(True) for row in self.revealed)
return revealed_cells == safe_cells
def run(self):
width = self.cols * CELL_SIZE
height = self.rows * CELL_SIZE
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Minesweeper")
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
col = pos[0] // CELL_SIZE
row = pos[1] // CELL_SIZE
if event.button == 1: # Left click
if (row, col) in self.flags:
continue
if self.board[row][col] == -1:
self.game_over = True
print("Game Over! You hit a mine!")
else:
self.reveal_cells(row, col)
if self.check_win():
self.game_over = True
print("Congratulations! You won!")
elif event.button == 3: # Right click
if self.revealed[row][col]:
continue
if (row, col) in self.flags:
self.flags.remove((row, col))
else:
self.flags.add((row, col))
screen.fill(WHITE)
for row in range(self.rows):
for col in range(self.cols):
self.draw_cell(screen, row, col)
if self.game_over:
for row in range(self.rows):
for col in range(self.cols):
if self.board[row][col] == -1:
self.revealed[row][col] = True
pygame.display.flip()
clock.tick(60)
pygame.quit()
def main():
difficulty = input("Choose difficulty level (easy/medium/hard): ").strip().lower()
if difficulty == "easy":
rows, cols, mines = 9, 9, 10
elif difficulty == "medium":
rows, cols, mines = 16, 16, 40
elif difficulty == "hard":
rows, cols, mines = 30, 16, 99
else:
print("Invalid difficulty level. Defaulting to easy.")
rows, cols, mines = 9, 9, 10
game = Minesweeper(rows, cols, mines)
game.run()
if __name__ == "__main__":
main()
这里空空如也











有帮助,赞一个