Advertisement
asweigart

Untitled

Jun 5th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. # Maze 3D by Al Sweigart al@inventwithpython.com
  2. import copy, sys, os
  3.  
  4. # Maze file constants:
  5. WALL = '#'
  6. EMPTY = ' '
  7. START = 'S'
  8. EXIT = 'E'
  9.  
  10. BLOCK = chr(9608)
  11. NORTH = 'n'
  12. SOUTH = 's'
  13. EAST = 'e'
  14. WEST = 'w'
  15.  
  16. # A
  17. #BCD
  18. #E@F
  19.  
  20. r'''
  21. .................
  22. ____.........____
  23. ...|\......./|...
  24. ...||.......||...
  25. ...||__...__||...
  26. ...||.|\./|.||...
  27. ...||.|.X.|.||...
  28. ...||.|/.\|.||...
  29. ...||_/...\_||...
  30. ...||..EXIT.||...
  31. ___|/.......\|___
  32. EXIT.........EXIT
  33. .................'''
  34.  
  35. def wallStrToWallDict(wallStr):
  36. wallDict = {}
  37. height = 0
  38. width = 0
  39. for y, line in enumerate(wallStr.splitlines()):
  40. if y > height:
  41. height = y
  42. for x, character in enumerate(line):
  43. if x > width:
  44. width = x
  45. wallDict[(x, y)] = character
  46. wallDict['height'] = height + 1
  47. wallDict['width'] = width + 1
  48. return wallDict
  49.  
  50. EXIT = {(0, 0): 'E', (1, 0): 'X', (2, 0): 'I', (3, 0): 'T'}
  51.  
  52. ALL_OPEN = wallStrToWallDict(r'''
  53. .................
  54. ____.........____
  55. ...|\......./|...
  56. ...||.......||...
  57. ...||__...__||...
  58. ...||.|\./|.||...
  59. ...||.|.X.|.||...
  60. ...||.|/.\|.||...
  61. ...||_/...\_||...
  62. ...||.......||...
  63. ___|/.......\|___
  64. .................
  65. .................'''.strip())
  66.  
  67. CLOSED = {}
  68. CLOSED['A'] = wallStrToWallDict(r'''
  69. _____
  70. .....
  71. .....
  72. .....
  73. _____'''.strip()) # Paste to 6, 4
  74.  
  75. CLOSED['B'] = wallStrToWallDict(r'''
  76. .\.
  77. ..\
  78. ...
  79. ...
  80. ...
  81. ../
  82. ./.'''.strip()) # Paste to 4, 3
  83.  
  84. CLOSED['C'] = wallStrToWallDict(r'''
  85. ___________
  86. ...........
  87. ...........
  88. ...........
  89. ...........
  90. ...........
  91. ...........
  92. ...........
  93. ...........
  94. ___________'''.strip()) # Paste to 3, 1
  95.  
  96. CLOSED['D'] = wallStrToWallDict(r'''
  97. ./.
  98. /..
  99. ...
  100. ...
  101. ...
  102. \..
  103. .\.'''.strip()) # Paste to 10, 3
  104.  
  105. CLOSED['E'] = wallStrToWallDict(r'''
  106. ..\..
  107. ...\_
  108. ....|
  109. ....|
  110. ....|
  111. ....|
  112. ....|
  113. ....|
  114. ....|
  115. ....|
  116. ....|
  117. .../.
  118. ../..'''.strip()) # Paste to 0, 0
  119.  
  120. CLOSED['F'] = wallStrToWallDict(r'''
  121. ../..
  122. _/...
  123. |....
  124. |....
  125. |....
  126. |....
  127. |....
  128. |....
  129. |....
  130. |....
  131. |....
  132. .\...
  133. ..\..
  134. '''.strip()) # Paste to 12, 0
  135. # TODO note the extra new line
  136.  
  137. def displayWallDict(wallDict):
  138. print(BLOCK * (wallDict['width'] + 2))
  139. for y in range(wallDict['height']):
  140. print(BLOCK, end='')
  141. for x in range(wallDict['width']):
  142. wall = wallDict[(x, y)]
  143. if wall == '.':
  144. wall = ' '
  145. print(wall, end='')
  146. print(BLOCK) # Print block with a newline.
  147. print(BLOCK * (wallDict['width'] + 2))
  148.  
  149. def pasteWallDict(srcWallDict, dstWallDict, left, top):
  150. dstWallDict = copy.copy(dstWallDict)
  151. for x in range(srcWallDict['width']):
  152. for y in range(srcWallDict['height']):
  153. dstWallDict[(x + left, y + top)] = srcWallDict[(x, y)]
  154. return dstWallDict
  155.  
  156. # A
  157. #BCD
  158. #E@F
  159. def makeWallDict(maze, playerx, playery, playerDirection, exitx, exity):
  160. section = {}
  161. if playerDirection == NORTH:
  162. # Map of the sections, relative A
  163. # to the player @: BCD
  164. # E@F
  165. offsets = (('A', 0, -2), ('B', -1, -1), ('C', 0, -1), ('D', 1, -1), ('E', -1, 0), ('F', 1, 0))
  166. if playerDirection == SOUTH:
  167. # Map of the sections, relative F@E
  168. # to the player @: DCB
  169. # A
  170. offsets = (('A', 0, 2), ('B', 1, 1), ('C', 0, 1), ('D', -1, 1), ('E', 1, 0), ('F', -1, 0))
  171. if playerDirection == EAST:
  172. # Map of the sections, relative EB
  173. # to the player @: @CA
  174. # FD
  175. offsets = (('A', 2, 0), ('B', 1, -1), ('C', 1, 0), ('D', 1, 1), ('E', 0, -1), ('F', 0, 1))
  176. if playerDirection == WEST:
  177. # Map of the sections, relative DF
  178. # to the player @: AC@
  179. # BE
  180. offsets = (('A', -2, 0), ('B', -1, 1), ('C', -1, 0), ('D', -1, -1), ('E', 0, 1), ('F', 0, -1))
  181.  
  182. for sec, xOffset, yOffset in offsets:
  183. section[sec] = maze.get((playerx + xOffset, playery + yOffset), WALL)
  184.  
  185. wallDict = copy.copy(ALL_OPEN)
  186. PASTE_CLOSED_TO = {'A': (6, 4), 'B': (4, 3), 'C': (3, 1), 'D': (10, 3), 'E': (0, 0), 'F': (12, 0)}
  187. for sec in 'ABDCEF':
  188. if section[sec] == WALL:
  189. wallDict = pasteWallDict(CLOSED[sec], wallDict, PASTE_CLOSED_TO[sec][0], PASTE_CLOSED_TO[sec][1])
  190.  
  191. '''
  192. displayWallDict(pasteWallDict(CLOSED['A'], ALL_OPEN, 6, 4))
  193. displayWallDict(pasteWallDict(CLOSED['B'], ALL_OPEN, 4, 3))
  194. displayWallDict(pasteWallDict(CLOSED['C'], ALL_OPEN, 3, 1))
  195. displayWallDict(pasteWallDict(CLOSED['D'], ALL_OPEN, 10, 3))
  196. displayWallDict(pasteWallDict(CLOSED['E'], ALL_OPEN, 0, 0))
  197. displayWallDict(pasteWallDict(CLOSED['F'], ALL_OPEN, 12, 0))
  198. '''
  199. sys.exit()
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. print('MAZE RUNNER 3D')
  207. print('By Al Sweigart al@inventwithpython.com')
  208. print()
  209. print('(Maze files are generated by mazemaker.py)')
  210.  
  211. # Get the maze file's filename from the user:
  212. while True:
  213. print('Enter the filename of the maze (or "quit"):')
  214. filename = input()
  215.  
  216. if filename.upper() == 'QUIT':
  217. sys.exit()
  218.  
  219. if os.path.exists(filename):
  220. break
  221. print('There is no file named', filename)
  222.  
  223. # Load the maze from a file:
  224. mazeFile = open(filename)
  225. maze = {}
  226. lines = mazeFile.readlines()
  227. playerx = None
  228. playery = None
  229. exitx = None
  230. exity = None
  231. y = 0
  232. for line in lines:
  233. WIDTH = len(line.rstrip())
  234. for x, character in enumerate(line.rstrip()):
  235. assert character in (WALL, EMPTY, START, EXIT), 'Invalid character at column %s, line %s' % (x + 1, y + 1)
  236. if character in (WALL, EMPTY):
  237. maze[(x, y)] = character
  238. elif character == START:
  239. playerx, playery = x, y
  240. maze[(x, y)] = EMPTY
  241. elif character == EXIT:
  242. exitx, exity = x, y
  243. maze[(x, y)] = EMPTY
  244. y += 1
  245. HEIGHT = y
  246.  
  247. assert playerx != None and playery != None, 'Missing start point in maze file.'
  248. assert exitx != None and exity != None, 'Missing exit point in maze file.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement