# Draw a Map
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],
]
# define where people are located (initialize)
x = 2
y = 1
EndX = 7
Endy = 9
# re-assigning the map with a string
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 ("")
# The Core knowledge that is used
# print ("Pre-swap map")
# Print_map ()
# Map_data[2][1], map_data[2+1][1] = map_data[2+1][1], map_data[2][1]
# print ("Post-swap map")
# Print_map ()
# Draw a map first
Print_map ()
While True:
# Input of the instruction
Order = input ("Please enter instruction (a: left, S: Lower, D: Right, W: up):")
# to judge the instructions entered by the user
# When the user enters a, perform a left-to-Go interchange (column row-invariant column subscript minus 1)
If order = = "a":
y = y-1
# hit the wall, the game is over
If map_data[x][y] = = 1:
Print ("Game Over")
Break
Else
MAP_DATA[X][Y],MAP_DATA[X][Y+1] = map_data[x][y+1], Map_data[x][y] # Exchange operation
Print_map ()
# when the user enters S, the execution goes down to swap (column invariant row to row subscript plus 1)
Elif order = = "S":
x = x + 1
If map_data[x][y] = = 1:
Print ("Game Over")
Break
Else
Map_data[x][y], map_data[x-1][y] = Map_data[x-1][y], Map_data[x][y] # Exchange operation
Print_map ()
# when the user enters D, performs a right-walk interchange (column row-invariant column subscript plus 1)
Elif order = = "D":
y = y + 1
If map_data[x][y] = = 1:
Print ("Game Over")
Break
Else
Map_data[x][y], map_data[x][y-1] = map_data[x][y-1], Map_data[x][y] # Exchange operation
Print_map ()
If map_data[x][y] = = Map_data[endx][endy]:
Print ("Congratulations on your pass")
Break
# performs an upward walk when the user enters W (column invariant row subscript minus 1)
Elif order = = "W":
x = x-1
If map_data[x][y] = = 1:
Print ("Game Over")
Break
Else
Map_data[x][y], map_data[x + 1][y] = map_data[x + 1][y], Map_data[x][y] # Exchange operation
Print_map ()
# When the user enters an irregular instruction in the error prompt, and re-enter
Else
Print ("You entered the command in error, please re-enter by the instruction rule!") ")
Continue
#
:
Python Maze Game (basic version)