Craig A Gomez
Craig A Gomez

Reputation: 139

Python event has not Key attribute

I am trying to create a program for when my character moves. here is my code

while True:
   for event in pygame.event.get():
       if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if(event.key == K_RIGHT):
            playerPos[0] +=1
    if (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1:
        playerPos[0] =+ 1

here is the error

Traceback (most recent call last):
  File "H:\python things\firstgame.py", line 90, in <module>
if (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1:
AttributeError: 'Event' object has no attribute 'key'

Upvotes: 1

Views: 6845

Answers (3)

Greedy Cat
Greedy Cat

Reputation: 36

At first, you have to check what type of event is. In pygame only the KEYDOWN and KEYUP events have a key attribute. So it will work if you do this:

if event.type == event.KEYDOWN:
    if event.key == event.K_RIGHT:
        playerPos[0] += 1

Upvotes: 0

Craig A Gomez
Craig A Gomez

Reputation: 139

In case anyone comes across this post and had same issue, I fixed my error and ended up with this. added comments to help others understand

while True:
    #get all the user events
    for event in pygame.event.get():

        #if user wants to quit
        if event.type == pygame.locals.QUIT:
            #and the game close the window
            pygame.quit()
            sys.exit()

        #if a key is pressed
        elif event.type == pygame.locals.KEYDOWN:
            #if right arrow is pressed
            if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
                #change player's x postion
                playerPos[0] +=1
            if event.key == K_LEFT and playerPos[0] > 0:
                #change player x position
                playerPos[0] -=1
            if event.key == K_UP and playerPos[0] > 0:
                #change players x position
                playerPos[1] -=1
            if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
                #change player x position
                playerPos[1] += 1

Upvotes: 3

OLIVER.KOO
OLIVER.KOO

Reputation: 5993

I think the problem is if (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1: is outside of the forloop so the event doesn't have a key attribute.

the event inside the forloop has a key attribute because of pygame.event.get(): move that line inside of the forloop and that should solve your problem.

while True:
   for event in pygame.event.get():
       if event.type == QUIT:
            pygame.quit()
            sys.exit()
       elif event.type == KEYDOWN:
            if(event.key == K_RIGHT):
            playerPos[0] +=1
       elif (event.key == K_RIGHT) and playerPos[0] < MAPWIDTH -1:
            playerPos[0] =+ 1

I would also suggest you combine line 87 and 88 into one like such elif(event.type == KEYDOWN) and (event.key == K_RIGHT):

I hope this helps.

Upvotes: 1

Related Questions