【Python游戏开发】扫雷游戏demo
准备步骤
项目开发使用【Mu 编辑器】
1.使用Mu编辑器创建新项目
2.点击【保存】,将项目保存到指定路径中
3.点击【图片】,会打开项目图片存储位置,将所需图片复制粘贴至该文件夹中
游戏编写
1.设置游戏场景
ROWS = 15 # 设置行数
COLS = 15 # 设置列数
SIZE = 25 # 方块尺寸
WIDTH = SIZE * COLS # 游戏场景宽度
HEIGHT = SIZE * ROWS # 游戏场景高度
2.生成方块阵列
blocks = []
# 初始化方块列表
for i in range(ROWS):
for j in range(COLS):
block = Actor("minesweep_block") # 方块初始显示背面状态
block.left = j * SIZE # 方块对应横坐标位置
block.top = i * SIZE # 方块对应纵坐标位置
block.isbomb = False # 地雷标识,False表示其不是地雷
blocks.append(block)
# 绘制图像
def draw():
for block in blocks:
block.draw()
点击【开始】运行游戏,可见窗口显示下方方块矩阵
3.矩阵埋雷
import random
BOMBS = 20 # 地雷数量
# 使用random.shuffle()方法打乱地雷列表,改变每块方块对应的索引值
random.shuffle(blocks)
# 埋设地雷,因为矩阵方块对应的索引值被打乱了,所以直接设置前“BOMBS”个方块为地雷即可
for i in range(BOMBS):
blocks[i].isbomb = True # 设置其为地雷
blocks[i].image = "minesweep_bomb" # 顺便将图片更换为地雷,方便测试
点击【开始】运行游戏,可见原来的方块矩阵中出现了地雷,而且地雷的位置是随机的
4.实现右键插旗操作
# Pgzero提供的on_mouse_down函数,用于处理鼠标的单机操作
def on_mouse_down(pos, button):
for block in blocks:
# 判断点击矩阵中的哪个方块
if block.collidepoint(pos):
# 判断点击操作是否为鼠标右键
if button == mouse.RIGHT:
# 处理右键插旗操作
set_flag(block)
# 右键插旗操作
def set_flag(block):
# 判断是否已标记插旗,False:未标记,则标记为插旗状态,True:已标记,则取消标记
if not block.isflag:
block.image = "minesweep_flag" # 更改为旗子图标
block.isflag = True
else:
block.image = "minesweep_block" # 更改为普通图标
block.isflag = False
注:需先在初始化地雷列表代码中定义
block.isflag = False
,否则会报AttributeError: 'Actor' object has no attribute 'isflag'
的错误
# 初始化地雷列表
for i in range(ROWS):
for j in range(COLS):
block = Actor("minesweep_block")
block.left = j * SIZE
block.top = i * SIZE
blocks[i].isbomb = False
block.isflag = False # 定义插旗标识
blocks.append(block)
点击【开始】运行游戏,右键点击方块,会使方块变为插旗状态,再次点击,会便会普通状态
5.实现左键翻开操作
def on_mouse_down(pos, button):
for block in blocks:
if block.collidepoint(pos):
if button == mouse.RIGHT:
set_flag(block)
# 判断点击操作是否为鼠标左键,且点击方块被插旗
elif button == mouse.LEFT and not block.isflag:
# 处理左键翻开操作
open_block(block)
# 左键翻开操作
def open_block(bk):
bk.isopen = True # 标识为已翻开
bombnum = get_bomb_number(bk) # 获取其周围地雷数量
bk.image = "minesweep_number" + str(bombnum)
# 如果周围地雷数量不为0,则返回
if bombnum != 0:
return
# 如果周围地雷数量为0,则翻开周围未翻开的方块
for block in get_neighbours(bk):
if not block.isopen:
# 递归翻开方块九宫格区域未翻开的方块
open_block(block)
# 获取翻开方块周围9宫格区域存在的未翻开的方块
def get_neighbours(bk):
nblocks = []
for block in blocks:
# 如果方块已翻开,则不插入列表中
if block.isopen:
continue
# 如果方块在当前点击方块的九宫格区域内,则插入列表中
if block.x == bk.x - SIZE and block.y == bk.y \
or block.x == bk.x + SIZE and block.y == bk.y \
or block.x == bk.x and block.y == bk.y - SIZE \
or block.x == bk.x and block.y == bk.y + SIZE \
or block.x == bk.x - SIZE and block.y == bk.y - SIZE \
or block.x == bk.x + SIZE and block.y == bk.y - SIZE \
or block.x == bk.x - SIZE and block.y == bk.y + SIZE \
or block.x == bk.x + SIZE and block.y == bk.y + SIZE :
nblocks.append(block)
return nblocks
# 计算翻开方块周围9宫格区域的地雷数
def get_bomb_number(bk):
num = 0
for block in get_neighbours(bk):
if block.isbomb:
num += 1
return num
`block.x == bk.x - SIZE and block.y == bk.y` # 该方块在当前点击方块左方,即九宫格的4位置 `block.x == bk.x + SIZE and block.y == bk.y` # 该方块在当前点击方块右方,即九宫格的6位置 `block.x == bk.x and block.y == bk.y - SIZE` # 该方块在当前点击方块下方,即九宫格的2位置 `block.x == bk.x and block.y == bk.y + SIZE` # 该方块在当前点击方块上方,即九宫格的8位置 `block.x == bk.x - SIZE and block.y == bk.y - SIZE` # 该方块在当前点击方块左下方,即九宫格的1位置 `block.x == bk.x + SIZE and block.y == bk.y - SIZE` # 该方块在当前点击方块右下方,即九宫格的3位置 `block.x == bk.x - SIZE and block.y == bk.y + SIZE` # 该方块在当前点击方块左上方,即九宫格的7位置 `block.x == bk.x + SIZE and block.y == bk.y + SIZE ` # 该方块在当前点击方块右上方,即九宫格的9位置
注:需先在初始化地雷列表代码中定义
block.isbomb= False
,否则会报AttributeError: 'Actor' object has no attribute 'isbomb'
的错误
# 初始化地雷列表
for i in range(ROWS):
for j in range(COLS):
block = Actor("minesweep_block")
block.left = j * SIZE
block.top = i * SIZE
blocks[i].isbomb = False
block.isflag = False
block.isopen = False # 定义翻开标识
blocks.append(block)
到这一步后,运行游戏,点击非地雷方块,若九宫格内都无地雷,会全部展开,否则,会告知其周围有几个地雷
6.判断点击地雷
failed = False # 游戏失败标识
def on_mouse_down(pos, button):
for block in blocks:
if block.collidepoint(pos):
if button == mouse.RIGHT:
set_flag(block)
elif button == mouse.LEFT and not block.isflag:
# 判断点击的方块是否为地雷块
if block.isbomb:
blow_up()
else:
open_block(block)
# 翻开地雷,判断游戏失败
def blow_up():
global failed
# 将failed改为True,代表游戏失败
failed = True
def draw():
for block in blocks:
block.draw()
# 游戏失败判定
if failed:
screen.draw.text("Failed",center=(WIDTH // 2, HEIGHT // 2),fontsize = 100, color = "red")
运行游戏,直接点击地雷会出现失败提示
7.游戏胜利判定
finished = False # 游戏胜利标识
# 使用update函数实时检测游戏是否已达成结束条件
def update():
global finished
# 游戏已胜利或失败,则退出,游戏继续
if finished or failed:
return
# 遍历所有方块
for block in blocks:
# 如果存在不是地雷、且未翻开的方块,则退出,游戏继续
if not block.isbomb and not block.isopen:
return
# 所有非地雷的方块都已被翻开,则游戏结束
finished = True
def draw():
for block in blocks:
block.draw()
if failed:
screen.draw.text("Failed",center=(WIDTH // 2, HEIGHT // 2),fontsize = 100, color = "red")
# 游戏胜利提示
if finished:
screen.draw.text("finished",center=(WIDTH // 2, HEIGHT // 2),fontsize = 100, color = "red")
运行游戏,翻开所有非地雷的方块后
完整游戏代码
import random
ROWS = 15 # 设置行数
COLS = 15 # 设置列数
SIZE = 25 # 方块尺寸
WIDTH = SIZE * COLS # 游戏场景宽度
HEIGHT = SIZE * ROWS # 游戏场景高度
failed = False # 游戏失败标识
finished = False # 游戏胜利标识
blocks = [] # 方块列表
BOMBS = 20 # 地雷数量
# 初始化方块列表
for i in range(ROWS):
for j in range(COLS):
block = Actor("minesweep_block") # 方块初始显示背面状态
block.left = j * SIZE # 方块对应横坐标位置
block.top = i * SIZE # 方块对应纵坐标位置
block.isbomb = False # 地雷标识,False表示其不是地雷
block.isflag = False # 定义插旗标识
block.isopen = False # 定义翻开标识
blocks.append(block)
random.shuffle(blocks) # 打乱方块列表
# 埋设地雷
for i in range(BOMBS):
blocks[i].isbomb = True # 设置其为地雷
# blocks[i].image = "minesweep_bomb" # 顺便将图片更换为地雷,方便测试
# 处理鼠标事件
def on_mouse_down(pos, button):
# 游戏已结束,不处理点击事件
if finished or failed:
return
for block in blocks:
# 判断点击矩阵中的哪个方块
if block.collidepoint(pos) and not block.isopen:
# 鼠标右键点击
if button == mouse.RIGHT:
# 处理右键插旗操作
set_flag(block)
# 鼠标左键点击
elif button == mouse.LEFT and not block.isflag:
# 点击地雷块
if block.isbomb:
blow_up()
# 点击普通方块
else:
open_block(block)
# 右键插旗操作
def set_flag(block):
# 判断是否已标记插旗,False:未标记,则标记为插旗状态,True:已标记,则取消标记
if not block.isflag:
block.image = "minesweep_flag" # 更改为旗子图标
block.isflag = True
else:
block.image = "minesweep_block" # 更改为普通图标
block.isflag = False
# 左键翻开操作
def open_block(bk):
bk.isopen = True # 标识为已翻开
bombnum = get_bomb_number(bk) # 获取其周围地雷数量
bk.image = "minesweep_number" + str(bombnum)
# 如果周围地雷数量不为0,则返回
if bombnum != 0:
return
# 如果周围地雷数量为0,则翻开周围未翻开的方块
for block in get_neighbours(bk):
if not block.isopen:
# 递归翻开方块九宫格区域未翻开的方块
open_block(block)
# 获取翻开方块周围9宫格区域存在的未翻开的方块
def get_neighbours(bk):
nblocks = []
for block in blocks:
# 如果方块已翻开,则不插入列表中
if block.isopen:
continue
# 如果方块在当前点击方块的九宫格区域内,则插入列表中
if block.x == bk.x - SIZE and block.y == bk.y \
or block.x == bk.x + SIZE and block.y == bk.y \
or block.x == bk.x and block.y == bk.y - SIZE \
or block.x == bk.x and block.y == bk.y + SIZE \
or block.x == bk.x - SIZE and block.y == bk.y - SIZE \
or block.x == bk.x + SIZE and block.y == bk.y - SIZE \
or block.x == bk.x - SIZE and block.y == bk.y + SIZE \
or block.x == bk.x + SIZE and block.y == bk.y + SIZE :
nblocks.append(block)
return nblocks
# 计算翻开方块周围9宫格区域的地雷数
def get_bomb_number(bk):
num = 0
# 遍历九宫格区域方块,计算地雷数量
for block in get_neighbours(bk):
if block.isbomb:
num += 1
return num
# 翻开地雷,判断游戏失败
def blow_up():
global failed
# 将failed改为True,代表游戏失败
failed = True
# 使用update函数实时检测游戏是否已达成结束条件
def update():
global finished
# 游戏已胜利或失败,则退出,游戏继续
if finished or failed:
return
# 遍历所有方块
for block in blocks:
# 如果存在不是地雷、且未翻开的方块,则退出,游戏继续
if not block.isbomb and not block.isopen:
return
# 所有非地雷的方块都已被翻开,游戏胜利
finished = True
# 绘制图像
def draw():
for block in blocks:
block.draw()
# 游戏失败判定
if failed:
screen.draw.text("Failed",center=(WIDTH // 2, HEIGHT // 2),fontsize = 100, color = "red")
# 游戏胜利提示
if finished:
screen.draw.text("finished",center=(WIDTH // 2, HEIGHT // 2),fontsize = 100, color = "red")