TurtleGraphics Python: Bouncing turtle off the walls?

我怕爱的太早我们不能终老 提交于 2019-12-02 03:26:50

Try something like this:

if not (left <= x <= right):
    turtle.left(180 - 2 * turtle.heading())
elif not (bottom <= y <= top):
    turtle.left(-2 * turtle.heading())
else:
    pass

My python syntax is a little rusty, sorry :P. But the math is a little different for a horizontal vs. a vertical flip.

EDIT:

I suspect that what is happening is your turtle is getting into a situation where it is pointing upwards and stuck above the top wall. That would lead it to just flip indefinitely. You could try adding the following conditions:

if (x <= left and 90 <= turtle.heading() <= 270) or (right <= x and not 90 <= turtle.heading() <= 270):
    turtle.left(180 - 2 * turtle.heading())
elif (y <= bottom and turtle.heading() >= 180) or (top <= y and turtle.heading <= 180):
    turtle.left(-2 * turtle.heading())
else:
    pass

If that works, there is probably a bug elsewhere in your code. Edge handling is tricky to get right. I assume that turtle.heading() will always return something between 0 and 360 - if not then it will be even more tricky to get right.

Gday,

Your problem seems to be that you are using the same trigonometry to calculate the right and left walls, as you are the top and bottom. A piece of paper and a pencil should suffice to calculate the required deflections.

def inbounds(limit, value):
    'returns boolean answer to question "is turtle position within my axis limits"'
    return -limit < value * 2 < limit

def bounce(num_steps, step_size, initial_heading):
    '''given the number of steps, the size of the steps 
        and an initial heading in degrees, plot the resultant course
        on a turtle window, taking into account elastic collisions 
        with window borders.
    '''

    turtle.reset()
    height = turtle.window_height()
    width = turtle.window_width()
    turtle.left(initial_heading)

    for step in xrange(num_steps):
        turtle.forward(step_size)
        x, y = turtle.position()

        if not inbounds(height, y):
            turtle.setheading(-turtle.heading())

        if not inbounds(width, x):
            turtle.setheading(180 - turtle.heading())

I've used the setheading function and a helper function (inbounds) to further declare the intent of the code here. Providing some kind of doc-string is also good practice in any code that you write (provided the message it states is accurate!!)

Your mileage may vary on the use of xrange, Python 3.0+ renames it to simply range.

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