码二代 > 新闻中心 > 新闻资讯 > 正文
新闻中心
最新发布
新闻资讯

Python2D迷宫|少儿编程网

作者: 来源:转载 日期:2023/2/22 10:10:20 23次阅读 评论:0
Python2D迷宫 I love Scratch   编程乐园   申明原创   2023-02-18 11:43 最近闲的无聊(为啥每次开头都是这个?我也不知道),搞了个迷宫 首先要创建迷宫文件 (就是开头是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 '░'

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!

本站作者已申明原创,禁止转载!

文章内容属作者个人观点,不代表本站立场,如有侵权立删。


QQ行业群:202818485 434219048

微信公众号:kidscode_cn,二维码 

    标签:
    评论信息
    我要评论

    还没有找到合适的少儿编程培训机构?联系我们,帮您找 靠谱 的儿童学习编程机构,省事省力又省钱!

    小学编程公众号