我正在编写一个随机行走的海龟程序,我有一个改变速度的问题。如果我定义了海龟(笔)速度,而不设定海龟速度,海龟运行默认速度3。但是,如果我在边圈内设定速度,速度设置就正确。奇怪的是,我可以在没有问题的情况下将海龟的形状设置在时间循环之外。
有什么好主意吗?
卢克
from turtle import Turtle, Screen, left, position, right
import turtle
import random
from time import time
from lib import collision
#print("Enter Number of Pins")
total_pens = 2
pen_list = []
for pen in range(total_pens):
pen = Turtle()
pen.shape("circle")
# pen.speed(0) # I want to set the speed here
pen_list.append(pen)
_screen = Screen()
_screen.title("Random-Walk")
x_max = 100
y_max = 100
turtle.setworldcoordinates(-x_max, -y_max, x_max, y_max)
wall_position = {"top":y_max, "bottom":-y_max, "left": -x_max, "right":x_max}
pencolor_list = ["red", "blue", "green"]
rotation_list = [0, 90, -90]
travel_range = range(0,100,10)
start_time = time()/60
runtime = 1
while True:
for pen in pen_list:
pen.speed(0) # setting the speed only works here.
pen.pencolor(random.choice(pencolor_list))
rotation_direction = random.choice(rotation_list)
if rotation_direction == 90:
pen.left(90)
elif rotation_direction == -90:
pen.right(90)
elif rotation_direction == 0:
pass
turtle_heading = pen.heading()
turtle_position = pen.position()
move_distance = random.choice(travel_range)
travel_length = collision(wall_position, turtle_position, turtle_heading, move_distance)
if travel_length[1] > 0:
pen.forward(travel_length[0])
pen.left(180)
pen.forward(travel_length[1])
else:
pen.forward(travel_length[0])
end_time = time()/60
if (end_time - start_time) > runtime:
break
else:
pass
turtle.done()发布于 2022-02-26 16:28:30
我想我知道发生了什么。"turtle.setworldcoordinates(-x_max,-y_max,x_max,y_max)“语句正以任何理由重置海龟的速度。
发布于 2022-02-26 16:32:46
我将要设置速度的for循环移到while循环之前,它正确地控制速度。我认为这与screen.reset()函数中的turtle.setworldcoordinates()有关。
https://stackoverflow.com/questions/71278189
复制相似问题