如何让所有的海龟同时移动
How to make all the turtles move simultaneously
我想同时移动所有的海龟,我希望能够创造 100 只海龟。我必须在代码中分别创建每一个,因此创建 100 个或更多将花费很长时间。我想要一种能够设置我想要的海龟数量的方法——一个 100 及以上的数字。我希望他们同时移动。我也想设置边界。任何有关如何执行其中任何一个或所有操作的想法都将不胜感激。
总而言之,我希望能够:
- 设置生成海龟的数量。
- 一次全部搬走,一个都搬不动
每次。
- 设置边界,让它不能去任何地方。
注意:我也知道有人问了几个问题,但没有提供有效的答案。我的代码:
import turtle
import numpy as np
tlist = list()
colorlist = ["red", "green", "black", "blue", "brown"]
for i in range(5):
tlist.append(turtle.Turtle(shape="turtle"))
tlist[i].color(colorlist[i])
tlist[i].speed(1)
screen = turtle.getscreen()
for i in range(30):
for t in tlist:
t.speed(1)
t.right((np.random.rand(1) - .5) * 180)
t.forward(int((np.random.rand(1) - .5) * 100))
screen.update()
你不需要"one smart person in here"来解决这个问题,你需要自己花更多的时间在SO上搜索海龟示例的财富。但是,我将把它作为个人挑战来编写极简代码,让 100 只海龟进行有界随机运动:
from turtle import Screen, Turtle
from random import randint
WIDTH, HEIGHT = 600, 600
CURSOR_SIZE, TURTLE_SIZE = 20, 10
TURTLES = 100
def move():
for turtle in screen.turtles():
turtle.forward(1)
x, y = turtle.position()
if not (TURTLE_SIZE - WIDTH/2 < x < WIDTH/2 - TURTLE_SIZE and TURTLE_SIZE - HEIGHT/2 < y < HEIGHT/2 - TURTLE_SIZE):
turtle.undo() # undo forward()
turtle.setheading(randint(1, 360)) # change heading for next iteration
screen.update()
screen.ontimer(move)
screen = Screen()
screen.tracer(False)
screen.setup(WIDTH, HEIGHT)
for turtle in range(TURTLES):
turtle = Turtle()
turtle.penup()
turtle.shapesize(TURTLE_SIZE / CURSOR_SIZE)
turtle.shape('turtle')
turtle.setheading(randint(1, 360))
turtle.goto(randint(TURTLE_SIZE - WIDTH/2, WIDTH/2 - TURTLE_SIZE), randint(TURTLE_SIZE - HEIGHT/2, HEIGHT/2 - TURTLE_SIZE))
move()
screen.exitonclick()
我想同时移动所有的海龟,我希望能够创造 100 只海龟。我必须在代码中分别创建每一个,因此创建 100 个或更多将花费很长时间。我想要一种能够设置我想要的海龟数量的方法——一个 100 及以上的数字。我希望他们同时移动。我也想设置边界。任何有关如何执行其中任何一个或所有操作的想法都将不胜感激。
总而言之,我希望能够:
- 设置生成海龟的数量。
- 一次全部搬走,一个都搬不动 每次。
- 设置边界,让它不能去任何地方。
注意:我也知道有人问了几个问题,但没有提供有效的答案。我的代码:
import turtle
import numpy as np
tlist = list()
colorlist = ["red", "green", "black", "blue", "brown"]
for i in range(5):
tlist.append(turtle.Turtle(shape="turtle"))
tlist[i].color(colorlist[i])
tlist[i].speed(1)
screen = turtle.getscreen()
for i in range(30):
for t in tlist:
t.speed(1)
t.right((np.random.rand(1) - .5) * 180)
t.forward(int((np.random.rand(1) - .5) * 100))
screen.update()
你不需要"one smart person in here"来解决这个问题,你需要自己花更多的时间在SO上搜索海龟示例的财富。但是,我将把它作为个人挑战来编写极简代码,让 100 只海龟进行有界随机运动:
from turtle import Screen, Turtle
from random import randint
WIDTH, HEIGHT = 600, 600
CURSOR_SIZE, TURTLE_SIZE = 20, 10
TURTLES = 100
def move():
for turtle in screen.turtles():
turtle.forward(1)
x, y = turtle.position()
if not (TURTLE_SIZE - WIDTH/2 < x < WIDTH/2 - TURTLE_SIZE and TURTLE_SIZE - HEIGHT/2 < y < HEIGHT/2 - TURTLE_SIZE):
turtle.undo() # undo forward()
turtle.setheading(randint(1, 360)) # change heading for next iteration
screen.update()
screen.ontimer(move)
screen = Screen()
screen.tracer(False)
screen.setup(WIDTH, HEIGHT)
for turtle in range(TURTLES):
turtle = Turtle()
turtle.penup()
turtle.shapesize(TURTLE_SIZE / CURSOR_SIZE)
turtle.shape('turtle')
turtle.setheading(randint(1, 360))
turtle.goto(randint(TURTLE_SIZE - WIDTH/2, WIDTH/2 - TURTLE_SIZE), randint(TURTLE_SIZE - HEIGHT/2, HEIGHT/2 - TURTLE_SIZE))
move()
screen.exitonclick()