创建了一个程序来随机化海龟的移动,但是一旦它碰到一个边界(窗口),就不能让它打破循环。尝试了一些类似问题的解决方案,但仍然没有成功。你知道吗
from turtle import Turtle, Screen
import random
def createTurtle(color, width):
tempName = Turtle("turtle")
tempName.speed("fastest")
tempName.color(color)
tempName.width(width)
return tempName
def inScreen(screen, turt):
min_x, max_x = -wn.window_width() / 2 , wn.window_width() / 2
min_y, max_y = -wn.window_height() / 2 , wn.window_height() / 2
turtleX, turtleY = turt.pos()
while (min_x < turtleX < max_x) and (min_y < turtleY < max_y):
turt.left(random.randrange(360))
turt.fd(random.randrange(100))
turtleX, turtleY = turt.pos()
print(turtleX, ",", turtleY)
wn = Screen()
alpha = createTurtle("red", 3)
inScreen(wn, alpha)
wn.exitonclick()
变量turtleX和turtleY正在while条件中使用,但是在循环中永远不会重新评估它们。你知道吗
这就是为什么print函数只输出
0.0 , 0.0
相关问题 更多 >
编程相关推荐