如何打断精灵碰撞?

How to interrupt spritecollide?

开车穿过有房子的区域。如果汽车撞到一所房子,汽车就会停下来,并且应该打印一条信息。由于汽车与房子保持接触的时间更长——即使我随后向后行驶——有许多消息,而不仅仅是一个。我怎样才能让那里只有一条消息?

hits = pygame.sprite.spritecollide( auto, land, False)   
for hit in hits:        
    if pygame.sprite.spritecollide(auto, land, False):     
        print("hit ")

您必须将前一帧的命中保存在列表中,如果命中不在前一帧中则打印一条消息:

old_hits = []

# application loop
while True:
    # [...]
    
    hits = pygame.sprite.spritecollide( auto, land, False)   
    for hit in hits:        
        if hit not in old_hits:     
            print("hit ")
    old_hits = hits