[pygame] BUG: pygame.event.get(pygame.KEYDOWN) fails

2009-05-02 Thread evilmrhenry

Tested under Linux. (Python 2.5.4, pygame 1.7.1 release 4.2 from Debian)
and Windows. (with a slightly older version of python/pygame)

The code pygame.event.get(pygame.KEYDOWN) does not work correctly. If
called in this manner, it will work, but only for the first 100 or so
keypresses. After a certain point, the get() function will no longer
return the appropriate events, and will instead return an empty list, no
matter what the physical input. pygame.event.get() functions properly.

Below is some test code. When running the first program, the counter
tends to stop at around 120. The 2nd program, which should be identical,
functions as expected.



#Broken
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
counter = 0
while 1:
pygame.time.wait(60)
for event in pygame.event.get(pygame.KEYDOWN):
counter +=1
print counter

---

#Works
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
counter = 0
while 1:
pygame.time.wait(60)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
counter +=1
print counter



Re: [pygame] BUG: pygame.event.get(pygame.KEYDOWN) fails

2009-05-02 Thread René Dudfield
hi,

why this isn't working, is because when you pass in an event type it
leaves the other events in the queue.  Which means at some point the
queue fills up, and no other events can go on there.

It's not the nicest behaviour... but is working as specified.  It
would be better if it could tell you this was happening.  Maybe with a
warning message somewhere... I'm not sure the best way.  Since, for
some programs, this behaviour is ok.

So I think at least a warning in the documentation for
pygame.event.get(type) is in order.

If you clear the event queue at the end of your loop, it should work.


You can clear the events every loop like this:

#was broken, now working
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
counter = 0
while 1:
   pygame.time.wait(60)
   for event in pygame.event.get(pygame.KEYDOWN):
   counter +=1
   print counter
   pygame.event.clear()



#this is probably better...  getting all the events off the queue each round.
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
counter = 0
while 1:
   pygame.time.wait(60)
   events = pygame.event.get()

   for event in events:
   if e.type in [pygame.KEYDOWN]:
  counter +=1
  print counter




On Sun, May 3, 2009 at 8:38 AM, evilmrhenry evilmrhe...@emhsoft.com wrote:
 Tested under Linux. (Python 2.5.4, pygame 1.7.1 release 4.2 from Debian)
 and Windows. (with a slightly older version of python/pygame)

 The code pygame.event.get(pygame.KEYDOWN) does not work correctly. If
 called in this manner, it will work, but only for the first 100 or so
 keypresses. After a certain point, the get() function will no longer
 return the appropriate events, and will instead return an empty list, no
 matter what the physical input. pygame.event.get() functions properly.

 Below is some test code. When running the first program, the counter
 tends to stop at around 120. The 2nd program, which should be identical,
 functions as expected.



 #Broken
 import pygame
 pygame.init()
 screen = pygame.display.set_mode((640, 480))
 counter = 0
 while 1:
    pygame.time.wait(60)
    for event in pygame.event.get(pygame.KEYDOWN):
        counter +=1
        print counter

 ---

 #Works
 import pygame
 pygame.init()
 screen = pygame.display.set_mode((640, 480))
 counter = 0
 while 1:
    pygame.time.wait(60)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            counter +=1
            print counter