Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据大小选择最近的对象_Python_Pygame - Fatal编程技术网

Python 如何根据大小选择最近的对象

Python 如何根据大小选择最近的对象,python,pygame,Python,Pygame,我正在做一个基于代理的模拟。我希望每个代理选择关注哪个代理。有很多不同的输入,但这里我要问的是愿景一。因此,代理必须查看哪个代理最接近,同时还要考虑大小。他们应该关心更大的事情,而不是更小的事情,一个非常接近的小代理并不像一个稍微远一点的大代理那么重要。到目前为止,这似乎还不错,但考虑到谁是最近的,谁是大的,也会考虑到其他超出视野的人 到目前为止,我有: # both seen and viewer are two different groups def sight(seen, viewer)

我正在做一个基于代理的模拟。我希望每个代理选择关注哪个代理。有很多不同的输入,但这里我要问的是愿景一。因此,代理必须查看哪个代理最接近,同时还要考虑大小。他们应该关心更大的事情,而不是更小的事情,一个非常接近的小代理并不像一个稍微远一点的大代理那么重要。到目前为止,这似乎还不错,但考虑到谁是最近的,谁是大的,也会考虑到其他超出视野的人

到目前为止,我有:

# both seen and viewer are two different groups
def sight(seen, viewer)
    for entity in viewer:
# This is needed later to decide which way to go, not used here
        currentDir = entity.Direction
# Figure out where the viewer is
        pos = pygame.math.Vector2(entity.rect.centerx, entity.rect.centery)
# Find out who is closest. 
# Uses viewer location and location of each agent in the other group to see who is closest. 
# At the end, divided by size*size of the agent in the other group (for height / width).
# Otherwise they'd pay attention to a tiny agent that's close by 
# instead of a huge agent that's just a bit further away.
        closeby = min([e for e in seen.sprites()], key=lambda e: (pos.distance_to(pygame.math.Vector2(e.rect.centerx, e.rect.centery)) / (e.size * e.size)))
        vector = pygame.math.Vector2((entity.rect.centerx - closeby.rect.centerx), (entity.rect.centery - closeby.rect.centery))
# Get the distance to that other agent
        distance = math.hypot(entity.rect.centerx - closeby.rect.centerx, entity.rect.centery - closeby.rect.centery)
# They can't see forever, so they have a sight limit
            if distance < entity.sight:
                Blah blah, rest of the code here
应该限制在谁在可视范围内(但我不知道怎么做),或者如果选择为“closeby”的代理在可视范围之外,则应选择第二个最近的代理。如果他们再次超出可视范围,则应选择第三个最近的,以此类推,直到有人进入可视范围。但我也不知道该怎么做

任何人都可以在限制“min”([e for e in blah blah blah”)或选择该组中的下一个min(如果第一个min不是指“entity.sight”标准)方面提供任何帮助,我将不胜感激。我觉得第二个选项(在组中迭代)更有可能实现,但我对python非常陌生,所以我不知道


非常感谢大家!

或者使用循环查找最近且可见的元素:

导入数学
def瞄准器(可见,查看器)
对于查看器中的实体:
currentDir=实体方向
entityPos=pygame.math.Vector2(entity.rect.center)
我的=没有
minCloseByDist=math.inf
对于seen.sprites()中的e for e:
ePos=pygame.math.Vector2(e.rect.center)
距离=整个类型。距离(ePos)
如果距离<实体视线:
closeByDist=距离/(e.size*e.size)
如果我的==无或closeByDist
或者,您可以先查找视图中的所有图元,然后查找最近的图元:

def瞄准器(可见,查看器)
对于查看器中的实体:
currentDir=实体方向
entityPos=pygame.math.Vector2(entity.rect.center)
inSight=[e代表seen.sprites()中的e,如果entityPos.distance_to(pygame.math.Vector2(e.rect.center))
使用循环查找最近且可见的元素:

导入数学
def瞄准器(可见,查看器)
对于查看器中的实体:
currentDir=实体方向
entityPos=pygame.math.Vector2(entity.rect.center)
我的=没有
minCloseByDist=math.inf
对于seen.sprites()中的e for e:
ePos=pygame.math.Vector2(e.rect.center)
距离=整个类型。距离(ePos)
如果距离<实体视线:
closeByDist=距离/(e.size*e.size)
如果我的==无或closeByDist
或者,您可以先查找视图中的所有图元,然后查找最近的图元:

def瞄准器(可见,查看器)
对于查看器中的实体:
currentDir=实体方向
entityPos=pygame.math.Vector2(entity.rect.center)
inSight=[e代表seen.sprites()中的e,如果entityPos.distance_to(pygame.math.Vector2(e.rect.center))
closeby = min([e for e in etc etc etc