此代码对电脑要求有点大
import pygame
import random
# 游戏窗口尺寸
WIDTH = 400
HEIGHT = 400
# 定义游戏元素数据
grid_size = 20
grid_num_cols = WIDTH // grid_size
grid_num_rows = HEIGHT // grid_size
num_bombs = 40
num_flags = 0
game_over = False
# 初始化雷区
def init_grid():
global grid, bombs
grid = [[0 for col in range(grid_num_cols)] for row in range(grid_num_rows)]
bombs = []
while len(bombs) < num_bombs:
col = random.randint(0, grid_num_cols - 1)
row = random.randint(0, grid_num_rows - 1)
if (row, col) not in bombs:
bombs.append((row, col))
grid[row][col] = -1
for row in range(grid_num_rows):
for col in range(grid_num_cols):
if grid[row][col] == -1:
continue
count = 0
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
r = row + dr
c = col + dc
if r < 0 or r >= grid_num_rows or c < 0 or c >= grid_num_cols:
continue
if grid[r][c] == -1:
count += 1
grid[row][col] = count
# 开始游戏
def start_game():
global game_over, num_flags
game_over = False
num_flags = 0
init_grid()
# 绘制游戏界面
def draw():
screen.fill((255, 255, 255))
for r in range(grid_num_rows):
for c in range(grid_num_cols):
x = c * grid_size
y = r * grid_size
if grid[r][c] == -1 and not game_over:
pygame.draw.rect(screen, (255, 0, 0), (x, y, grid_size, grid_size))
elif grid[r][c] >= 0:
pygame.draw.rect(screen, (192, 192, 192), (x, y, grid_size, grid_size))
if grid[r][c] > 0:
font = pygame.font.SysFont(None, int(grid_size*0.8))
text = font.render(str(grid[r][c]), True, (0, 0, 255))
text_rect = text.get_rect(center=(x + grid_size/2, y + grid_size/2))
screen.blit(text, text_rect)
if (r, c) in flags:
pygame.draw.rect(screen, (0, 255, 0), (x, y, grid_size, grid_size))
pygame.draw.line(screen, (0, 0, 0), (x+3, y+3), (x+grid_size-3, y+grid_size-3), 3)
pygame.draw.line(screen, (0, 0, 0), (x+grid_size-3, y+3), (x+3, y+grid_size-3), 3)
pygame.display.update()
# 处理鼠标事件
def handle_mouse_event(pos, button):
global game_over, num_flags, flags
if game_over:
return
r = pos[1] // grid_size
c = pos[0] // grid_size
if button == 1: # 左键点击方块,翻开
if (r, c) in flags:
return
if grid[r][c] == -1: # 点到炸弹了,游戏结束
game_over = True
elif grid[r][c] == 0: # 递归翻开周围方块
reveal_grid(r, c)
else:
grid[r][c] = -grid[r][c]
elif button == 3: # 右键点击方块,插旗
if (r, c) in flags:
num_flags -= 1
flags.remove((r, c))
else:
num_flags += 1
flags.add((r, c))
if check_win():
game_over = True
# 递归翻开周围方块
def reveal_grid(row, col):
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
r = row + dr
c = col + dc
if r < 0 or r >= grid_num_rows or c < 0 or c >= grid_num_cols:
continue
if grid[r][c] != 0 and grid[r][c] < 9:
grid[r][c] = -grid[r][c]
elif grid[r][c] == 0:
grid[r][c] = -9
reveal_grid(r, c)
# 检查是否胜利
def check_win():
for r in range(grid_num_rows):
for c in range(grid_num_cols):
if grid[r][c] >= 0 and (r, c) not in flags:
return False
return True
# 初始化Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 创建游戏时钟
clock = pygame.time.Clock()
# 开始游戏
start_game()
# 插旗列表
flags = set()
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
start_game()
elif event.type == pygame.MOUSEBUTTONDOWN:
handle_mouse_event(pygame.mouse.get_pos(), event.button)
# 绘制游戏界面
draw()
# 更新游戏窗口
pygame.display.update()
# 限制游戏帧率
clock.tick(30)