Question

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

Was it helpful?

Solution

if you just did this:

import pygame

Then you need to do this:

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

OTHER TIPS

If the interpreter complains about the symbol not being defined, probably you need to fully qualify the name:

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

or explicitly import it:

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

Add:

from pygame.locals import *

to the beginning of your program, this will import the key variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top