Python 弹跳龟功能正常,但不';跑不动
我正在为一只乌龟在广场上蹦蹦跳跳的学校做一个代码。到目前为止还看不到,我打算改变。问题是当我运行代码时,什么都没有发生,我也不知道为什么。欢迎任何帮助。xxturt和yyturt是一种试图在海龟通过边境时使其后退的方法Python 弹跳龟功能正常,但不';跑不动,python,turtle-graphics,Python,Turtle Graphics,我正在为一只乌龟在广场上蹦蹦跳跳的学校做一个代码。到目前为止还看不到,我打算改变。问题是当我运行代码时,什么都没有发生,我也不知道为什么。欢迎任何帮助。xxturt和yyturt是一种试图在海龟通过边境时使其后退的方法 import turtle import random turt=turtle.Turtle() while True: xturt = turt.xcor() yturt = turt.ycor() if abs(xturt) >= 50: hea
import turtle
import random
turt=turtle.Turtle()
while True:
xturt = turt.xcor()
yturt = turt.ycor()
if abs(xturt) >= 50:
heading = turt.heading()
for i in range(1):
rand=random.randint(90,150)
xxturt=xturt-50
turt.back(xxturt)
turt.setheading(rand + heading)
turt.fd(1)
if abs(yturt) >= 50:
heading = turt.heading()
for i in range(1):
rando=random.randint(90,150)
yyturt=yturt-50
turt.back(yyturt)
turt.setheading(rando + heading)
turt.fd(1)
screen.exitonclick()
弹跳乌龟功能正常,但不运行
所以海龟“不跑”,“不可见”和“什么也没发生”,但它“起作用”?真的吗
修复了使代码无法均匀启动的错误缩进行,它不做任何事情的原因是您在代码“通过边界”时编写了代码,但没有为它编写任何其他操作。如果它根本不移动,它就不能游荡到边界之外
让我们简化您的代码,看看是否无法让海龟基本上移动:
from turtle import Screen, Turtle
from random import randint
screen = Screen()
turtle = Turtle('turtle')
while True:
x, y = turtle.position()
if not abs(x) < 100 > abs(y):
turtle.backward(1)
heading = turtle.heading()
rand = 180 + randint(-30, 30)
turtle.setheading(rand + heading)
turtle.forward(1)
screen.exitonclick() # never reached
从海龟导入屏幕,海龟
从随机导入randint
screen=screen()
海龟=海龟(“海龟”)
尽管如此:
x、 y=海龟位置()
如果不是abs(x)<100>abs(y):
乌龟。向后(1)
heading=海龟。heading()
rand=180+randint(-30,30)
乌龟.设置航向(兰德+航向)
乌龟前进(1)
screen.exitonclick()#从未到达
添加一些打印语句,或在代码中的不同位置使用python的日志功能,以查看它在哪里被卡住,以及当时发生了什么。“到目前为止它还不可见,我计划对此进行更改”我建议您先解决这个问题。如果你能看到结果,你就更容易理解你的代码在做什么。对不起,当我说“不可见”时,我指的是海龟被困在的盒子里。谢谢你向我解释一切。