我的玩家通过向敌人投掷武器进行攻击。当前,这是通过使用抛出的奇异剑来完成的,然后将其传送回玩家以再次抛出。我要更改此设置,以便有一个名为剑的类,当玩家想要扔出武器时可以调用该类。但是,我不知道如何在精灵组之间进行碰撞。这是我的碰撞当前工作方式的一个示例(只显示整个敌人的等级更容易)
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): #There is an indent in the actual code, but stack overflow removed it in copying and pasting
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 fly(self):
global dash_time
global dash
global const
x = player_rect.centerx - self.rect.centerx
y = player_rect.centery - self.rect.centery
if x == 0:
x = 0.1
if y == 0:
y = 0.1
self.dash_counter = random.randint(0, 120)
if self.dash_counter == 30:
if self.dash == False:
self.dash_time = pygame.time.get_ticks() ##def self in __init__
self.dash = True
self.const = 5.5
if pygame.time.get_ticks() - self.dash_time > 1500:
self.dash = False
self.const = 3
norm = (x**2 + y**2)**0.5
self.bat_x += self.const * x / norm
self.bat_y += self.const * y / norm
if self.const * y / norm > 0:
if self.fly_count == 0:
self.image = bat_front1
self.fly_time = pygame.time.get_ticks()
self.fly_count = 1
if self.fly_count == 1:
if pygame.time.get_ticks() - self.fly_time > 600:
self.image = bat_front2
if pygame.time.get_ticks() - self.fly_time > 1200:
self.fly_count = 0
if self.const * y / norm < 0:
if self.fly_count == 0:
self.image = bat_back1
self.fly_time = pygame.time.get_ticks()
self.fly_count = 1
if self.fly_count == 1:
if pygame.time.get_ticks() - self.fly_time > 600:
self.image = bat_back2
if pygame.time.get_ticks() - self.fly_time > 1200:
self.fly_count = 0
def update(self): ##RELEVANT##
global player_immune
global player_health
global immune_time
global player_hitsound
global enemy_hitsound
global dmg_multiplier
self.rect.topleft = (self.bat_x, self.bat_y)
bat_immune = False
bat_health = 5
offsetP2B = (int(x - self.bat_x), int(y - self.bat_y)) #Player to Bat
resultP2B = self.mask.overlap(player_mask, offsetP2B)
if resultP2B:
if player_immune == False:
if invincibility_potion_activ == False:
player_health -= 1
player_hitsound.play()
immune_time = pygame.time.get_ticks()
player_immune = True
offsetS2B = (int(fx - self.bat_x), int(fy - self.bat_y)) #Sword to Bat
resultS2B = self.mask.overlap(sword_mask, offsetS2B)
if resultS2B:
if self.bat_immune == False:
if fire == True:
self.bat_health -= 1 * dmg_multiplier
enemy_hitsound.play()
self.bat_time = pygame.time.get_ticks()
self.bat_immune = True
if self.bat_health <= 0:
self.kill()
if self.bat_immune == True:
if pygame.time.get_ticks() - self.bat_time > 100:
self.bat_immune = False
self.fly()
all_bats = pygame.sprite.Group()
这很完美,但是我希望我的武器像敌人一样,一个精灵组,这样我可以一次在屏幕上显示多个,但是我该怎么做呢?老实说,我不知道如何解决这个问题!
答案 0 :(得分:2)
我相信pygame.sprite。groupcollide()是您要找的东西吗? “找到在两个组之间碰撞的所有精灵。”
如果您想一次通过一把剑,也可以使用pygame.sprite。spritecollideany()。