pythonpgzero

Is there a way to make this if statement only run once?


I am trying to make a script that gives the player 5 lives for every 100 points. It works, however a bit too well. When I run the code, when i reach 100 points, it doesn't stop giving lives, and only stops when I get more points.

This is the code I'm using:

def increase_lives():
    global lives
    global score
    global extralives
    
    if score % 100 == 0 and score != 0 or keyboard.lctrl and keyboard.ralt:
        extralives = 1
    if extralives == 1:
        lives += 5
        extralives -= 1

I am using Thonny with PyGame and PGZero. Any help is greatly appreciated.


Solution

  • You can create a temp variable to mark a "milestone".

    global lives
    global score
    global extralives
    global milestone
    
    if (score % 100 == 0 and score != 0 and score != milestone) or (keyboard.lctrl and keyboard.ralt):
        extralives = 1
        milestone = score
    if extralives == 1:
        lives += 5
        extralives -= 1