以正确的顺序错误地移除精灵组敌人
Incorrectly removing sprite group enemies in the right order
我有 2 个精灵组,一组用于敌人,一组用于玩家的武器。我如何设置敌人精灵组的示例...(武器精灵组以相同方式完成)
class Bat(pygame.sprite.Sprite):
def __init__(self, bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time):
pygame.sprite.Sprite.__init__(self)
self.bat_health = bat_health
self.bat_immune = bat_immune
self.const = const
self.dash_counter = dash_counter
self.dash = dash
self.dash_time = dash_time
self.fly_count = fly_count
self.fly_time = fly_time
self.image = bat_image
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.topleft = (bat_x, bat_y)
self.bat_x = bat_x
self.bat_y = bat_y
def update(self):
if pygame.sprite.groupcollide(all_bats, all_sword, False, True):
self.bat_health -= 1
if self.bat_health == 0:
self.kill()
... #Irrelevant
all_bats = pygame.sprite.Group()
def bat_create():
bat_x = r_x*40
bat_y = r_y*40
bat_health = 5
bat_immune = False
const = 3
dash_counter = 0
dash = False
dash_time = 0
fly_count = 0
fly_time = 0
new_bat = Bat(bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time)
all_bats.add(new_bat)
当玩家的武器击中蝙蝠时,无论击中的是哪只蝙蝠,在击中 5 次后第一只刷新的蝙蝠将被杀死。我不知道为什么会这样。
看起来问题是这样的:
def update(self):
if pygame.sprite.groupcollide(all_bats, all_sword, False, True):
self.bat_health -= 1
groupcollide
returns 碰撞列表。上面的代码正在检查 all_bats
与 all_swords
,所以一切 Vs 一切。而且 if ( non-empty-list )
总是 returns True
。
所以如果任何蝙蝠+剑发生碰撞,这个精灵的生命值就会减少。每次 any 蝙蝠碰撞时,这可能会减少 every bat 的 self.bat_health
。因此,第一个创建的项目将首先达到零。
您需要检查 "this sprite" 是否是碰撞的一部分。这是一个相当简单的改变:
def update(self):
hit_list = pygame.sprite.groupcollide(all_bats, all_sword, False, True):
if ( self in hit_list ):
self.bat_health -= 1
但是在每个球拍 update()
期间对每个球拍执行 groupcollide
效率不高。也许将 groupcollide
调用移动到 .update()
函数之外的某个地方,并且只执行一次这些计算。将碰撞结果传递给 sprite-update 函数等。
我有 2 个精灵组,一组用于敌人,一组用于玩家的武器。我如何设置敌人精灵组的示例...(武器精灵组以相同方式完成)
class Bat(pygame.sprite.Sprite):
def __init__(self, bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time):
pygame.sprite.Sprite.__init__(self)
self.bat_health = bat_health
self.bat_immune = bat_immune
self.const = const
self.dash_counter = dash_counter
self.dash = dash
self.dash_time = dash_time
self.fly_count = fly_count
self.fly_time = fly_time
self.image = bat_image
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.topleft = (bat_x, bat_y)
self.bat_x = bat_x
self.bat_y = bat_y
def update(self):
if pygame.sprite.groupcollide(all_bats, all_sword, False, True):
self.bat_health -= 1
if self.bat_health == 0:
self.kill()
... #Irrelevant
all_bats = pygame.sprite.Group()
def bat_create():
bat_x = r_x*40
bat_y = r_y*40
bat_health = 5
bat_immune = False
const = 3
dash_counter = 0
dash = False
dash_time = 0
fly_count = 0
fly_time = 0
new_bat = Bat(bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time)
all_bats.add(new_bat)
当玩家的武器击中蝙蝠时,无论击中的是哪只蝙蝠,在击中 5 次后第一只刷新的蝙蝠将被杀死。我不知道为什么会这样。
看起来问题是这样的:
def update(self):
if pygame.sprite.groupcollide(all_bats, all_sword, False, True):
self.bat_health -= 1
groupcollide
returns 碰撞列表。上面的代码正在检查 all_bats
与 all_swords
,所以一切 Vs 一切。而且 if ( non-empty-list )
总是 returns True
。
所以如果任何蝙蝠+剑发生碰撞,这个精灵的生命值就会减少。每次 any 蝙蝠碰撞时,这可能会减少 every bat 的 self.bat_health
。因此,第一个创建的项目将首先达到零。
您需要检查 "this sprite" 是否是碰撞的一部分。这是一个相当简单的改变:
def update(self):
hit_list = pygame.sprite.groupcollide(all_bats, all_sword, False, True):
if ( self in hit_list ):
self.bat_health -= 1
但是在每个球拍 update()
期间对每个球拍执行 groupcollide
效率不高。也许将 groupcollide
调用移动到 .update()
函数之外的某个地方,并且只执行一次这些计算。将碰撞结果传递给 sprite-update 函数等。