下面我为你讲解如何用python写扫雷游戏实例代码:
1. 准备工作
首先,需要安装pygame库,这里以pip安装为例。
pip install pygame
安装完成后,我们可以开始编写代码。
2. 创建游戏窗口
首先,要在pygame中创建游戏窗口。我们可以使用pygame.display.set_mode方法来创建游戏窗口。
import pygame
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
运行代码,可以看到一个空白的游戏窗口。
3. 绘制游戏界面
接下来,我们向游戏窗口中绘制游戏主界面。需要用到pygame.draw方法来绘制矩形和直线。
import pygame
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
# 绘制游戏界面
block_size = 30
for x in range(0, screen_width, block_size):
pygame.draw.line(screen, (0, 0, 0), (x, 0), (x, screen_height))
for y in range(0, screen_height, block_size):
pygame.draw.line(screen, (0, 0, 0), (0, y), (screen_width, y))
pygame.display.update()
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
运行代码,可以看到一个由多个格子构成的游戏界面。
4. 添加雷和数字
要添加雷和数字,需要先定义一个格子类,表示每一个格子的状态。
class Block:
def __init__(self, row, col, block_size):
self.row = row
self.col = col
self.x = col * block_size
self.y = row * block_size
self.width = block_size
self.is_mine = False
self.num = 0
定义好格子类后,可以在游戏界面上随机生成一些雷,并计算每个格子周围的雷数,并用数字表示出来。
import pygame
import random
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义格子类
class Block:
def __init__(self, row, col, block_size):
self.row = row
self.col = col
self.x = col * block_size
self.y = row * block_size
self.width = block_size
self.is_mine = False
self.num = 0
# 随机生成雷
def create_mines(blocks, num_mines):
for i in range(num_mines):
while True:
block = random.choice(blocks)
if not block.is_mine:
block.is_mine = True
break
# 计算每个格子周围的雷数
def calculate_num(blocks):
for block in blocks:
if block.is_mine:
block.num = -1
else:
row = block.row
col = block.col
num = 0
for i in range(row - 1, row + 2):
for j in range(col - 1, col + 2):
if i < 0 or i >= len(blocks) or j < 0 or j >= len(blocks) or (i == row and j == col):
continue
if blocks[i * len(blocks[0]) + j].is_mine:
num += 1
block.num = num
# 绘制游戏界面
block_size = 30
blocks = []
for row in range(screen_height // block_size):
for col in range(screen_width // block_size):
block = Block(row, col, block_size)
blocks.append(block)
pygame.draw.rect(screen, (192, 192, 192), (block.x, block.y, block.width, block.width))
create_mines(blocks, 99)
calculate_num(blocks)
for block in blocks:
if block.is_mine:
pygame.draw.circle(screen, (255, 0, 0), (block.x + block.width // 2, block.y + block.width // 2), block.width // 2 // 2)
elif block.num > 0:
font = pygame.font.Font(None, block.width // 2)
text = font.render(str(block.num), True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.center = (block.x + block.width // 2, block.y + block.width // 2)
screen.blit(text, text_rect)
pygame.display.update()
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
运行代码,可以看到一个有雷和数字的游戏界面。
5. 点击格子
最后,还需要添加点击格子的交互。当点击一个格子时,如果是雷,则游戏结束;否则显示该格子周围的数字。
import pygame
import random
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义格子类
class Block:
def __init__(self, row, col, block_size):
self.row = row
self.col = col
self.x = col * block_size
self.y = row * block_size
self.width = block_size
self.is_mine = False
self.num = 0
# 创建雷
def create_mines(blocks, num_mines):
for i in range(num_mines):
while True:
block = random.choice(blocks)
if not block.is_mine:
block.is_mine = True
break
# 计算数字
def calculate_num(blocks):
for block in blocks:
if block.is_mine:
block.num = -1
else:
row = block.row
col = block.col
num = 0
for i in range(row - 1, row + 2):
for j in range(col - 1, col + 2):
if i < 0 or i >= len(blocks) or j < 0 or j >= len(blocks) or (i == row and j == col):
continue
if blocks[i * len(blocks[0]) + j].is_mine:
num += 1
block.num = num
# 根据数字和雷的位置绘制游戏界面
def draw_screen(blocks):
screen.fill((192, 192, 192))
for block in blocks:
if block.is_mine:
pygame.draw.circle(screen, (255, 0, 0), (block.x + block.width // 2, block.y + block.width // 2), block.width // 2 // 2)
elif block.num > 0:
font = pygame.font.Font(None, block.width // 2)
text = font.render(str(block.num), True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.center = (block.x + block.width // 2, block.y + block.width // 2)
screen.blit(text, text_rect)
elif block.num == 0:
pygame.draw.rect(screen, (255, 255, 255), (block.x, block.y, block.width, block.width), 1)
# 游戏主循环
def main(blocks):
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
row = pos[1] // block_size
col = pos[0] // block_size
block = blocks[row * len(blocks[0]) + col]
if block.is_mine:
running = False
elif block.num == 0:
block.num = -2
queue = [block]
while queue:
temp = queue.pop(0)
row = temp.row
col = temp.col
for i in range(row - 1, row + 2):
for j in range(col - 1, col + 2):
if i < 0 or i >= len(blocks) or j < 0 or j >= len(blocks) or (i == row and j == col):
continue
next_block = blocks[i * len(blocks[0]) + j]
if next_block.num == 0:
queue.append(next_block)
next_block.num = next_block.num if next_block.num != -1 else -2
draw_screen(blocks)
pygame.display.update()
# 绘制游戏界面
block_size = 30
blocks = []
for row in range(screen_height // block_size):
for col in range(screen_width // block_size):
block = Block(row, col, block_size)
blocks.append(block)
create_mines(blocks, 99)
calculate_num(blocks)
draw_screen(blocks)
pygame.display.update()
main(blocks)
pygame.quit()
exit()
运行代码,按钮点击格子,可以看到数字不断展开,直到没有空格子。如果点到雷,游戏结束。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:用python写扫雷游戏实例代码分享 - Python技术站