gpt4 book ai didi

python - 让 turtle 从墙上弹起来

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:07 25 4
gpt4 key购买 nike

我正在为某人制作一个程序,这样他们就可以锻炼他们的眼睛,我正在使用 turtle ,所以它会随机移动。我希望 turtle 从墙上弹开,这样它就不会从侧面消失,再也不会被看到!我看过: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()

最佳答案

您将 left 命令覆盖为 int 值:

left = -right

所以当你尝试转动 turtle 时

left(f)

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

关于python - 让 turtle 从墙上弹起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com