python - 无论如何在龟蟒中做类似 list[select_all] 的事情吗?
问题描述
所以我正在用 python turtle 制作一个游戏,里面有很多克隆。我已经将不同的克隆放到不同的列表中。
如果 turtle.distance(list[select_all]) 我该怎么做?
我真的需要你的帮助,因为我不想写一行一百个字母长的代码。谢谢。
解决方案
充实一个完整的例子,我想我不会使用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()
推荐阅读
- azure-data-explorer - 使用 Azure 数据资源管理器使用动态值汇总和透视数据集中的数据
- python - 检测变量并在设置脚本中获取其值
- laravel - 动态接收id和model
- postgresql - Nginx tcp 流反向代理
- c# - C# Directory.GetFolder 对文件夹进行排序并将它们设置为 List<> 而不会丢失内存并执行额外的排序操作
- python - Python cProfile/pstats 导致溢出?
- reactjs - 条件反应文本掩码渲染
- python - 返回与其元组的第二个元素中的最大值链接的字典键
- npm - Node-RED 输入/输出未显示
- cesium - 使用 Cesium.CallbackProperty 显示/隐藏实体