简体   繁体   中英

Pygame Event.key return

Using Python 3.3 , Pygame 1.9 , PygCurse , and Eclipse with the PyDev plugin

I have a simple loop that I need to exit when the player presses enter. Using input() is not an acceptable substitute as the loop will eventually perform different actions based on which key is pressed. So essentially, I need to test for which key is pressed and then perform an action. I am currently only testing for the enter key. Here is the loop:

 while 1:
     event = pygame.event.poll()
     if event.type == pygame.QUIT:
        break;
     if (event.type == pygame.KEYDOWN) & (event.key == pygame.K_RETURN):
         break

The error this returns is:

 if (event.type == pygame.KEYDOWN) & (event.key == pygame.K_RETURN):
 AttributeError: 'Event' object has no attribute 'key'

All of the examples I've seen (from http://nullege.com/codes/search?cq=pygame.event.poll ) use event.key to identify which key is being pressed so I'm either obviously missing something in the code above or there was some kind of syntax change that I missed and I'm only looking at old examples.

Your problem can be solved by simply changing & to and . Python uses and for a logical "and" comparison. & is used for a bitwise "and" comparison.

while 1:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
       break
    if (event.type == pygame.KEYDOWN) and (event.key == pygame.K_RETURN):
       break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM