扫雷小游戏python代码
时间: 2023-10-30 22:07:14 浏览: 173
以下是一个简单的扫雷游戏的 Python 代码。该代码使用了 Pygame 库来创建游戏窗口和游戏界面。
```python
import pygame
import random
# 游戏参数
WIDTH = 800
HEIGHT = 600
FPS = 30
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (200, 200, 200)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 游戏初始化
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Minesweeper")
clock = pygame.time.Clock()
# 游戏变量
grid_size = 20
grid_x = int(WIDTH / grid_size)
grid_y = int((HEIGHT - 40) / grid_size)
mine_count = 50
mine_list = []
game_over = False
# 字体
font = pygame.font.SysFont(None, 24)
# 音效
boom_sound = pygame.mixer.Sound("boom.wav")
win_sound = pygame.mixer.Sound("win.wav")
# 网格类
class Grid(pygame.sprite.Sprite):
def __init__(self, x, y, size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.state = "hidden"
self.is_mine = False
self.neighbor_mines = 0
def reveal(self):
if self.state == "hidden":
self.state = "revealed"
if self.is_mine:
boom_sound.play()
global game_over
game_over = True
elif self.neighbor_mines == 0:
for neighbor in self.neighbors:
neighbor.reveal()
def flag(self):
if self.state == "hidden":
self.state = "flagged"
elif self.state == "flagged":
self.state = "hidden"
def update(self):
if self.state == "hidden":
self.image.fill(GREY)
elif self.state == "revealed":
if self.is_mine:
self.image.fill(RED)
else:
self.image.fill(WHITE)
if self.neighbor_mines > 0:
text = font.render(str(self.neighbor_mines), True, BLACK)
text_rect = text.get_rect(center=self.rect.center)
screen.blit(text, text_rect)
elif self.state == "flagged":
self.image.fill(GREEN)
# 创建网格
all_sprites = pygame.sprite.Group()
grid = []
for x in range(grid_x):
row = []
for y in range(grid_y):
g = Grid(x * grid_size, y * grid_size + 40, grid_size)
all_sprites.add(g)
row.append(g)
grid.append(row)
# 创建地雷
for i in range(mine_count):
x = random.randint(0, grid_x - 1)
y = random.randint(0, grid_y - 1)
if not grid[x][y].is_mine:
grid[x][y].is_mine = True
mine_list.append(grid[x][y])
# 计算每个格子周围的地雷数量
for x in range(grid_x):
for y in range(grid_y):
if not grid[x][y].is_mine:
neighbors = []
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if (dx != 0 or dy != 0) and x + dx >= 0 and x + dx < grid_x and y + dy >= 0 and y + dy < grid_y:
neighbors.append(grid[x + dx][y + dy])
grid[x][y].neighbors = neighbors
grid[x][y].neighbor_mines = sum(1 for n in neighbors if n.is_mine)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
pos = pygame.mouse.get_pos()
x = pos[0] // grid_size
y = (pos[1] - 40) // grid_size
if x >= 0 and x < grid_x and y >= 0 and y < grid_y:
if event.button == 1:
grid[x][y].reveal()
elif event.button == 3:
grid[x][y].flag()
# 更新状态
all_sprites.update()
# 绘制屏幕
screen.fill(BLACK)
all_sprites.draw(screen)
if game_over:
text = font.render("GAME OVER", True, WHITE)
text_rect = text.get_rect(center=(WIDTH // 2, 20))
screen.blit(text, text_rect)
elif all(g.is_mine or g.state == "revealed" for g in mine_list):
win_sound.play()
text = font.render("YOU WIN", True, WHITE)
text_rect = text.get_rect(center=(WIDTH // 2, 20))
screen.blit(text, text_rect)
pygame.display.flip()
# 控制帧率
clock.tick(FPS)
```
阅读全文