[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Odd Little pygame.event Bug?



I tried your sample code and I have the same kind of problem. Whenever I have three keys pressed, the behavior seems erratic.
The comment in the doc to the pygame.key.get_pressed method implies this is normal.
Good luck.

-mats


----- Original Message ----
From: Kris Schnee <kschnee@xxxxxxxxxx>
To: pygame-users@xxxxxxxx
Sent: Monday, April 16, 2007 9:43:06 PM
Subject: [pygame] Odd Little pygame.event Bug?

While playing with my little game framework, I found I could jump in
each of eight compass directions, except northwest. Investigating the
problem, I found that when my character was moving northwest (angle 315,
where 0 is north and 90 is east), the "Jump" function wasn't getting
called. So I told the main input loop to print a message whenever the
Space (jump) key was hit.

According to Pygame, it seems, hitting the Space key while the Up and
Left arrows are held down doesn't generate a KEYDOWN event! Yet there
seems to be no problem with hitting another key, or at least K or
LSHIFT, with this arrow combination, or with any other arrow combination.

Below is a test script that replicates the error. Am I overlooking
something obvious here, or is there a glitch in pygame.event?

Kris

<code>

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480))

"""
Run a standard Pygame event loop with a blank screen. ESC to exit.
The loop prints what "angle" you're holding down with the arrow keys,
whenever you hit Space, LSHIFT, or K.
The error is that hitting Space while holding Left and Up doesn't seem
to generate a KEYDOWN event!
"""

done = False
while not done:

     report_angle = False

     ## Handle events.
     keys_pressed = pygame.key.get_pressed() ## Keys that're down.

     for event in pygame.event.get():
         if event.type == QUIT:
             done = True
         elif event.type == KEYDOWN:
             print "A key was hit: "+str(event.key)
             if event.key == K_ESCAPE:
                 done = True

             ## This doesn't seem to work!
             elif event.key == K_SPACE:
                 print "Pressed Space."
                 report_angle = True

             ## This one works.
             elif event.key == K_LSHIFT:
                 print "Pressed LeftShift."
                 report_angle = True

             ## This one works fine.
             elif event.key == K_k:
                 print "Pressed K."
                 report_angle = True

     if report_angle:
         move_x, move_y = 0, 0
         if keys_pressed[ K_UP ]:
             move_y = 1
         elif keys_pressed[ K_DOWN ]:
             move_y = -1
         if keys_pressed[ K_LEFT ]:
             move_x = -1
         elif keys_pressed[ K_RIGHT ]:
             move_x = 1
         angle =
{(0,0):None,(0,-1):180,(0,1):0,(-1,0):270,(1,0):90,(-1,1):315,(1,1):45,(-1,-1):225,(1,-1):135}[(move_x,move_y)]
         print "Angle: "+str(angle)

     pygame.display.update()

pygame.display.quit()


</code>