最近闲的无聊(为啥每次开头都是这个?我也不知道),搞了个迷宫
首先要创建迷宫文件
(就是开头是maze的TXT文件,如maze73.txt)
你可以自己设计(#号是墙壁,S入口,E出口)
如:
###########
#S# # # ##### # # E#
###########
注意:1.要用Courier等等宽字体,不能用Tahoma之类的不等宽(M和i不一样大)
2.要是奇数行和奇数列
这样就不行:
######
#S#
####E# #####
#(错误示范!!!)
也可以从 https://wwrv.lanzouh.com/iAJvA0nxdxng
下载创建好的迷宫
代码:
"""Maze Runner 2D Move around a maze and try to escape. """ import sys, os # Maze file constants: WALL = '#' EMPTY = ' ' START = 'S' EXIT = 'E' PLAYER = '@' BLOCK = chr(9617) # Character 9617 is '░' def displayMaze(maze): # Display the maze: for y in range(HEIGHT): for x in range(WIDTH): if (x, y) == (playerx, playery): print(PLAYER, end='') elif (x, y) == (exitx, exity): print('X', end='') elif maze[(x, y)] == WALL: print(BLOCK, end='') else: print(maze[(x, y)], end='') print() # Print a newline after printing the row. print('''Maze Runner 2D ''') # Get the maze file's filename from the user: while True: print('Enter the filename of the maze (or LIST or QUIT):') filename = input('> ') # List all the maze files in the current folder: if filename.upper() == 'LIST': print('Maze files found in', os.getcwd()) for fileInCurrentFolder in os.listdir(): if (fileInCurrentFolder.startswith('maze') and fileInCurrentFolder.endswith('.txt')): print(' ', fileInCurrentFolder) continue 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 {}, line {}'.format(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, 'No start in maze file.' assert exitx != None and exity != None, 'No exit in maze file.' while True: # Main game loop. displayMaze(maze) while True: # Get user move. print(' W') print('Enter direction, or QUIT: ASD') move = input('> ').upper() if move == 'QUIT': print('Thanks for playing!') sys.exit() if move not in ['W', 'A', 'S', 'D']: print('Invalid direction. Enter one of W, A, S, or D.') continue # Check if the player can move in that direction: if move == 'W' and maze[(playerx, playery - 1)] == EMPTY: break elif move == 'S' and maze[(playerx, playery + 1)] == EMPTY: break elif move == 'A' and maze[(playerx - 1, playery)] == EMPTY: break elif move == 'D' and maze[(playerx + 1, playery)] == EMPTY: break print('You cannot move in that direction.') # Keep moving in this direction until you encounter a branch point. if move == 'W': while True: playery -= 1 if (playerx, playery) == (exitx, exity): break if maze[(playerx, playery - 1)] == WALL: break # Break if we've hit a wall. if (maze[(playerx - 1, playery)] == EMPTY or maze[(playerx + 1, playery)] == EMPTY): break # Break if we've reached a branch point. elif move == 'S': while True: playery += 1 if (playerx, playery) == (exitx, exity): break if maze[(playerx, playery + 1)] == WALL: break # Break if we've hit a wall. if (maze[(playerx - 1, playery)] == EMPTY or maze[(playerx + 1, playery)] == EMPTY): break # Break if we've reached a branch point. elif move == 'A': while True: playerx -= 1 if (playerx, playery) == (exitx, exity): break if maze[(playerx - 1, playery)] == WALL: break # Break if we've hit a wall. if (maze[(playerx, playery - 1)] == EMPTY or maze[(playerx, playery + 1)] == EMPTY): break # Break if we've reached a branch point. elif move == 'D': while True: playerx += 1 if (playerx, playery) == (exitx, exity): break if maze[(playerx + 1, playery)] == WALL: break # Break if we've hit a wall. if (maze[(playerx, playery - 1)] == EMPTY or maze[(playerx, playery + 1)] == EMPTY): break # Break if we've reached a branch point. if (playerx, playery) == (exitx, exity): displayMaze(maze) print('You have reached the exit! Good job!') print('Thanks for playing!') sys.exit()
也可以从https://wwrv.lanzouh.com/ix66I0nxenrg下载源码
Tnank you!
本站作者已申明原创,禁止转载!
文章内容属作者个人观点,不代表本站立场,如有侵权立删。