简体   繁体   中英

Python while statement turtle.distance

It is my understanding that the following code

turtle.forward(50)

means moving the turtle on the screen.

Here is a code I am trying to learn:

def forward(distance):
while distance > 0:
    if turtle.distance(0,0) > 100:
        angle = turtle.towards(0,0)
        turtle.setheading(angle)
    turtle.forward(1)
    distance = distance - 1

I don't really understand how

turtle.forward(1)
distance = distance -1

works here. I understand that if the distance is greater than 100, then the turtle turns back and moves from the end position to the position less than 100. I experimented with it but it is still not clear to me what the code turtle.forward(1) means. Is the turtle supposed to move by 1 pixel? And the final distance is less by another pixel? When I put in different numbers instead of 1, I get strange results. Sorry for asking this question - I am learning very slowly. Thanks.

turtle.forward(1) is telling the turtle to move forward in the current direction by one pixel.

if turtle.distance(0,0) > 100 checks how far away the turtle is from the origin (center of the pane) and see if it's greater than 100.

angle = turtle.towards(0,0) turtle.setheading(angle) turns the turtle towards the origin

Here's how it comes together:

def forward(distance):
    while distance > 0: # loop this while there's still distance 
        if turtle.distance(0,0) > 100: # check if the distance from the origin is greater that 100
            angle = turtle.towards(0,0) # find the angle for the origin from turtle's current location 
            turtle.setheading(angle) # turns the turtle toward the angle
        turtle.forward(1) # move title forward in the current angle by one pixel 
        distance = distance - 1 # act as a counter for the while loop, so when distance is finally 0, the while loop breaks

The code is kind of contradictory to what it tries to achieve. It looks like the forward function is trying to move towards position (0, 0) in distance number of steps.

def forward(distance):

    while distance > 0:               

        # if the turtle is more than 100 distance away from (0, 0)
        # change orientation to face (0, 0)               
        if turtle.distance(0,0) > 100:
            angle = turtle.towards(0,0)
            turtle.setheading(angle)

        # Make the turtle move one step towards (0, 0)
        turtle.forward(1)

        # IMO, distance should've been called steps_left 
        distance = distance - 1

LOGICAL INCONSISTENCY

The code seems like it is trying to make the turtle move towards the origin (0, 0) BUT at the same time it has a counter of sorts called distance - that acts more like steps_allowed_to_take - which might cause the turtle to reach the origin but still attempt to move towards the origin. The turtle.forward(1) in this scenario would actually cause the turtle to move one step away from the origin and in the next loop, the turtle would move back to the origin. Doesn't make sense.

This function is a replacement for turtle.forward() that keeps a turtle that's on a random walk within a circular boundary.

Consider a simplified version of the function:

def forward(distance):
    while distance > 0:
        turtle.forward(1)
        distance = distance - 1

It does exactly what turtle.forward(distance) does, though less efficiently. It moves the turtle forward one pixel at a time until it goes the entire distance, rather then go the whole distance in one motion via turtle.forward(distance) . Why? This allows the code to make a decision on each pixel of the move regarding whether the turtle has violated the boundary and adjust its heading:

    if turtle.distance(0, 0) > 100:
        angle = turtle.towards(0, 0)
        turtle.setheading(angle)

Let's embed this function in code to illustrate what it does:

from random import randint
import turtle

def forward(distance):
    while distance > 0:
        if turtle.distance(0, 0) > 100:
            angle = turtle.towards(0, 0)
            turtle.setheading(angle)
        turtle.forward(1)
        distance = distance - 1

boundary = turtle.Turtle(visible=False)
boundary.color("red")
boundary.penup()
boundary.sety(-100)
boundary.pendown()
boundary.circle(100)

turtle.shape("turtle")  # default turtle

for _ in range(1000):  # akin to while True:
    turtle.left(randint(0, 90))
    forward(randint(1, 50))

turtle.done()

OUTPUT

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM