弹丸与播放器碰撞问题?

Projectile collide with a player issue?

目前正在开发 1v1 Pygame 射击游戏,但在让碰撞正常工作时遇到了问题。子弹应该能够在存活 120 秒后击中任何玩家。出于某种原因,当没有玩家与子弹发生碰撞时会触发碰撞,有时当玩家在视觉上与子弹发生碰撞时不会触发碰撞。

我尝试了很多不同的东西,但我似乎无法做到正确。我正在讨论将碰撞重写为子弹 class 而不是玩家 class 但我会 post 我的一些代码。

def playerdeathevent(self):
        for bullet in bullet_list:
            if bullet.time_alive >= 120:
                # bullet_active_list = pygame.sprite.Group()
                bullet_active_list.add(bullet)
                # if self.PlayerHitboxRect.collidepoint([bullet.rect.x,bullet.rect.y]):
                #     draw_text(('Player ' + str(self.playernumber) + ' Died'), titlefont, WHITE, screen, 600, 700)
                #     pygame.sprite.Group.empty(bullet_list)
                #     return 'Destroyed'
                if pygame.sprite.spritecollideany(self, bullet_active_list):
                    explode = Explosion(self.rect.x + 39, self.rect.y + 40)
                    explosion_group.add(explode)
                    collisionsound = mixer.Sound('Audio/EXPLODE-SFX1.wav')
                    collisionsound.play()
                    draw_text(('Player ' + str(self.playernumber) + ' Died'), titlefont, WHITE, screen, 600, 700)
                    pygame.sprite.Group.empty(bullet_list)
                    self.rect.x = self.rect.x + 1
                    self.rect.y = self.rect.y + 1
                    return 'Destroyed'
                else:
                    return 'Alive'
def player_score_count(self):
        if self.playernumber == 1:
            draw_text(str(self.playerLives), submenufont, WHITE, screen, 293, 55)
        elif self.playernumber == 2:
            draw_text(str(self.playerLives), submenufont, WHITE, screen, 1093, 55)
        if self.playerdeathevent() == 'Destroyed' and self.playerdeathevent() != 'Alive':
            if self.playerLives == 0:
                print('Game Over')
                draw_text('GAME OVER', titlefont, WHITE, screen, 600, 640)
                draw_text(('Player ' + str(self.playernumber) + ' Loses'), titlefont, WHITE, screen, 580, 700)
            else:
                self.playerLives = self.playerLives - 1
                print('HIT')

任何关于如何解决这个问题的建议都会很好。

显然是Indentation is wrong. First you need to identify the active bullets. empty()bullet_active_list然后添加当前活动的项目符号。然后使用主动子弹进行碰撞检测:

def playerdeathevent(self):

    bullet_active_list.empty()
    for bullet in bullet_list:
        bullet_active_list.add(bullet)

    # INDENTATION
    #<--|
    if pygame.sprite.spritecollideany(self, bullet_active_list):
        explode = Explosion(self.rect.x + 39, self.rect.y + 40)
        explosion_group.add(explode)   
        # [...]