Plot /draw maze-like lines with Python?

假如想象 提交于 2021-01-29 10:27:24

问题


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.


回答1:


You can use pygames and literally draw anything anyway. Since you have not started on it at all, try this tutorial here

Or you could try turtle.




回答2:


Or you could try 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()



来源:https://stackoverflow.com/questions/51488045/plot-draw-maze-like-lines-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!