目录

游戏界面尺寸

方块尺寸

雷的数量

游戏状态

最后,我们定义一个函数run(),用于运行游戏:


带你用Python制作一个经典小游戏:扫雷-LMLPHP

 

带你用Python制作一个经典小游戏:扫雷-LMLPHP

扫雷是一款单人益智游戏,目的是在不触雷的情况下,揭示所有非雷方块。玩家可以通过点击方块来揭示其内容,如果揭示到雷,游戏结束。在游戏开始前,玩家可以选择游戏难度,包括初级、中级和高级。

代码实现:

首先,我们需要导入必要的模块:

import random import pygame

然后,定义一些常量和变量:

游戏界面尺寸

SCREEN_WIDTH = 400 SCREEN_HEIGHT = 500

方块尺寸

BLOCK_WIDTH = 20 BLOCK_HEIGHT = 20

雷的数量

BOMB_COUNT = {"easy": 10, "medium": 40, "hard": 99}

游戏状态

GAME_START = 0 GAME_RUN = 1 GAME_OVER = 2

接下来,定义我们的主类Minesweeper:

class Minesweeper: def init(self): # 初始化游戏界面和游戏状态 self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.game_state = GAME_START self.clock = pygame.time.Clock() pygame.display.set_caption("Minesweeper")

# 初始化游戏
def init_game(self):
    # 初始化地图
    self.map = [[0 for i in range(10)] for j in range(10)]
    self.bomb_count = BOMB_COUNT[self.difficulty]
    self.flag_count = self.bomb_count
    self.bomb_location = []
    self.left_click_count = 0

    # 随机生成地雷
    while len(self.bomb_location) < self.bomb_count:
        x = random.randint(0, 9)
        y = random.randint(0, 9)
        if self.map[y][x] == 0:
            self.map[y][x] = -1
            self.bomb_location.append((x, y))

    # 计算每个方块周围的地雷数
    for y in range(10):
        for x in range(10):
            if self.map[y][x] == -1:
                continue
            count = 0
            for yy in range(y - 1, y + 2):
                for xx in range(x - 1, x + 2):
                    if xx < 0 or xx > 9 or yy < 0 or yy > 9:
                        continue
                    if self.map[yy][xx] == -1:
                        count += 1
            self.map[y][x] = count

在init_game()函数中,我们首先初始化了地图,然后随机生成地雷,并且计算每个方块周围的地雷数。

接下来,定义一个函数draw_block(),用于绘制方块:

带你用Python制作一个经典小游戏:扫雷-LMLPHP

 

# 绘制方块
def draw_block(self, x, y, value):
    if value == -1:
        # 绘制雷
        pygame.draw.rect(self.screen, (255, 0, 0), (x * BLOCK_WIDTH, y * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT))
    else:
        # 绘制数字或空白方块
        pygame.draw.rect(self.screen, (255, 255, 255), (x * BLOCK_WIDTH, y * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT))
        if value > 0:
            font = pygame.font.SysFont(None, BLOCK_HEIGHT - 2)
            text = font.render(str(value), True, (0, 0, 0))
            text_rect = text.get_rect()
            text_rect.centerx = x * BLOCK_WIDTH + BLOCK_WIDTH / 2
            text_rect.centery = y * BLOCK_HEIGHT + BLOCK_HEIGHT / 2
            self.screen.blit(text, text_rect)

在主类中,我们还需要定义事件处理函数handle_event():

# 事件处理函数
def handle_event(self, event):
    if event.type == pygame.QUIT:
        self.game_state = GAME_OVER
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            self.game_state = GAME_OVER
        elif event.key == pygame.K_F2:
            self.init_game()
    elif event.type == pygame.MOUSEBUTTONDOWN:
        if self.game_state == GAME_START:
            self.game_state = GAME_RUN
        if event.button == 1:
            # 左键
            x, y = event.pos
            x = x / BLOCK_WIDTH
            y = y / BLOCK_HEIGHT
            if self.map[y][x] == -1:
                self.game_state = GAME_OVER
            else:
                self.reveal_block(x, y)
                self.left_click_count += 1
        elif event.button == 3:
            # 右键
            x, y = event.pos
            x = x / BLOCK_WIDTH
            y = y / BLOCK_HEIGHT
            if self.map[y][x] == -1:
                self.bomb_count += 1
                self.flag_count += 1
            else:
                self.flag_count -= 1
            self.map[y][x] = -2 if self.map[y][x] == 0 else 0

在handle_event()函数中,我们处理了游戏中的各种事件,包括鼠标左键点击、鼠标右键点击、键盘按键等。

接下来,我们定义一个函数reveal_block(),用于揭示方块:

# 揭示方块
def reveal_block(self, x, y):
    if x < 0 or x > 9 or y < 0 or y > 9:
        return
    if self.map[y][x] == -1 or self.map[y][x] == -2:
        return
    if self.map[y][x] > 0:
        self.map[y][x] = -2
        return
    self.map[y][x] = -2
    self.reveal_block(x - 1, y)
    self.reveal_block(x + 1, y)
    self.reveal_block(x, y - 1)
    self.reveal_block(x, y + 1)
    self.reveal_block(x - 1, y - 1)
    self.reveal_block(x + 1, y - 1)
    self.reveal_block(x - 1, y + 1)
    self.reveal_block(x + 1, y + 1)

在reveal_block()函数中,我们首先检查方块是否是地雷或旗子,如果是,则不做任何操作。如果方块是数字或空白方块,则将其标记为已经揭示,并递归揭示周围的方块。

最后,我们定义一个函数run(),用于运行游戏:

# 运行游戏
def run(self):
    while self.game_state != GAME_OVER:
        self.clock.tick(60)
        for event in pygame.event.get():
            self.handle_event(event)
        self.screen.fill((0, 0, 0))
        if self.game_state == GAME_RUN:
            for y in range(10):
                for x in range(10):
                    self.draw_block(x, y, self.map[y][x])
            if self.left_click_count == 100 - self.bomb_count:
                self.game_state = GAME_OVER
                font = pygame.font.SysFont(None, 30)
                text = font.render("You Win!", True, (0, 255, 0))
                text_rect = text.get_rect()
                text_rect.centerx = SCREEN_WIDTH / 2
                text_rect.centery = SCREEN_HEIGHT - 80
                self.screen.blit(text, text_rect)
        elif self.game_state == GAME_START:
            font = pygame.font.SysFont(None, 30)
            text = font.render("Press any key to start game", True, (255, 255, 255))
            text_rect = text.get_rect()
            text_rect.centerx = SCREEN_WIDTH / 2
            text_rect.centery = SCREEN_HEIGHT - 80
            self.screen.blit(text, text_rect)
        else:
            # GAME_OVER
            font = pygame.font.SysFont(None, 30)
            text = font.render("Game Over! Press F2 to restart", True, (255, 0, 0))
            text_rect = text.get_rect()
            text_rect.centerx = SCREEN_WIDTH / 2
            text_rect.centery = SCREEN_HEIGHT - 80
            self.screen.blit(text, text_rect)
        pygame.display.update()

最后,我们创建一个Minesweeper对象并运行游戏:

if name == "main": pygame.init() minesweeper = Minesweeper() minesweeper.difficulty = "easy" # 初级 minesweeper.init_game() minesweeper.run() pygame.quit()

带你用Python制作一个经典小游戏:扫雷-LMLPHP

07-02 18:48