python - 弹跳海龟功能但不运行
问题描述
我正在为学校编写代码,其中乌龟在广场上弹跳。到目前为止它不可见,我打算改变它。问题是当我运行代码时,什么也没有发生,我不知道为什么。欢迎任何帮助。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()
解决方案
弹跳海龟功能但不运行
所以乌龟“不跑”,“不可见”和“什么都没有发生”,但它“起作用”?真的吗?
修复错误的缩进行,导致代码甚至无法启动,它没有做任何事情的原因是你编写了代码,当它“通过边界”时,但没有写任何东西让它做其他事情。如果它根本不动,它就不能徘徊在它的边界之外。
让我们简化您的代码,看看我们是否不能让海龟基本移动:
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
推荐阅读
- c - C - 为什么将元素分配给动态分配数组的第一个元素总是不返回任何内容
- python-3.x - 打开字节流作为图像文件以访问 exif。'UnicodeDecodeError:'utf-8' 编解码器无法解码位置 0 中的字节 0xff:无效起始字节'
- python - 我正在尝试使用 selenium 加载等待列表并单击按钮,但我似乎找不到该元素
- javascript - qgis2web(传单) - 使用 javascript 切换“层列表”中的复选框
- c++ - 标准库或编译器在哪里利用 noexcept 移动语义(向量增长除外)?
- node.js - 根据部分路径的动态键映射获取json值
- python - 如何将其转换为列表或 numpy 数组
- java - Jackson.databind 异常的 Weblogic 部署问题
- c# - 在 Win 表单上出现错误“拒绝访问”
- vue.js - Nuxt.js:我可以使用基于布尔值的三元运算符添加一个类吗?