Question

I have recently been creating a cops and robbers game in pygame. All my code was working until I tried to add control of the cop. Here is what I am using for it:

while True:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        sys.exit()
    elif event.type == pygame.KEYDOWN:
        if pygame.key.get_pressed()[K_LEFT]:
            coprect[0] -= 5
        if pygame.key.get_pressed()[K_RIGHT]:
            coprect[0] += 5
        if pygame.key.get_pressed()[K_DOWN]:
            coprect[1] -= 5
        if pygame.key.get_pressed()[K_UP]:
            coprect[1] += 5

Earlier in the code I have:

pygame.key.set_repeat(1, 2)

So that the keypresses are repeated.

The error I get is:

Traceback (most recent call last): File "C:\Users*username*\Documents\Programming\colour_flasher.py", line 22, in if pygame.key.get_pressed()[K_LEFT]: NameError: name 'K_LEFT' is not defined

Any help available? Thanks in advance?

Was it helpful?

Solution

If you try:

pygame.K_LEFT

it should work

http://www.pygame.org/docs/ref/key.html

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