Steve104 Steve104 - 20 days ago 7
Python Question

Pygame update the score when enemies collide


def Score_Change():
global Score
Score += 1
print(Score) #For testing purposes

This line makes two enemies who collide disappear. However the function at the end always is on for some reason and i want it to only come on when the enemies collide


Everytime this line


is executed, Score_Change() will be called, the code inside this function will be executed, and its return value (None), will be passed to the groupcollide function as collided argument.

Note that the last parameter expects a function that calculates if two sprites collide. From the documentation:

The collided argument is a callback function used to calculate if two sprites are colliding. It should take two sprites as values and return a bool value indicating if they are colliding. If collided is not passed, then all sprites must have a “rect” value, which is a rectangle of the sprite area, which will be used to calculate the collision.

You usually pass a function like pygame.sprite.collide_mask or pygame.sprite.collide_circle as this argument.

So it seems you expect Score_Change to be called if a collision happens. That's not the case. What you could do is something like:

for k, v in pygame.sprite.groupcollide(Enemy_List,Bullet_List,True,True):
    # k is the enemy
    # v is a list of bullets that them
    global Score
    Score += 1