python-3.xnumberspygamereducing

Stop number decrease once 0 reached - Python


I am working on a small game in pygame and I have set up a health variable so each time my player gets hit by an enemy ship my health decreases by 25, however sometimes (If I my player rapidly across the screen) I am able to get this below 0 and it continues reducing by 25 into minus numbers.

Is there any way I can set it so the score cannot go below 0 once 0 is reached?

Heres my code so far:

for player in player_list:

    block_hit_list = pygame.sprite.spritecollide(player, block_list, True)

    for block in block_hit_list:
        health -= 25
        collision.play()

    if health < 0:
        health = max(value, 0)

    if health == 0:
        gameover.play()
        collision.stop()
        player_list.remove(player)
        all_sprites_list.remove(player)
        block_list.remove(block)
        all_sprites_list.remove(block)

Update

I have updated the code and now it seems to be functioning properly, not sure if I have used something which makes sense (If so, any improvements are welcomed) :( I am quite new to python.


Solution

  • I am not too sure what you final goal is since this is only a snippet of code but you need to either increase your health variable or break the loop

    if health == 0:
        break