while当另一只乌龟有 50 个单位时,如何使用循环停止随机移动的乌龟?
我有一只乌龟随机选择一个位置并创建一个大点或洞,另一只乌龟随机移动 90 度转弯并每次向前移动 50 个单位。随机移动的乌龟在离开屏幕末端时停止,但是当乌龟到达另一只乌龟创建的洞时,我如何也让乌龟停止?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
鸿蒙传说
相关分类