Getting Turtle to Bounce Off the Walls

被刻印的时光 ゝ 提交于 2019-12-11 04:18:13

问题


I am making a program for somebody, so they can exercise their eyes, and I am using the turtle, so it will move randomly. I want the turtle to bounce off the walls, so that it does not disappear off the sides, never to be seen again! I have looked at: TurtleGraphics Python: Bouncing turtle off the walls? and used some code from jnylen, so some credit goes to him! Anyway, here is my old, and terrible code (This is kept for reference):

from random import *
from turtle import *
penup() # Get rid of those lines it leaves behind!
def main():
    while True: # I want this to be infinite
        a = randint(1,600)
        print a
        b = randint(1,359)
        print b
        c = randint(1,600)
        print c
        d = randint(1,359)
        print d #Checking my randoms
        forward(a)
        edge() # Keep calling edge, so it will bounce on the edge.
        left(b)
        edge() # Keep calling edge, so it will bounce on the edge.
        forward(c)
        edge() # Keep calling edge, so it will bounce on the edge.
        right(d)
        edge() # Keep calling edge, so it will bounce on the edge.

def edge():
    x, y = position()
    top = window_height()/2
    bottom = -top
    right = window_width()/2
    left = -right
    if (x <= left and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
        f = 178 * h
        left(f)
        main()
    elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
        f = -2 * heading()
        left(f)
        main()
    else:
        main()
main()

However, When I run it, I get the output:

189
199
553
152
26
175
597
263
119
201
582
329
231
267
344
28

Traceback (most recent call last):
  File "C:/Users/George/Desktop/Eyes.py", line 39, in <module>
    main()
  File "C:/Users/George/Desktop/Eyes.py", line 15, in main
    edge()
  File "C:/Users/George/Desktop/Eyes.py", line 38, in edge
    main()
  File "C:/Users/George/Desktop/Eyes.py", line 15, in main
    edge()
  File "C:/Users/George/Desktop/Eyes.py", line 38, in edge
    main()
  File "C:/Users/George/Desktop/Eyes.py", line 15, in main
    edge()
  File "C:/Users/George/Desktop/Eyes.py", line 38, in edge
    main()
  File "C:/Users/George/Desktop/Eyes.py", line 15, in main
    edge()
  File "C:/Users/George/Desktop/Eyes.py", line 31, in edge
    left(f)
TypeError: 'int' object is not callable

Please, can somebody help me fix this. I have tried setting f to a float, a string and an int, but nothing works! This is probably something really obvious, so if it is, sorry.

Here is my new, and hopefully, correct code:

from random import *
from turtle import *
penup() # Get rid of those lines it leaves behind!
def main():
    while True: # I want this to be infinite
        a = randint(1,600)
        print a
        b = randint(1,359)
        print b
        c = randint(1,600)
        print c
        d = randint(1,359)
        print d #Checking my randoms
        forward(a)

        x, y = position()
        top = window_height()/2
        bottom = -top
        right = window_width()/2
        l = -right

        if (x <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
            f = 178 * heading()
            left(f)
            print "1"
        elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
            f = -2 * heading()
            left(f)
            print "2"
        else:
            print "3"

        left(b)

        x, y = position()
        top = window_height()/2
        bottom = -top
        right = window_width()/2
        l = -right
        if (x <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
            f = 178 * heading()
            left(f)
            print "1"
        elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
            f = -2 * heading()
            left(f)
            print "2"
        else:
            print "3"

        forward(c)

        x, y = position()
        top = window_height()/2
        bottom = -top
        right = window_width()/2
        l = -right
        if (x <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
            f = 178 * heading()
            left(f)
            print "1"
        elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
            f = -2 * heading()
            left(f)
            print "2"
        else:
            print "3"


        right(d)

        x, y = position()
        top = window_height()/2
        bottom = -top
        right = window_width()/2
        l = -right
        if (x <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
            f = 178 * heading()
            left(f)
            print "1"
        elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
            f = -2 * heading()
            left(f)
            print "2"
        else:
            print "3"


main()

回答1:


You overwritten the left command to the int value here:

left = -right

so when you try to turn your turtle with

left(f)

you try to call the int object, simply rename your local variable left



来源:https://stackoverflow.com/questions/19482196/getting-turtle-to-bounce-off-the-walls

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