如何知道群体中哪个精灵发生了碰撞?
How to know which sprite has collided from the group?
我创建了一个 class,这个 class 的每个成员都是我的精灵组的成员。我测试我的玩家和团队之间的碰撞:
pygame.sprite.spritecollide(self,surprise_sprites,False)
我想知道我组中的哪个精灵发生了碰撞,以便使用他们 class 的功能。
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()
returns 发生碰撞的精灵列表。
可以遍历一个精灵列表:
blocks_hit_list = pygame.sprite.spritecollide(self,surprise_sprites,False)
for hit_sprite in blocks_hit_list:
# [...] whatever e.g.
# hit_sprite.myMethod();
我创建了一个 class,这个 class 的每个成员都是我的精灵组的成员。我测试我的玩家和团队之间的碰撞:
pygame.sprite.spritecollide(self,surprise_sprites,False)
我想知道我组中的哪个精灵发生了碰撞,以便使用他们 class 的功能。
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()
returns 发生碰撞的精灵列表。
可以遍历一个精灵列表:
blocks_hit_list = pygame.sprite.spritecollide(self,surprise_sprites,False)
for hit_sprite in blocks_hit_list:
# [...] whatever e.g.
# hit_sprite.myMethod();