0

Good day! Judging by the documentation, the line pygame.time.set_timer(pygame.USEREVENT, 1000) sets a timer for 1s, after which the USEREVENT event will be created. I have this line in an infinite loop, updated every ~33ms. Then, as far as I understand, USEREVENT should not be received at all, because every 33ms iteration I update the timer by 1000ms.

import pygame
pygame.init()
 
sc = pygame.display.set_mode((500, 604))
clock = pygame.time.Clock()
while True:
    pygame.time.set_timer(pygame.USEREVENT, 1000)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.USEREVENT:
            print('event')
    clock.tick(30)

But in practice, something else happens: sometimes an event is not thrown for several seconds, sometimes several pieces are thrown: one event every one or two iterations. Maybe I don't understand something about how pygame.time.set_timer() works?

The presented code does not make sense, it only illustrates the problem that I encountered

I write in PyCharm

FoRBeR
  • 1
  • What do you mean *work incorrectly*? What is your result and the expected result? Please add these details so we can understand your code. – BrainFl Mar 29 '22 at 11:26
  • Which versions of Python and PyGame are you using? I don't see any events with PyGame `2.1.2` and Python `3.10.2`. – import random Mar 29 '22 at 14:12
  • The way it should be described above the code block, how it works in practice below the code block. The version of Pygame was 2.0.1, after the update it began to work many times better. A couple of times I caught this event, but in the vast majority of tests passed as it should. Apparently it was a bug in the old version. Thanks! – FoRBeR Mar 29 '22 at 14:39
  • `set_timer` does not update the time on which an event will be released, it creates a new timer which releases an event after the given time. The old one is not deleted, so you should always receive the event when you use `set_timer`. You can't overwrite a previously set timer. – The_spider Mar 31 '22 at 18:45

0 Answers0