r/pygame • u/Several-Pin-6931 • Oct 29 '26
Pygame bug help
I'm currently working on my first pygame project and I'm trying to code a feature to target the closest enemy. However, a problem occurs when the enemies are defeated.
In my program, enemies are appended to a list and their distance to the player is also stored in a separate list. The problem comes when an enemy sprite is killed the enemy and distance aren't removed from their list so the game will still target the enemy as if it were still their.
def Closest_Enemy(self):
global closestEnemy
smallestIndex = 0
distlist =[]
for i in range(0, len(self.game.enemieslist)):
distlist.append(self.game.enemieslist[i].distance)
closestEnemy = self.game.enemieslist[np.argmin(distlist)]I used this code to find the closest enemy
I also tried to remove the enemies and distanced from the list after the enemy is killed but I just got a bunch of index errors for the lists.
Any help would be appreciated.
