1

I am doing a pygame project and it is supposed to have only 4 fps which makes it a frame every 0.25 seconds. My problem is that when you click briefly on a key, the event might not get detected because the program does not check for events over the whole 0.25 seconds but rather once every 0.25 seconds, which makes it easy for it to miss out on an event. Is there a way to solve this problem in pygame? (I am using clock.tick() to set the fps)

Enrique Metner
  • 169
  • 1
  • 9

1 Answers1

2

I cannot provide a solution for your particular situation as you do not show us your code. However

My problem is that when you click briefly on a key, the event might not get detected

No that is not true. All events are detected. pygame.event.get() returns all the event that have been occurred since the last call of this function. See How to get keyboard input in pygame?.
Your game logic may be wrong and you are setting a status when the KEYDOWN event occurs, but rest it when the KEYUP event occurs. This will cause the behavior.

It is even possible to run the event loop more often than the application loop. Run the application loop and event loop at a higher frame rate, but run the game at only 4 FPS. For example, run the application loop at 40 FPS. Use a frame counter and run the game when the counter reaches 10:

clock = pygame.time.Clock()
frame_count = 0
run = True
while run:

    clock.tick(40)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    frame_count = (frame_count +1) % 10
    if frame_count == 0:

        # run game and do drawing
        # [...]

        pygame.display.flip()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174