首页 > 解决方案 > 无论如何在龟蟒中做类似 list[select_all] 的事情吗?

问题描述

所以我正在用 python turtle 制作一个游戏,里面有很多克隆。我已经将不同的克隆放到不同的列表中。

如果 turtle.distance(list[select_all]) 我该怎么做?

我真的需要你的帮助,因为我不想写一行一百个字母长的代码。谢谢。

标签: pythonlistpython-turtle

解决方案


充实一个完整的例子,我想我不会使用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()

在此处输入图像描述


推荐阅读