0

I'm trying to count every 0.1 second inside for loop with event timer

self.time_interval = 500  # 500 milliseconds == 0.1 seconds
self.timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(self.timer_event, self.time_interval, 1)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.QUIT
                sys.exit()
            if event.type == pygame.MOUSEBUTTONUP:
                for cell in self.groundcells:
                    if cell.collidepoint(pygame.mouse.get_pos()):
                        pygame.time.set_timer(self.timer_event, self.time_interval, 1)
            for i in range(5):
                if event.type == self.timer_event:
                    print('hi')

It's just fragments from code, but it's should be enough I think. So, as a result I've got displayed 5 times 'hi', but its appears at one time, which is not correct. Could be great if somebody can explain me how to fix that. If you can provide code for example as well, please, make so. Thank a lot guys.

AsasaD
  • 1
  • 1
  • 500ms is definitely not equal to 0.1 seconds. It's 0.5 seconds. – Tim Roberts Dec 15 '22 at 19:32
  • Doesn't matter to be honest. It's not affect to code at all. – AsasaD Dec 15 '22 at 19:40
  • The real problem is 'hi' don't printed every iteration, just once and all of them at one time. – AsasaD Dec 15 '22 at 19:42
  • Right, because `event.type` never changes during that inner loop. You need to move the `if event.type == self.timer_event` out one level, and then use a separate counter to detect when you've had 5 of them come in, not a `for` loop. – Tim Roberts Dec 15 '22 at 19:46
  • Could you provide some example? Sorry, can't understand how to do it. Thank you. – AsasaD Dec 15 '22 at 20:03
  • Really? I gave you the recipe. Set `countdown=5` before the loop. Delete `for i in range(5)` and shift the `if` statement left one place. Add `if countdown:` / `print('hi')` / `countdown -= 1`. – Tim Roberts Dec 15 '22 at 20:13

0 Answers0