Domanda

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.

È stato utile?

Soluzione 2

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

Altri suggerimenti

Try replacing the:

if health == 0:

    health = min

with:

if health < min:

    health = min

This should work. I hope this helps.

Add extra logic. When the life is 0 or less after reduce -25:

if health <= 0:
    break

This is better because if there's another enemy that hits -35 and your life is 25? Of course the result will be -10 <= 0 and break.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top