gpt4 book ai didi

python - 如何知道群体中哪个 Sprite 发生了碰撞?

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:22 24 4
gpt4 key购买 nike

我创建了一个类,这个类的每个成员都是我的 Sprite 组的成员。我测试我的玩家和团队之间的碰撞:

pygame.sprite.spritecollide(self,surprise_sprites,False)

我想知道我组中的哪个 Sprite 发生了碰撞,以便使用它们类中的函数。

class Surprise(pygame.sprite.Sprite):
def __init__(self,x,y,win):
pygame.sprite.Sprite.__init__(self, sol_sprites)
pygame.sprite.Sprite.__init__(self, surprise_sprites)
self.width = TILESIZE
self.height = TILESIZE
self.image = Block_surprise
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.y = y
self.x = x
self.win = win
self.exist = True

def update(self):
if self.exist:
self.collision()
win.blit(self.image,(camera.apply_player([self.rect.x]),self.rect.y))

def collision(self):
blocks_hit_list = pygame.sprite.spritecollide(self,player_sprite,False)
if not (blocks_hit_list == []):
self.exist = False
self.image = brick_img
print("TOUCHE")

def i_want_to_execute_a_function_here(self):

最佳答案

pygame.sprite.spritecollide()返回发生碰撞的 Sprite 列表。
可以遍历 Sprite 列表:

blocks_hit_list = pygame.sprite.spritecollide(self,surprise_sprites,False)
for hit_sprite in blocks_hit_list:
# [...] whatever e.g.
# hit_sprite.myMethod();

关于python - 如何知道群体中哪个 Sprite 发生了碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56239157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com