如何在for循环中移动多只海龟?
How to move multiple turtles in a for loop?
所以,我是 python 的新手,对于我的 class,我的任务是让 10 只海龟参加一场海龟比赛,这些海龟都在移动并且应该停在终点线。我被指示为海龟列出一个列表,并有一个 while 循环让它们随机移动,还有一个嵌套的 if 循环来检查获胜者。我无法让所有海龟同时移动,当我 运行 我的代码时,它一个接一个地移动海龟而不是一起移动,有什么想法吗?
import turtle as trtl
import random as rand
zoomers = []
zom = [0,1,2,3,4,5,6,7,8,9]
tloc = -130
trtl.penup()
trtl.goto(-150, 150)
trtl.pendown()
trtl.goto(-150,-140)
trtl.penup()
trtl.goto(180,150)
trtl.pendown()
trtl.goto(180,-140)
trtl.hideturtle()
for z in zom:
zoom = trtl.Turtle("turtle")
zoom.penup()
zoom.goto(-150,-tloc)
tloc += 25
robux = rand.randrange(0,20)
zoomers.append(zoom)
for n in zoomers:
cash = 0
while cash < 100:
zoom.forward(robux)
cash = cash + 1
if zoom.xcor() == 180:
print("We have a winner!")
break```
我们甚至可以在不使用 turtle
库的情况下对其进行建模:
import random
twerbles = list(zip(range(10), [0]*10)) #list of turtles all starting at position 0 (numbered 0-10)
finishline = 10 #the threshold they must pass
while True:
twerbles = [(twerble, position+random.randint(1,2)) for twerble, position in twerbles] #move each turtle up a random amount.
print(twerbles) #this can be removed if you dont want to see their positions as they go
winners = [twerble for twerble, position in twerbles if position >= finishline] #list of turtles that passed the threshold (if any)
if winners: print(winners); break #if there are winners print them and stop looping
[(0, 1), (1, 2), (2, 1), (3, 2), (4, 1), (5, 2), (6, 1), (7, 1), (8, 1), (9, 2)]
[(0, 2), (1, 3), (2, 2), (3, 3), (4, 2), (5, 4), (6, 3), (7, 3), (8, 3), (9, 3)]
[(0, 4), (1, 5), (2, 4), (3, 4), (4, 4), (5, 6), (6, 5), (7, 5), (8, 4), (9, 5)]
[(0, 5), (1, 7), (2, 6), (3, 5), (4, 6), (5, 8), (6, 6), (7, 7), (8, 5), (9, 6)]
[(0, 6), (1, 8), (2, 8), (3, 6), (4, 7), (5, 9), (6, 7), (7, 9), (8, 7), (9, 7)]
[(0, 8), (1, 9), (2, 10), (3, 7), (4, 9), (5, 11), (6, 8), (7, 11), (8, 8), (9, 9)]
[2, 5, 7] #winning turtles
与海龟一起移动
import turtle as trtl
import random
finishline = 30
twerbles = [(trtl.Turtle("turtle"), 0) for i in range(10)]
for i,(twerble,position) in enumerate(twerbles):
twerble.penup()
twerble.goto(-150,150 - i*25)
while True:
twerbles = [(twerble, position+random.randint(1,2)) for twerble, position in twerbles]
for twerble, position in twerbles:
twerble.forward(position)
winners = [twerble for twerble, position in twerbles if position >= finishline]
if winners:
break
没有直接的方法可以做到这一点,但这是下一个最好的方法。
创建一只海龟Screen
。将其 tracer()
设置为 0
,并在循环的每次迭代中更新屏幕。它可能会变得太快,因此从 time
模块中导入 sleep
以减慢速度。
最小示例:
from turtle import Turtle, Screen
from random import randrange
from time import sleep
wn = Screen()
wn.tracer(0)
t1 = Turtle('turtle')
t2 = Turtle('turtle')
t1.penup()
t2.penup()
t1.goto(-100, 50)
t2.goto(-100, -50)
while True:
sleep(0.1)
t1.forward(randrange(0, 20))
t2.forward(randrange(0, 20))
wn.update()
您需要创建一个海龟集合。为每只海龟循环移动(一步)直到一只海龟完成。
试试这个代码:
import turtle as trtl
import random as rand
zoomers = []
zom = [0,1,2,3,4,5,6,7,8,9]
tloc = -130
trtl.penup()
trtl.goto(-150, 150)
trtl.pendown()
trtl.goto(-150,-140)
trtl.penup()
trtl.goto(180,150)
trtl.pendown()
trtl.goto(180,-140)
trtl.hideturtle()
zoomers = [trtl.Turtle("turtle") for z in zom]
for i,t in enumerate(zoomers):
t.penup()
t.goto(-150,150 - i*25)
for i,z in enumerate(zom):
zoom = zoomers[i] #trtl.Turtle("turtle")
tloc += 25
# zoomers.append(zoom)
running = True
while running:
for zoom in zoomers:
robux = rand.randrange(0,20)
zoom.forward(robux)
if zoom.xcor() >= 180:
print("We have a winner!")
running = False
break
input('Press enter to exit...')
您正在使用一只乌龟。您可以通过创建多个海龟对象轻松地做到这一点。我用了 3 个乌龟对象,他们有一个小比赛。
试试这个代码,你可以在这个代码中实现你的赢家逻辑。
import turtle as trtl
import random as rand
zoom1 = trtl.Turtle()
zoom2 = trtl.Turtle()
zoom3 = trtl.Turtle()
zoomers = [zoom1, zoom2, zoom3]
for zoom in zoomers:
zoom.penup()
zoom1.goto(-150, 150)
zoom2.goto(-120, 150)
zoom3.goto(-90, 150)
for zoom in zoomers:
zoom.pendown()
zoom.right(90)
for zoom in zoomers:
robux = rand.randrange(10,50)
zoom.forward(robux)
编辑:您需要非阻塞代码来实现同时且彼此独立的海龟移动。您可以通过使用多线程来做到这一点。试试 this.
到目前为止,所有其他解决方案都会在固定时间片内将海龟移动一段随机距离。让我们把它反过来,让海龟在随机时间片中移动一个恒定的距离:
from turtle import Turtle, Screen
from random import randrange
def run(turtle):
turtle.forward(5)
if turtle.xcor() < half_width:
screen.ontimer(lambda: run(turtle), randrange(20, 150))
screen = Screen()
half_width = screen.window_width() / 2
lane_width = 20
for order, color in enumerate(['red', 'green', 'blue']):
turtle = Turtle('turtle')
turtle.speed('fastest')
turtle.color(color)
turtle.penup()
turtle.goto(-half_width, (order | 1) * lane_width)
lane_width *= -1
run(turtle)
screen.exitonclick()
所以,我是 python 的新手,对于我的 class,我的任务是让 10 只海龟参加一场海龟比赛,这些海龟都在移动并且应该停在终点线。我被指示为海龟列出一个列表,并有一个 while 循环让它们随机移动,还有一个嵌套的 if 循环来检查获胜者。我无法让所有海龟同时移动,当我 运行 我的代码时,它一个接一个地移动海龟而不是一起移动,有什么想法吗?
import turtle as trtl
import random as rand
zoomers = []
zom = [0,1,2,3,4,5,6,7,8,9]
tloc = -130
trtl.penup()
trtl.goto(-150, 150)
trtl.pendown()
trtl.goto(-150,-140)
trtl.penup()
trtl.goto(180,150)
trtl.pendown()
trtl.goto(180,-140)
trtl.hideturtle()
for z in zom:
zoom = trtl.Turtle("turtle")
zoom.penup()
zoom.goto(-150,-tloc)
tloc += 25
robux = rand.randrange(0,20)
zoomers.append(zoom)
for n in zoomers:
cash = 0
while cash < 100:
zoom.forward(robux)
cash = cash + 1
if zoom.xcor() == 180:
print("We have a winner!")
break```
我们甚至可以在不使用 turtle
库的情况下对其进行建模:
import random
twerbles = list(zip(range(10), [0]*10)) #list of turtles all starting at position 0 (numbered 0-10)
finishline = 10 #the threshold they must pass
while True:
twerbles = [(twerble, position+random.randint(1,2)) for twerble, position in twerbles] #move each turtle up a random amount.
print(twerbles) #this can be removed if you dont want to see their positions as they go
winners = [twerble for twerble, position in twerbles if position >= finishline] #list of turtles that passed the threshold (if any)
if winners: print(winners); break #if there are winners print them and stop looping
[(0, 1), (1, 2), (2, 1), (3, 2), (4, 1), (5, 2), (6, 1), (7, 1), (8, 1), (9, 2)]
[(0, 2), (1, 3), (2, 2), (3, 3), (4, 2), (5, 4), (6, 3), (7, 3), (8, 3), (9, 3)]
[(0, 4), (1, 5), (2, 4), (3, 4), (4, 4), (5, 6), (6, 5), (7, 5), (8, 4), (9, 5)]
[(0, 5), (1, 7), (2, 6), (3, 5), (4, 6), (5, 8), (6, 6), (7, 7), (8, 5), (9, 6)]
[(0, 6), (1, 8), (2, 8), (3, 6), (4, 7), (5, 9), (6, 7), (7, 9), (8, 7), (9, 7)]
[(0, 8), (1, 9), (2, 10), (3, 7), (4, 9), (5, 11), (6, 8), (7, 11), (8, 8), (9, 9)]
[2, 5, 7] #winning turtles
与海龟一起移动
import turtle as trtl
import random
finishline = 30
twerbles = [(trtl.Turtle("turtle"), 0) for i in range(10)]
for i,(twerble,position) in enumerate(twerbles):
twerble.penup()
twerble.goto(-150,150 - i*25)
while True:
twerbles = [(twerble, position+random.randint(1,2)) for twerble, position in twerbles]
for twerble, position in twerbles:
twerble.forward(position)
winners = [twerble for twerble, position in twerbles if position >= finishline]
if winners:
break
没有直接的方法可以做到这一点,但这是下一个最好的方法。
创建一只海龟Screen
。将其 tracer()
设置为 0
,并在循环的每次迭代中更新屏幕。它可能会变得太快,因此从 time
模块中导入 sleep
以减慢速度。
最小示例:
from turtle import Turtle, Screen
from random import randrange
from time import sleep
wn = Screen()
wn.tracer(0)
t1 = Turtle('turtle')
t2 = Turtle('turtle')
t1.penup()
t2.penup()
t1.goto(-100, 50)
t2.goto(-100, -50)
while True:
sleep(0.1)
t1.forward(randrange(0, 20))
t2.forward(randrange(0, 20))
wn.update()
您需要创建一个海龟集合。为每只海龟循环移动(一步)直到一只海龟完成。
试试这个代码:
import turtle as trtl
import random as rand
zoomers = []
zom = [0,1,2,3,4,5,6,7,8,9]
tloc = -130
trtl.penup()
trtl.goto(-150, 150)
trtl.pendown()
trtl.goto(-150,-140)
trtl.penup()
trtl.goto(180,150)
trtl.pendown()
trtl.goto(180,-140)
trtl.hideturtle()
zoomers = [trtl.Turtle("turtle") for z in zom]
for i,t in enumerate(zoomers):
t.penup()
t.goto(-150,150 - i*25)
for i,z in enumerate(zom):
zoom = zoomers[i] #trtl.Turtle("turtle")
tloc += 25
# zoomers.append(zoom)
running = True
while running:
for zoom in zoomers:
robux = rand.randrange(0,20)
zoom.forward(robux)
if zoom.xcor() >= 180:
print("We have a winner!")
running = False
break
input('Press enter to exit...')
您正在使用一只乌龟。您可以通过创建多个海龟对象轻松地做到这一点。我用了 3 个乌龟对象,他们有一个小比赛。
试试这个代码,你可以在这个代码中实现你的赢家逻辑。
import turtle as trtl
import random as rand
zoom1 = trtl.Turtle()
zoom2 = trtl.Turtle()
zoom3 = trtl.Turtle()
zoomers = [zoom1, zoom2, zoom3]
for zoom in zoomers:
zoom.penup()
zoom1.goto(-150, 150)
zoom2.goto(-120, 150)
zoom3.goto(-90, 150)
for zoom in zoomers:
zoom.pendown()
zoom.right(90)
for zoom in zoomers:
robux = rand.randrange(10,50)
zoom.forward(robux)
编辑:您需要非阻塞代码来实现同时且彼此独立的海龟移动。您可以通过使用多线程来做到这一点。试试 this.
到目前为止,所有其他解决方案都会在固定时间片内将海龟移动一段随机距离。让我们把它反过来,让海龟在随机时间片中移动一个恒定的距离:
from turtle import Turtle, Screen
from random import randrange
def run(turtle):
turtle.forward(5)
if turtle.xcor() < half_width:
screen.ontimer(lambda: run(turtle), randrange(20, 150))
screen = Screen()
half_width = screen.window_width() / 2
lane_width = 20
for order, color in enumerate(['red', 'green', 'blue']):
turtle = Turtle('turtle')
turtle.speed('fastest')
turtle.color(color)
turtle.penup()
turtle.goto(-half_width, (order | 1) * lane_width)
lane_width *= -1
run(turtle)
screen.exitonclick()