本文介绍了使用Python绘制/绘制迷宫状线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程新手,我正在尝试绘制类似道路边界的线.基本上,我正在创建路线图之类的东西,我希望矩形始终在轨迹中移动(我的意思是,矩形的x,y坐标相对于直线增加).

New to programming, I am trying to plot road boundary-like lines. Basically, I am creating something like a road map and I want a rectangle to always be moving through the trajectory (I mean, the x, y coordinates of the rectangle increase with relation to the lines).

这个想法就像下面的图片.

The idea is like the image below.

有人可以帮助我吗?如何创建和绘制沿箭头方向移动的红线和黑矩形?

Can anyone help me? How do I create and plot the red lines as well as the black rectangle which is moving in the direction of the arrows?

更新:我需要每次检查从矩形到线条的距离是否在某个阈值以下.我想我可能需要使用某种数组/元素.但是我不清楚如何使用它.谁能帮我?谢谢.

UPDATE: I need to check that the distance from the rectangle to the lines is under a certain threshold each time. I think I might need to use some kind of array/element. But I am not clear how to use it. Can anyone help me? Thanks.

推荐答案

下面是我在Python turtle中实现的极简地图关注者.(不是迷宫追随者,因为它不会进行任何回溯.)这应该给您一个大概的思路,如何使用数组包含迷宫结构来绘制迷宫和在其中移动的对象:

Below's a minimalist map follower I implemented in Python turtle. (Not a maze follower as it doesn't do any backtracking.) This should give you a rough idea how to plot a maze and an object moving through it using an array to contain the maze structure:

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绘制/绘制迷宫状线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:19