【问题标题】:Getting Turtle to Bounce Off the Walls让乌龟从墙上弹起
【发布时间】:2013-10-20 20:02:50
【问题描述】:

我正在为某人编写程序,以便他们可以锻炼眼睛,而我正在使用乌龟,因此它会随机移动。 我想让乌龟从墙上弹开,这样它就不会从侧面消失,再也见不到了! 我看过: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()

【问题讨论】:

  • 除了@lejlot 的回答之外,你的程序最终肯定会崩溃,因为它包含一个无限递归(你调用main,它调用edge,它再次调用main 等等)。
  • 我知道了,我要细化代码,所以我真正完成项目时它不会调用这么多函数
  • 如果您不使用from &lt;module&gt; import *,问题的原因可能会更加明显。

标签: python int turtle-graphics


【解决方案1】:

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

left = -right

所以当你试图转动你的乌龟时

left(f)

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

【讨论】:

  • 感谢您的回答,程序现在可以运行了,但是乌龟并没有反弹,它只是永远进行下去!
  • BartoszKP 指出了为了完成这项工作必须解决的重要逻辑错误 - 您的多级递归不仅仅是关于效率,它只是不正确的代码。我的回答只解决语法问题,而不是概念错误
  • 哦!我才意识到我有多愚蠢!这段代码永远不会起作用,因为它只执行forward(a),然后调用edge(),后者调用main,后者运行forward(a),然后再次调用edge()!我在想什么?!
  • 没错,在解决了这个问题和我的回答中列出的错误之后,一切都应该恢复正常(在删除那些真正不正确的重复出现之后,不仅仅是效率低下)
  • 我现在已经完善了我的代码。我已经在问题中对其进行了编辑。
猜你喜欢
  • 1970-01-01
  • 2019-07-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多