让乌龟从墙上反弹

时间:2013-10-20 20:02:51

标签: python int turtle-graphics

我正在为某人制作节目,所以他们可以锻炼自己的眼睛,而我正在使用乌龟,所以它会随机移动。 我希望乌龟从墙上弹开,这样它就不会从侧面消失,永远不会再被看见! 我查看过:TurtleGraphics Python: Bouncing turtle off the walls?并使用了jnylen的一些代码,所以有些功劳归功于他! 无论如何,这是我的旧的,可怕的代码(这是为了参考):

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()

然而,当我运行它时,我得到输出:

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

拜托,有人可以帮我解决这个问题。我尝试将f设置为floatstringint,但无效!这可能是非常明显的事情,所以如果是,抱歉。

这是我新的,希望正确的代码:

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 个答案:

答案 0 :(得分:1)

您在此处将left命令覆盖为int值:

left = -right

所以当你试图用

转动你的乌龟
left(f)

您尝试调用int对象,只需重命名您的本地变量left