Python Maze Game (basic version)

Source: Internet
Author: User

# 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.