rishi
rishi

Reputation: 640

How do I check for a certain condition and implement it once with reference to pygame

I'm making a pygame game and I want to increase the number of enemies whenever the player reaches 20 kills i.e kills % 20 == 0. I have a function enemy_spawn(number_of_enemies) and I implemented the following code below. I would like to know what I'm doing wrong because there are infinite enemies once it executes. Please suggest some possible fixes. I have attached only the relevant code. Thank you.

number_of_enemies = 5
num_check = False

redrawWindow():
    global num_check
    global number_of_enemies

    if kills%20 == 0 and kills > 1:
        num_check = True
    if num_check:
        number_of_enemies += 1
        num_check = False

Upvotes: 1

Views: 51

Answers (2)

Glenn Mackintosh
Glenn Mackintosh

Reputation: 2790

Change the second if to an elif so that it does not get executed right after the first if sets num_check = True. Like this:

if kills%20 == 0 and kills > 1:
    num_check = True
elif num_check:
    number_of_enemies += 1
    num_check = False

Upvotes: 2

adam
adam

Reputation: 378

This makes sense. After you set num_check to False, the first if condition sets it back to True if the user does not obtain any new kills.

One solution is to also reset the kill count to 0 when num_check is True. For practical use, it may be logical to have a second kill count that keeps track of total kills as well.

Upvotes: 2

Related Questions