Search code examples
pythonpygamekeyspacepressed

Error in the pygame.key.get_pressed() method


I have no idea why the following is giving the error: name 'K_SPACE' is not defined. Importing pygame also imports the keyboard constants which includes 'K_SPACE'.

My code:

if pygame.key.get_pressed()[K_SPACE]:
    pygame.quit

Thanks


Solution

  • if you just did this:

    import pygame
    

    Then you need to do this:

    if pygame.key.get_pressed()[pygame.K_SPACE]:
        pygame.quit