coding_noobo
coding_noobo

Reputation: 25

PYGAME, check for second press

So I want to change the variable from False to True on the first key press, and then on the second press (of the same key for e.g. "g") I want the variable to change back to "False"

Here is an example of not working code:

...

if event.type == pygame.KEYUP:
    if event.key == pygame.K_g:
        show_location=True
if event.type == pygame.KEYUP:
    if event.key == pygame.K_g:
        show_location=False
...

Can someone explain to me how to do it right?

Upvotes: 0

Views: 24

Answers (1)

Blckknght
Blckknght

Reputation: 104877

Rather than hard-coding True or False in your assignment, just negate the current value:

if event.type == pygame.KEYUP:
    if event.key == pygame.K_g:
        show_location = not show_location

Upvotes: 1

Related Questions