用Python代码写一个扫雷游戏要求使用pygame
时间: 2023-06-27 16:07:06 浏览: 320
好的,以下是一个基本的扫雷游戏的Python代码,使用pygame库实现:
```python
import pygame
import random
# 游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 网格大小
GRID_SIZE = 20
# 地雷数量
MINE_COUNT = 20
# 颜色定义
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 初始化游戏
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('扫雷')
# 加载图标
icon = pygame.image.load('mine.png')
pygame.display.set_icon(icon)
# 创建字体
font = pygame.font.Font(None, 30)
# 加载图片
mine_img = pygame.image.load('mine.png')
flag_img = pygame.image.load('flag.png')
# 定义方块类
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
self.mine = False
self.revealed = False
self.flagged = False
self.adjacent_mines = 0
# 绘制方块
def draw(self):
rect = pygame.Rect(self.x * GRID_SIZE, self.y * GRID_SIZE, GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, GRAY, rect)
pygame.draw.rect(screen, BLACK, rect, 1)
if self.revealed:
if self.mine:
screen.blit(mine_img, rect)
else:
text = font.render(str(self.adjacent_mines), True, BLACK)
text_rect = text.get_rect()
text_rect.center = rect.center
screen.blit(text, text_rect)
elif self.flagged:
screen.blit(flag_img, rect)
# 揭开方块
def reveal(self):
if not self.flagged:
self.revealed = True
if self.mine:
return False
elif self.adjacent_mines == 0:
for x_offset in range(-1, 2):
for y_offset in range(-1, 2):
x = self.x + x_offset
y = self.y + y_offset
if 0 <= x < WINDOW_WIDTH // GRID_SIZE and 0 <= y < WINDOW_HEIGHT // GRID_SIZE:
block = blocks[x][y]
if not block.revealed:
block.reveal()
return True
# 初始化方块列表
blocks = [[Block(x, y) for y in range(WINDOW_HEIGHT // GRID_SIZE)] for x in range(WINDOW_WIDTH // GRID_SIZE)]
# 放置地雷
mines = []
for i in range(MINE_COUNT):
while True:
x = random.randrange(WINDOW_WIDTH // GRID_SIZE)
y = random.randrange(WINDOW_HEIGHT // GRID_SIZE)
if not blocks[x][y].mine:
blocks[x][y].mine = True
mines.append(blocks[x][y])
break
# 计算相邻地雷数量
for x in range(WINDOW_WIDTH // GRID_SIZE):
for y in range(WINDOW_HEIGHT // GRID_SIZE):
block = blocks[x][y]
for x_offset in range(-1, 2):
for y_offset in range(-1, 2):
if x_offset == 0 and y_offset == 0:
continue
x_adj = x + x_offset
y_adj = y + y_offset
if 0 <= x_adj < WINDOW_WIDTH // GRID_SIZE and 0 <= y_adj < WINDOW_HEIGHT // GRID_SIZE:
if blocks[x_adj][y_adj].mine:
block.adjacent_mines += 1
# 游戏循环
game_over = False
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x = event.pos[0] // GRID_SIZE
y = event.pos[1] // GRID_SIZE
block = blocks[x][y]
if not block.flagged:
if not block.reveal():
game_over = True
for mine in mines:
mine.revealed = True
elif event.button == 3:
x = event.pos[0] // GRID_SIZE
y = event.pos[1] // GRID_SIZE
block = blocks[x][y]
block.flagged = not block.flagged
# 绘制界面
screen.fill(WHITE)
for x in range(WINDOW_WIDTH // GRID_SIZE):
for y in range(WINDOW_HEIGHT // GRID_SIZE):
blocks[x][y].draw()
pygame.display.flip()
# 退出游戏
pygame.quit()
```
在运行游戏之前,需要准备两张图片:mine.png 和 flag.png,分别表示地雷和旗帜。将这两张图片放在同级目录下即可。
通过运行该代码,即可启动扫雷游戏,使用鼠标左键揭开方块,使用鼠标右键标记地雷位置。
阅读全文