Python迷宮遊戲(基礎版)

來源:互聯網
上載者:User

標籤:pos   錯誤提示   pytho   gpo   pre   圖片   初始   賦值   ima   

 

 

#  畫地圖
map_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 1, 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, 1],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
# 定義人所在的位置(初始化)
x = 2
y = 1
endx = 7
endy = 9
# 用字串重新給地圖賦值
def print_map():
for nums in map_data:
for num in nums:
if num == 1:
print(" #",end=" ")
elif(num == 0):
print(" ",end=" ")
else:
print(" $",end=" ")
print("")

# 所用到的核心知識

# print("交換前的地圖")
# print_map()
# map_data[2][1], map_data[2+1][1] = map_data[2+1][1], map_data[2][1]
# print("交換後的地圖")
# print_map()

# 先畫地圖
print_map()
while True:
# 指令的輸入
order = input("請輸入指令(a: 左,s: 下, d: 右, w: 上):")
# 對使用者輸入的指令進行判斷
# 當使用者輸入a時執行向左走進行交換(列變行不變 列下標減1)
if order == "a":
y = y-1
# 碰到牆,遊戲結束
if map_data[x][y] == 1:
print("遊戲結束")
break
else:
map_data[x][y],map_data[x][y+1] = map_data[x][y+1], map_data[x][y] # 進行交換操作
print_map()

# 當使用者輸入s時執行向下走進行交換(列不變行變 行下標加1)
elif order == "s":
x = x + 1
if map_data[x][y] == 1:
print("遊戲結束")
break
else:
map_data[x][y], map_data[x-1][y] = map_data[x-1][y], map_data[x][y] # 進行交換操作
print_map()

# 當使用者輸入d時執行向右走進行交換(列變行不變 列下標加1)
elif order == "d":
y = y + 1
if map_data[x][y] == 1:
print("遊戲結束")
break
else:
map_data[x][y], map_data[x][y - 1] = map_data[x][y - 1], map_data[x][y] # 進行交換操作
print_map()
if map_data[x][y] == map_data[endx][endy]:
print("恭喜你過關了")
break

# 當使用者輸入w時執行向上走進行交換(列不變行變 行下標減1)
elif order == "w":
x = x - 1
if map_data[x][y] == 1:
print("遊戲結束")
break
else:
map_data[x][y], map_data[x + 1][y] = map_data[x + 1][y], map_data[x][y] # 進行交換操作
print_map()

# 當使用者輸入非規則內的指令時的錯誤提示,並重新輸入
else:
print("您輸入指令有誤,請重新按指令規則輸入!")
continue
#

 



Python迷宮遊戲(基礎版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.