赞
踩
将turtle.speed()设置为任何值,包括random.randint()的结果,既不必要也不充分。很方便。下面是我的极简主义海龟比赛代码。turtle.speed()方法是用来隐藏海龟的速度的,但是如果你改变了:turtle.forward(turtle.speed() + 1)
固定值:
^{pr2}$
您将看到,无论turtle.speed()的设置如何,海龟都以相同的速度移动。turtle.speed()值只决定海龟获取更新的速度。最终,turtle.forward()参数的差异控制着向前运动的量:from random import randint
from turtle import Turtle, Screen
TURTLE_SIZE = 20
MAXIMUM_SPEED = 10
DEFAULTS = {'shape':"turtle", 'visible': False}
screen = Screen()
starting_line = TURTLE_SIZE - screen.window_width()/2
finish_line = screen.window_width()/2 - TURTLE_SIZE - MAXIMUM_SPEED
turtles = dict(red=Turtle(**DEFAULTS), green=Turtle(**DEFAULTS), blue=Turtle(**DEFAULTS))
for i, (color, turtle) in enumerate(turtles.items()):
turtle.color(color)
turtle.penup()
turtle.goto(starting_line, i * TURTLE_SIZE)
turtle.speed(randint(0, MAXIMUM_SPEED))
turtle.showturtle()
racing = True
while racing:
for turtle in turtles.values():
turtle.forward(turtle.speed() + 1)
if turtle.xcor() >= finish_line:
racing = False
break
screen.exitonclick()
为了让乌龟比赛变得更加复杂,您可能需要使用ontimer()事件来允许它们更独立地移动。在
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。