SolnLase
SolnLase

Reputation: 89

Why the if statement is being executed yet the condition is false?

Even if the variable bullets_fire contains the boolean False, the command below is going to be executed. I also added print statement for bullets_fire nested in if statement, and it outputs False, why it's being executed then?

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_SPACE and not bullets_fire:
        print(bullets_fire)
        bullets_fire = True
        bullets_x = player_x
        bullets_y = player_y

Upvotes: 0

Views: 1061

Answers (1)

Daniel
Daniel

Reputation: 1995

your condition contains not before the boolean variable. it will execute only if event.key == pygame.K_SPACE evaluates as True and bullets_fire evaluated as False, since not False is the same as True.

Upvotes: 1

Related Questions