写一个迷宫的小游戏代码
时间: 2024-01-29 20:02:58 浏览: 25
# 迷宫游戏代码
# 定义迷宫的大小
maze_width = 10
maze_height = 10
# 定义迷宫路径
maze_path = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 1, 1]]
# 定义迷宫的起点和终点
start_x = 0
start_y = 0
end_x = 9
end_y = 9
# 定义迷宫的移动方向
direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# 定义一个空数组,用于保存走过的路径
path = []
# 定义一个标记数组,用于标记迷宫中的每一个位置
mark = [[0 for col in range(maze_width)] for row in range(maze_height)]
# 设置起点的标记
mark[start_x][start_y] = 1
# 定义一个函数,用于寻找迷宫的路径
def findPath(x, y):
# 如果到达终点,则将路径保存到数组中
if x == end_x and y == end_y:
path.append([x, y])
return True
# 如果当前