19 section[sec] = EXIT
19
19 wallDict = copy.copy(ALL_OPEN)
19 PASTE_CLOSED_TO = {'A': (6, 4), 'B': (4, 3), 'C': (3, 1),
20 'D': (10, 3), 'E': (0, 0), 'F': (12, 0)}
20 for sec in 'ABDCEF':
20 if section[sec] == WALL:
20 wallDict = pasteWallDict(CLOSED[sec], wallDict,
20 PASTE_CLOSED_TO[sec][0], PASTE_CLOSED_TO[sec][1])
20
20 # Draw the EXIT sign if needed:
20 if section['C'] == EXIT:
20 wallDict = pasteWallDict(EXIT_DICT, wallDict, 7, 9)
20 if section['E'] == EXIT:
21 wallDict = pasteWallDict(EXIT_DICT, wallDict, 0, 11)
21 if section['F'] == EXIT:
21 wallDict = pasteWallDict(EXIT_DICT, wallDict, 13, 11)
21
21 return wallDict
21
21
21 print('Maze Runner 3D, by Al Sweigart al@inventwithpython.com')
21 print('(Maze files are generated by mazemakerrec.py)')
21
22 # Get the maze file's filename from the user:
22 while True:
22 print('Enter the filename of the maze (or LIST or QUIT):')
22 filename = input('> ')
22
22 # List all the maze files in the current folder:
22 if filename.upper() == 'LIST':
22 print('Maze files found in', os.getcwd())
22 for fileInCurrentFolder in os.listdir():
22 if (fileInCurrentFolder.startswith('maze')
23 and fileInCurrentFolder.endswith('.txt')):
23 print(' ', fileInCurrentFolder)
23 continue
23
23 if filename.upper() == 'QUIT':
23 sys.exit()
23
23 if os.path.exists(filename):
23 break
23 print('There is no file named', filename)
24
24 # Load the maze from a file:
24 mazeFile = open(filename)
24 maze = {}
24 lines = mazeFile.readlines()
24 px = None
24 py = None
24 exitx = None
24 exity = None
24 y = 0
25 for line in lines:
25 WIDTH = len(line.rstrip())
25 for x, character in enumerate(line.rstrip()):
25 assert character in (WALL, EMPTY, START, EXIT), 'Invalid chacter at column {}, line {}'.format(x + 1, y + 1)
25 if character in (WALL, EMPTY):
25 maze[(x, y)] = character
25 elif character == START:
25 px, py = x, y
25 maze[(x, y)] = EMPTY
25 elif character == EXIT:
26 exitx, exity = x, y
26 maze[(x, y)] = EMPTY
26 y += 1
26 HEIGHT = y
26
26 assert px != None and py != None, 'No start point in file.'
26 assert exitx != None and exity != None, 'No exit point in file.'
26 pDir = NORTH
26
26
27 while True: # Main game loop.
27 displayWallDict(makeWallDict(maze, px, py, pDir, exitx, exity))
27
27 while True: # Get user move.
27 print('Location ({}, {}) Direction: {}'.format(px, py, pDir))
27 print(' (W)')
27 print('Enter direction: (A) (D) or QUIT.')
27 move = input('> ').upper()
27
27 if move == 'QUIT':
28 print('Thanks for playing!')
28 sys.exit()
Do'stlaringiz bilan baham: |