启动乌龟功能,但不运行

问题描述 投票:0回答:1

我正在为学校编写代码,在那儿,一只乌龟在广场上弹跳。到目前为止,它仍然不可见,我打算对其进行更改。问题是当我运行代码时,什么也没有发生,而且我不知道为什么。欢迎任何帮助。 xxturt和yyturt试图使龟退后,以防它越过边界]

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()
python turtle-graphics
1个回答
0
投票

启动乌龟功能,但不运行

因此,乌龟“不运行”,“不可见”并且“什么也没有发生”,但是“起作用”吗?真的吗?

修复不正确的缩进行使代码无法启动,它不执行任何操作的原因是您为代码“越过边界”编写了代码,但没有编写anything除此以外。如果它根本不动,它就不能越过边界。

让我们简化您的代码,看看我们是否无法让乌龟基本移动:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.