Python:如何让乌龟永远不会越过一条线 [英] Python: how to make a turtle never cross over a line

查看:30
本文介绍了Python:如何让乌龟永远不会越过一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 python 程序,它可以让海龟向前移动,向左转然后移动,或向右转然后随机移动,而不会越过自己之前绘制的线.它必须留在屏幕内并在完成最多可能的移动后清除并重新启动.这是我得到的:

I'm trying to write a python program that will make a turtle either move forward, turn left and then move, or turn right and then move randomly without ever crossing over its own previously drawn lines. It must stay within the screen and clear and restart once it has done the most possible moves. This is as far as I got:

import turtle
positions=[]

while 'x':
    count=132
    for x in range (132):
        count-=1
        print(count)
        move = randint(1,3)

        if move == 1:
            turtle.fd(50)
        if move == 2:
            turtle.rt(90)
            turtle.fd(50)
        if move == 3:
            turtle.lt(90)
            turtle.fd(50)

        if turtle.xcor()>290:
            turtle.rt(180)
            turtle.fd(50)
        if turtle.xcor()<-290:
            turtle.rt(180)
            turtle.fd(50)
        if turtle.ycor()>240:
            turtle.rt(180)
            turtle.fd(50)
        if turtle.ycor()<-240:
            turtle.rt(180)
            turtle.fd(50)
    turtle.clear()

我怎样才能让它记住它的位置而不是越过它们?非常感谢!

How can I make it remember its positions and not go over them? Many thanks!

推荐答案

以下是我解决您的自主乌龟问题的尝试.我选择了一个 set() 来跟踪访问过的位置,但也将海龟强制到一个网格上,以确保只能访问有限的一组点.我不喜欢你在撞墙时做 180 度的方法,因为这只会让你折回你的路径并失败——相反,我的乌龟试图避免撞墙.

Below is my attempt to solve your autonomous turtle problem. I chose a set() to track visited positions but also coerced the turtle onto a grid to make sure that only a limited set of points could be visited. I didn't like your approach of doing a 180 when you hit a wall as that just makes you retrace your path and fail -- instead my turtle tries to avoid hitting the walls.

我使用一个隐形的海龟克隆来试水",看看一个动作是好是坏.如果没有好的动作,它就会放弃,重置,然后重新开始.您必须关闭窗口才能终止程序:

I use an invisible turtle clone to "test the waters" to see if a move is good or bad. If there are no good moves, it gives up, resets, and starts again. You have to close the window to kill the program:

import turtle
from random import shuffle

WIDTH, HEIGHT = 600, 500

INCREMENT = 50

TURTLE_WIDTH = 20

X, Y = 0, 1

def dot_round(x, base=INCREMENT):
    return int(base * round(float(x) / base))

turtle.setup(WIDTH, HEIGHT)

turtle.shape("turtle")

while True:
    positions = set()

    while True:

        position = (dot_round(turtle.xcor()), dot_round(turtle.ycor()))  # coerce position to grid

        if position in positions:
            break  # collision with line

        positions.add(position)

        turtle.setposition(position)  # coerce turtle back onto our grid

        moves = list(range(3))

        shuffle(moves)

        clone = None

        for move in moves:
            clone = turtle.clone()  # use an invisible clone to test the waters
            clone.hideturtle()
            clone.penup()

            if move == 1:
                clone.right(90)
            elif move == 2:
                clone.left(90)

            clone.forward(INCREMENT)

            position = (dot_round(clone.xcor()), dot_round(clone.ycor()))

            if position[X] <= TURTLE_WIDTH//2 - WIDTH//2 or position[X] > WIDTH//2 - TURTLE_WIDTH//2:
                continue  # avoid collision with wall

            if position[Y] <= TURTLE_WIDTH//2 - HEIGHT//2 or position[Y] > HEIGHT//2 - TURTLE_WIDTH//2:
                continue  # avoid collision with wall

            if position not in positions:
                break

        else:  # no break
            break  # accept the inevitable, there's no good move

        turtle.setheading(clone.heading())
        turtle.forward(INCREMENT)

    turtle.reset()

# close the turtle window to get out of this program

这只乌龟只向前看一步——他很容易陷入无法摆脱的困境.

This turtle only looks one move ahead -- he easily gets himself into jams he can't get himself out of.

这希望能给你一些关于如何设计你自己的海龟自动机的想法.

This hopefully gives you some ideas how to design your own turtle automaton.

这篇关于Python:如何让乌龟永远不会越过一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆