接下来用python实现一个控制台的迷宫小游戏 游戏规则如下
输入exit退出游戏
输入8为向上走
输入5为向下走
输入4为向左走
输入6为向右走
游戏地图如下
Y即为玩家,输入对应数字后可以变换位置,当撞墙时位置不变
部分源码如下
import time # 导入时间模块
# 模拟迷宫地图的二维列表
map_list=[
[" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],
["=>","Y"," "," ","@","@"," "," "," "," "," "," ","@"," "," "," ","@","@"," "," "," ","@"," ","@"," ","@"," ","@"," ","@","@"],
[" ","@"," "," ","@","@","@"," ","@"," ","@"," ","@","@","@"," "," "," "," ","@"," ","@"," ","@","@","@"," ","@","@","@","@"],
[" ","@"," ",""@","@","@"],
[" ","@","@","@","@","@"," ","@"," ","@","@"," "," ","@"," ","@","@","@","@","@"," ","@","@"," "," "," "," ","@","@","@","@"],
[" ","@"," "," "," ","@"," ","@","@","@","@","@"," ","@"," "," "," ","@","@","@"," ","@"," ","@"," ","@"," "," "," "," ","@"],
[" ","@"," ","@"," ","@"," ","@","@"," ","@","@","@"," "," ","@","@"," "," "," "," ","@","@","@"," ","@"," ","@","@","@","@"],
[" ","@"," ","@"," "," "," ","@","@"," "," "," ","@","@","@","@","@","@","@","@","@","@","@"," ","@","@","@"," "," "," ","@"],
[" ","@"," ","@","@","@","@","@","@"," ","@"," ","@","@","@","@","@"," "," "," "," "," "," "," "," "," "," "," ","@"," ","@"],
[" ","@"," "," "," "," "," "," "," "," ","@"," ","@"," "," "," "," "," ","@","@","@","@","@"," ","@","@","@"," ","@"," "," ","=>"],
[" ","@"," ","@","@","@","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@"," ","@","@","@"],
[" ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@","@","@","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@"],
[" ","@","@","@","@","@"," ","@"," ","@","@"," ","@"," "," "," "," "," "," ","@","@","@","@","@","@","@","@"," ","@"," ","@"],
[" ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@"," ","@","@","@"," "," "," "," "," "," "," "," ","@"," "," "," ","@"],
[" ","@"," ","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@","@","@","@"," ","@","@","@","@","@","@","@"," ","@"],
[" ","@"," "," "," "," "," "," "," ","@","@"," ","@","@"," "," "," "," "," "," ","@"," "," "," "," "," "," "," "," "," ","@"],
[" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"]]
# 更新地图
def up_map():
# 打印小标题并指定打印区域的文字以及背景颜色
print("\033[1;30;42m ----------小迷宫-----------")
for i,values in enumerate(map_list): # 遍历二维列表中的18个子列表
for j in range(len(values)): # 遍历子列表中的元素
# 打印每个子列表中的所有元素,并且不换行打印
print(map_list[i][j], end="")
print("") # 每打印一个子列表所有元素,换行一次
print("\033[0m",end="") # 背景色结束位置
if __name__ == '__main__':
x = 1 # 横向坐标默认值
y = 1 # 纵向坐标默认值
step_number = 0 # 记录行走步数的变量
up_map() # 更新模拟地图
start = time.time() # 获取开始时的时间戳
while True:
# 判断当小人走到迷宫出口时,打印胜利文字,跳出循环
if x == 30 and y == 10:
print("\033[31m恭喜您!走出了迷宫!\033[0m")
print("\033[31m共计行走了",step_number,"步!\033[0m")
print("\033[31m共计用时",int(time.time()-start),"秒!\033[0m")
break
print("\033[31m请输入行走的方向(输入exit将退出游戏!)\033[0m")
# 获取输入的行走方向8为上、5为下、4为左、6为右
direction = input("\033[31m8为上、5为下、4为左、6为右:\033[0m")
# 当用户输入5,控制人物向下走
if (direction == '5'):
if map_list[y+1][x] != "@": # 判断行走位置不是墙时,@(表示墙)
map_list[y][x] = " " # 将小人原来的位置设置为空
y += 1 # 修改Y坐标位置
map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人)
step_number+=1 # 修改小人移动步数
else:
print('\033[31m碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
up_map() # 更新地图
# 当用户输入6,控制人物向右走
elif (direction == '6'):
if map_list[y][x + 1] != "@":
map_list[y][x] = " "
x += 1
map_list[y][x] = "Y"
step_number += 1 # 修改小人移动步数
else:
print('\033[31m碰到墙壁!\033[0m')
up_map() #更新地图
# 当用户输入8,控制人物向上走
elif (direction == '8'):
if map_list[y - 1][x] != "@":
map_list[y][x] = " "
y -= 1
物向左走
elif (direction == '4'):
if map_list[y][x - 1] != "@":
map_list[y][x] = " "
x -= 1
map_list[y][x] = "Y"
step_number += 1 # 修改小人移动步数
else:
print('\033[31m碰到墙壁!\033[0m')
up_map() # 更新地图
# 当用户输入退出命令,跳出循环
elif (direction == "exit"):
print("游戏退出。")
break