在turtle python中是否有类似list[select_all]的操作?
所以我正在用python turtle制作一个游戏,里面有很多克隆人。我把不同的克隆放在不同的列表中 我怎样才能做到像if turtle.distance(list[全选])这样的事情在turtle python中是否有类似list[select_all]的操作?,python,list,python-turtle,Python,List,Python Turtle,所以我正在用python turtle制作一个游戏,里面有很多克隆人。我把不同的克隆放在不同的列表中 我怎样才能做到像if turtle.distance(list[全选])这样的事情 我真的需要你的帮助,因为我不想写一行100个字母长的代码。谢谢。如果我理解正确,你想要一份游戏中所有海龟的优步列表。我认为您可以将列表理解与列表结合使用,如下所示: a = [1,2,3,4,5] #list 1 b = [6,7,8,9,10] #list 2 c = [a,b] # list of lists
我真的需要你的帮助,因为我不想写一行100个字母长的代码。谢谢。如果我理解正确,你想要一份游戏中所有海龟的优步列表。我认为您可以将列表理解与列表结合使用,如下所示:
a = [1,2,3,4,5] #list 1
b = [6,7,8,9,10] #list 2
c = [a,b] # list of lists
d = [element for array in c for element in array] # get every element from every list
print(d)
#Prints [1,2,3,4,5,6,7,8,9,10]
总之:制作列表列表,然后使用嵌套for循环制作列表。希望这是有帮助的。如果另一个答案是您所需要的,这可能是一种更有效的方法(如果不是,请告诉我)
为了充实一个完整的例子,我想我不会使用
any()
或all()
而是使用filter()
来查找距离目标指定距离内的实际海龟:
from turtle import Screen, Turtle
from random import randint
from itertools import chain
screen = Screen()
prototype = Turtle()
prototype.hideturtle()
prototype.shape('turtle')
prototype.penup()
red = [] # a bunch of randomly placed red turtles
for _ in range(15):
turtle = prototype.clone()
turtle.color('red')
turtle.goto(randint(-200, 200), randint(-200, 200))
turtle.showturtle()
red.append(turtle)
green = [] # a bunch of randomly placed green turtles
for _ in range(15):
turtle = prototype.clone()
turtle.color('green')
turtle.goto(randint(-200, 200), randint(-200, 200))
turtle.showturtle()
green.append(turtle)
yellow = prototype.clone() # our target turtle
yellow.color('yellow')
yellow.goto(randint(-200, 200), randint(-200, 200))
yellow.showturtle()
closest = filter(lambda t: yellow.distance(t) < 100, chain(red, green))
for turtle in closest: # turtles closest to yellow turtle turn blue
turtle.color('blue')
screen.exitonclick()
从海龟导入屏幕,海龟
从随机导入randint
来自itertools进口链
screen=screen()
原型=海龟()
prototype.hideturtle()
prototype.shape('turtle'))
prototype.penup()
红色=[]#一群随机放置的红海龟
对于范围内的(15):
turtle=prototype.clone()
海龟。颜色(‘红色’)
乌龟。后藤(randint(-200200),randint(-200200))
海龟
红色。追加(海龟)
绿色=[]#一群随机放置的绿海龟
对于范围内的(15):
turtle=prototype.clone()
海龟。颜色(‘绿色’)
乌龟。后藤(randint(-200200),randint(-200200))
海龟
绿色。追加(海龟)
黄色=prototype.clone()#我们的目标海龟
黄色。颜色(“黄色”)
黄色。转到(randint(-200200),randint(-200200))
黄色
最近=过滤器(λt:黄色。距离(t)<100,链(红色,绿色))
对于最近的海龟:#最接近黄色海龟的海龟变成蓝色
海龟。颜色(‘蓝色’)
screen.exitonclick()
您能否澄清并发送一些代码?据我所知,您可以使用
for
循环,您可能需要使用all
或any
功能。但是需要看一些实际的代码——如果。。。列出[全部选择]do。for循环工作。谢谢
from turtle import Screen, Turtle
from random import randint
from itertools import chain
screen = Screen()
prototype = Turtle()
prototype.hideturtle()
prototype.shape('turtle')
prototype.penup()
red = [] # a bunch of randomly placed red turtles
for _ in range(15):
turtle = prototype.clone()
turtle.color('red')
turtle.goto(randint(-200, 200), randint(-200, 200))
turtle.showturtle()
red.append(turtle)
green = [] # a bunch of randomly placed green turtles
for _ in range(15):
turtle = prototype.clone()
turtle.color('green')
turtle.goto(randint(-200, 200), randint(-200, 200))
turtle.showturtle()
green.append(turtle)
yellow = prototype.clone() # our target turtle
yellow.color('yellow')
yellow.goto(randint(-200, 200), randint(-200, 200))
yellow.showturtle()
closest = filter(lambda t: yellow.distance(t) < 100, chain(red, green))
for turtle in closest: # turtles closest to yellow turtle turn blue
turtle.color('blue')
screen.exitonclick()