python 迷宫边界_用Python绘制迷宫般的线条?
Or you could try turtle.
下面是我在pythonturtle中实现的一个最简单的map follower。(不是迷宫跟随者,因为它不做任何回溯。)这可以让你大致了解如何使用数组来包含迷宫结构来绘制迷宫和穿过迷宫的物体:from turtle import Turtle, Screen
MAP = '''
XXXXXXXXOX
XOOOOOOOOX
XOXXXXXXXX
XOOOOXXXXX
XXXXOXXXXX
XXXXOXXXXX
XXXXOOOOOX
XXXXXXXXOX
XOOOOOOOOX
XOXXXXXXXX
'''
MAP_ARRAY = [list(row) for row in MAP.strip().split('\n')]
MAP_ARRAY.reverse() # put 0, 0 in lower left corner
ADJACENT = [
(0, 1),
(-1, 0), (1, 0),
(0, -1),
]
SCALE = 3
STAMP_SIZE = 20
WIDTH, HEIGHT = len(MAP_ARRAY[0]), len(MAP_ARRAY)
def any_adjacent(x, y):
return [(x + dx, y + dy) for dx, dy in ADJACENT if 0 <= x + dx < WIDTH and 0 <= y + dy < HEIGHT and MAP_ARRAY[y + dy][x + dx] == 'O']
def move(): # slowly navigate the MAP, quit when no where new to go
x, y = turtle.position()
adjacent_squares = any_adjacent(int(x), int(y))
# always moves to first valid adjacent square, need to consider
# how to deal with forks in the road (e.g. shuffle adjacent_squares)
for adjacent in adjacent_squares:
if adjacent not in been_there:
turtle.goto(adjacent)
been_there.append(adjacent)
screen.ontimer(move, 1000) # one second per move, adjust as needed
break
screen = Screen() # recast the screen into MAP coordinates
screen.setup(WIDTH * STAMP_SIZE * SCALE, HEIGHT * STAMP_SIZE * SCALE)
screen.setworldcoordinates(-0.5, -0.5, WIDTH - 0.5, HEIGHT - 0.5)
turtle = Turtle('square', visible=False)
turtle.shapesize(SCALE)
turtle.speed('fastest')
turtle.penup()
for y, row in enumerate(MAP_ARRAY): # draw the MAP
for x, character in enumerate(row):
if character == 'X':
turtle.goto(x, y)
turtle.stamp()
turtle.color('red')
turtle.shapesize(SCALE / 2)
turtle.goto(1, 0) # should use unique character in MAP to indicate start & end points
turtle.showturtle()
been_there = [] # prevent doubling back
move()
screen.mainloop()
python 迷宫边界_用Python绘制迷宫般的线条?相关推荐
- python 时间序列预测_使用Python进行动手时间序列预测
python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...
- python 概率分布模型_使用python的概率模型进行公司估值
python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...
- 用python实现点阵屏_用Python代码来绘制彭罗斯点阵的教程
这里是显示彭罗斯点阵的Python的脚本.是的,这是可以运行的有效Phython代码. 译注:彭罗斯点阵,物理学术语.上世纪70年代英国数学家彭罗斯第一次提出了这个概念,称为彭罗斯点阵(Pen-ros ...
- python主程序流程图_用Python编程绘制流程图,你用过吗?
您一定听说过 "Graphviz"绘图软件吧.Graphviz (Graph Visualization Software) 是一个由AT&T实验室启动的开源工具包,它采用 ...
- 数据结构迷宫代码_数据结构课程设计——迷宫求解(二)
前言 接上文的介绍,本文将主要介绍如何生成随机迷宫,在网上找到的资源也比较多,这里我选取了随机 Prim 算法生成迷宫,选择这个算法的理由如下: 算法思想简单,易于实现 生成的迷宫比较自然,不会出现明 ...
- 使用python预测基金_使用python先知3 1创建预测
使用python预测基金 This tutorial was created to democratize data science for business users (i.e., minimiz ...
- python集群_使用Python集群文档
python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...
- python 网页编程_通过Python编程检索网页
python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...
- python机器学习预测_使用Python和机器学习预测未来的股市趋势
python机器学习预测 Note from Towards Data Science's editors: While we allow independent authors to publish ...
最新文章
- vi 环境,跳转函数定义
- jq的ajax和模块引擎
- Oracle感慨(转)
- void *变量用法
- 将同时共享的用户数量限制为20_共享充电宝市场需求及计划
- shell按照时间排序_【经典排序】希尔排序
- Kerberos KDC not reachable
- 集成学习:Boosting与Bagging
- php array_walk和array_map区别
- NERO-光盘刻录程序教程集
- android studio 登录与注册,Android Studio实现QQ的注册登录和好友列表跳转
- win10子系统安装php,win10 ubuntu 子系统安装php
- ASP.net和ASP的区别
- Flash和JS实现的图片幻灯片切换特效
- Android多维商品属性SKU选择
- 西门子 延时 光控 声控开关 5TG0 211 -1CC1 提高 灵敏度 的方法
- python axes_对Numpy中轴(axes)的解释(汉化版)
- java实现简单压缩与解压缩功能
- Java实现 LeetCode 113 路径总和 II
- GameStop的下半场:散户疯狂复仇,大空头彻底投降,但“世纪逼空大战”远未结束...