map_ditu1 = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
map_ditu2 = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
]
# 定义人所在的位置(初始化)
x = 2
y = 1
endx = 6
endy = 9
endx2= 9
endy2= 8
n1=map_ditu1
n2=map_ditu2
# 用字符串重新给地图赋值
def print_map1():
for nums in map_ditu1:
for num in nums:
if num == 1:
print(" *",end=" ")
elif(num == 0):
print(" ",end=" ")
else:
print(" $",end=" ")
print(" ")
def print_map2():
for nums in map_ditu2:
for num in nums:
if num == 1:
print(" *",end=" ")
elif(num == 0):
print(" ",end=" ")
else:
print(" $",end=" ")
print(" ")
choice=input("请输入你要选择的地图(n1 OR n2):")
if choice==n1:
print_map1()
while True:
order = input("请输入指令(a: 左,s: 下, d: 右, w: 上):")
# 当用户输入a时执行向左走进行交换(列变行不变 列下标减1)
if order == "a":
y = y-1
# 碰到墙,游戏结束
if map_ditu1[x][y] == 1:
print("游戏结束")
break
else:
map_ditu1[x][y],map_ditu1[x][y+1] = map_ditu1[x][y+1], map_ditu1[x][y] # 进行交换操作
print_map1()
# 当用户输入s时执行向下走进行交换(列不变行变 行下标加1)
elif order == "s":
x = x + 1
if map_ditu1[x][y] == 1:
print("游戏结束")
break
else:
map_ditu1[x][y], map_ditu1[x-1][y] = map_ditu1[x-1][y], map_ditu1[x][y] # 进行交换操作
print_map1()
# 当用户输入d时执行向右走进行交换(列变行不变 列下标加1)
elif order == "d":
y = y + 1
if map_ditu1[x][y] == 1:
print("游戏结束")
break
else:
map_ditu1[x][y], map_ditu1[x][y - 1] = map_ditu1[x][y - 1], map_ditu1[x][y] # 进行交换操作
print_map1()
if map_ditu1[x][y] == map_ditu1[endx][endy]:
print("恭喜你过关了")
break
# 当用户输入w时执行向上走进行交换(列不变行变 行下标减1)
elif order == "w":
x = x - 1
if map_ditu1[x][y] == 1:
print("游戏结束")
break
else:
map_ditu1[x][y], map_ditu1[x + 1][y] = map_ditu1[x + 1][y], map_ditu1[x][y] # 进行交换操作
print_map1()
else:
print("您输入指令有误,请重新输入!")
continue
else:
print_map2() # 先画地图
while True:
order = input("请输入指令(a: 左,s: 下, d: 右, w: 上):")
# 当用户输入a时执行向左走进行交换(列变行不变 列下标减1)
if order == "a":
y = y-1
# 碰到墙,游戏结束
if map_ditu2[x][y] == 1:
print("游戏结束")
break
else:
map_ditu2[x][y],map_ditu2[x][y+1] = map_ditu2[x][y+1], map_ditu2[x][y] # 进行交换操作
print_map2()
# 当用户输入s时执行向下走进行交换(列不变行变 行下标加1)
elif order == "s":
x = x + 1
if map_ditu2[x][y] == 1:
print("游戏结束")
break
else:
map_ditu2[x][y], map_ditu2[x-1][y] = map_ditu2[x-1][y], map_ditu2[x][y] # 进行交换操作
print_map2()
if map_ditu2[x][y] == map_ditu2[endx2][endy2]:
print("恭喜你过关了")
break
# 当用户输入d时执行向右走进行交换(列变行不变 列下标加1)
elif order == "d":
y = y + 1
if map_ditu2[x][y] == 1:
print("游戏结束")
break
else:
map_ditu2[x][y], map_ditu2[x][y - 1] = map_ditu2[x][y - 1], map_ditu2[x][y] # 进行交换操作
print_map2()
# 当用户输入w时执行向上走进行交换(列不变行变 行下标减1)
elif order == "w":
x = x - 1
if map_ditu2[x][y] == 1:
print("游戏结束")
break
else:
map_ditu2[x][y], map_ditu2[x + 1][y] = map_ditu2[x + 1][y], map_ditu2[x][y] # 进行交换操作
print_map2()
else:
print("您输入指令有误,请重新输入!")
continue