1 回答
TA贡献1865条经验 获得超7个赞
您的代码的问题是以下语句:
if dist == 'True':
你不想要引号周围True。虽然这会起作用:
if dist == True:
正确的表达方式是:
if dist is True:
或者更好:
if dist:
否则你的代码似乎工作。下面是利用一些海龟习语和其他代码清理的重写:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
添加回答
举报