0
from time import time
import pygame
pygame.init()

t = time()

Events = pygame.event.get()
print(Events)

end = False

while not end:
    if time()-t>3:
        print(Events)
        Events = pygame.event.get()
        t = time()

I wrote the following to know about the event queue in pygame. Here I am waiting for three seconds until the next event.get() is called and in these 3 seconds, I do a lot of events through my keyboard and mouse,

But still I see a blank event queue in the next print...

Why is it so because if I am not wrong, pygame queues all the events that happen and event.get() returns us the queue and then clears it.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Prakhar
  • 17
  • 4

1 Answers1

0

You have to create a Pygame window to get the IO events for the keyboard and mouse:

import pygame
pygame.init()

pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

end = False
while not end:
    clock.tick(1)

    events = pygame.event.get()
    if events:
        print(events)

    for event in events:
        if event.type == pygame.QUIT:
            end = True

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Prakhar `tick` reduces the frames per second. You can remove it or changed it e.g. `tick(100)`. Without `tick` you'll use 100% cpu. See [`tick`](https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick). I'm using `pygame.time.Clock.tick` instead of `time`. Pygame is based on SDL2. The event handling is related to the application window (at least for the IO events). – Rabbid76 Dec 28 '21 at 08:19