Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Maze 3D by Al Sweigart al@inventwithpython.com
- import copy, sys, os
- # Maze file constants:
- WALL = '#'
- EMPTY = ' '
- START = 'S'
- EXIT = 'E'
- BLOCK = chr(9608)
- NORTH = 'n'
- SOUTH = 's'
- EAST = 'e'
- WEST = 'w'
- # A
- #BCD
- #E@F
- r'''
- .................
- ____.........____
- ...|\......./|...
- ...||.......||...
- ...||__...__||...
- ...||.|\./|.||...
- ...||.|.X.|.||...
- ...||.|/.\|.||...
- ...||_/...\_||...
- ...||..EXIT.||...
- ___|/.......\|___
- EXIT.........EXIT
- .................'''
- def wallStrToWallDict(wallStr):
- wallDict = {}
- height = 0
- width = 0
- for y, line in enumerate(wallStr.splitlines()):
- if y > height:
- height = y
- for x, character in enumerate(line):
- if x > width:
- width = x
- wallDict[(x, y)] = character
- wallDict['height'] = height + 1
- wallDict['width'] = width + 1
- return wallDict
- EXIT = {(0, 0): 'E', (1, 0): 'X', (2, 0): 'I', (3, 0): 'T'}
- ALL_OPEN = wallStrToWallDict(r'''
- .................
- ____.........____
- ...|\......./|...
- ...||.......||...
- ...||__...__||...
- ...||.|\./|.||...
- ...||.|.X.|.||...
- ...||.|/.\|.||...
- ...||_/...\_||...
- ...||.......||...
- ___|/.......\|___
- .................
- .................'''.strip())
- CLOSED = {}
- CLOSED['A'] = wallStrToWallDict(r'''
- _____
- .....
- .....
- .....
- _____'''.strip()) # Paste to 6, 4
- CLOSED['B'] = wallStrToWallDict(r'''
- .\.
- ..\
- ...
- ...
- ...
- ../
- ./.'''.strip()) # Paste to 4, 3
- CLOSED['C'] = wallStrToWallDict(r'''
- ___________
- ...........
- ...........
- ...........
- ...........
- ...........
- ...........
- ...........
- ...........
- ___________'''.strip()) # Paste to 3, 1
- CLOSED['D'] = wallStrToWallDict(r'''
- ./.
- /..
- ...
- ...
- ...
- \..
- .\.'''.strip()) # Paste to 10, 3
- CLOSED['E'] = wallStrToWallDict(r'''
- ..\..
- ...\_
- ....|
- ....|
- ....|
- ....|
- ....|
- ....|
- ....|
- ....|
- ....|
- .../.
- ../..'''.strip()) # Paste to 0, 0
- CLOSED['F'] = wallStrToWallDict(r'''
- ../..
- _/...
- |....
- |....
- |....
- |....
- |....
- |....
- |....
- |....
- |....
- .\...
- ..\..
- '''.strip()) # Paste to 12, 0
- # TODO note the extra new line
- def displayWallDict(wallDict):
- print(BLOCK * (wallDict['width'] + 2))
- for y in range(wallDict['height']):
- print(BLOCK, end='')
- for x in range(wallDict['width']):
- wall = wallDict[(x, y)]
- if wall == '.':
- wall = ' '
- print(wall, end='')
- print(BLOCK) # Print block with a newline.
- print(BLOCK * (wallDict['width'] + 2))
- def pasteWallDict(srcWallDict, dstWallDict, left, top):
- dstWallDict = copy.copy(dstWallDict)
- for x in range(srcWallDict['width']):
- for y in range(srcWallDict['height']):
- dstWallDict[(x + left, y + top)] = srcWallDict[(x, y)]
- return dstWallDict
- # A
- #BCD
- #E@F
- def makeWallDict(maze, playerx, playery, playerDirection, exitx, exity):
- section = {}
- if playerDirection == NORTH:
- # Map of the sections, relative A
- # to the player @: BCD
- # E@F
- offsets = (('A', 0, -2), ('B', -1, -1), ('C', 0, -1), ('D', 1, -1), ('E', -1, 0), ('F', 1, 0))
- if playerDirection == SOUTH:
- # Map of the sections, relative F@E
- # to the player @: DCB
- # A
- offsets = (('A', 0, 2), ('B', 1, 1), ('C', 0, 1), ('D', -1, 1), ('E', 1, 0), ('F', -1, 0))
- if playerDirection == EAST:
- # Map of the sections, relative EB
- # to the player @: @CA
- # FD
- offsets = (('A', 2, 0), ('B', 1, -1), ('C', 1, 0), ('D', 1, 1), ('E', 0, -1), ('F', 0, 1))
- if playerDirection == WEST:
- # Map of the sections, relative DF
- # to the player @: AC@
- # BE
- offsets = (('A', -2, 0), ('B', -1, 1), ('C', -1, 0), ('D', -1, -1), ('E', 0, 1), ('F', 0, -1))
- for sec, xOffset, yOffset in offsets:
- section[sec] = maze.get((playerx + xOffset, playery + yOffset), WALL)
- wallDict = copy.copy(ALL_OPEN)
- PASTE_CLOSED_TO = {'A': (6, 4), 'B': (4, 3), 'C': (3, 1), 'D': (10, 3), 'E': (0, 0), 'F': (12, 0)}
- for sec in 'ABDCEF':
- if section[sec] == WALL:
- wallDict = pasteWallDict(CLOSED[sec], wallDict, PASTE_CLOSED_TO[sec][0], PASTE_CLOSED_TO[sec][1])
- '''
- displayWallDict(pasteWallDict(CLOSED['A'], ALL_OPEN, 6, 4))
- displayWallDict(pasteWallDict(CLOSED['B'], ALL_OPEN, 4, 3))
- displayWallDict(pasteWallDict(CLOSED['C'], ALL_OPEN, 3, 1))
- displayWallDict(pasteWallDict(CLOSED['D'], ALL_OPEN, 10, 3))
- displayWallDict(pasteWallDict(CLOSED['E'], ALL_OPEN, 0, 0))
- displayWallDict(pasteWallDict(CLOSED['F'], ALL_OPEN, 12, 0))
- '''
- sys.exit()
- print('MAZE RUNNER 3D')
- print('By Al Sweigart al@inventwithpython.com')
- print()
- print('(Maze files are generated by mazemaker.py)')
- # Get the maze file's filename from the user:
- while True:
- print('Enter the filename of the maze (or "quit"):')
- filename = input()
- if filename.upper() == 'QUIT':
- sys.exit()
- if os.path.exists(filename):
- break
- print('There is no file named', filename)
- # Load the maze from a file:
- mazeFile = open(filename)
- maze = {}
- lines = mazeFile.readlines()
- playerx = None
- playery = None
- exitx = None
- exity = None
- y = 0
- for line in lines:
- WIDTH = len(line.rstrip())
- for x, character in enumerate(line.rstrip()):
- assert character in (WALL, EMPTY, START, EXIT), 'Invalid character at column %s, line %s' % (x + 1, y + 1)
- if character in (WALL, EMPTY):
- maze[(x, y)] = character
- elif character == START:
- playerx, playery = x, y
- maze[(x, y)] = EMPTY
- elif character == EXIT:
- exitx, exity = x, y
- maze[(x, y)] = EMPTY
- y += 1
- HEIGHT = y
- assert playerx != None and playery != None, 'Missing start point in maze file.'
- assert exitx != None and exity != None, 'Missing exit point in maze file.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement