python扫雷小游戏

这篇文章介绍了使用Python库Pygame开发的一个简单的雷区游戏,包括游戏元素初始化、游戏界面绘制、鼠标事件处理和游戏逻辑。玩家通过左键点击翻开方块,右键插旗,目标是避开炸弹并揭开所有安全方块以获胜。
摘要由CSDN通过智能技术生成

                                                         此代码对电脑要求有点大

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)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值