1

I'm trying to perform something for 4 seconds. How would that work?

yes = True
while True:
    if yes:
        print('yes')
        if [4 seconds pass]:
            yes = False
    elif not yes:
        print('No')

I tried

seconds = pygame.time.get_ticks()
yes = input('True or False: ')
while True:
    if yes:
        print('yes')
        time = int((pygame.time.get_ticks - seconds)/ 1000)
        if time == 4:
            yes = False
    elif not yes:
        print('no')

But it didn't work

  • What's wrong with [`pygame.time.delay()`](https://www.pygame.org/docs/ref/time.html#pygame.time.delay)? Anyway `input` waits for an input. You cannot break `input` after certain time. See [Pygame Window not Responding after few seconds](https://stackoverflow.com/questions/64830453/pygame-window-not-responding-after-few-seconds/64832291#64832291) – Rabbid76 Nov 22 '20 at 11:37
  • That doesn't work correctly as I want a small part of the program to stop for 4 seconds and not the whole program. Otherwise I might as well have used time.sleep(4) –  Nov 22 '20 at 12:25

1 Answers1

0

n pygame the system time can be obtained by calling pygame.time.get_ticks(), which returns the number of milliseconds since pygame.init() was called. See pygame.time module.
Get the current time before the loop and compute the point in time until the action needs to be performed. Get the current time in the loop and compare it to the computed time:

end_time = pygame.time.get_ticks() + 4000
while True:
    current_time = pygame.time.get_ticks()
    if yes:
        If current_time < end_time:
            # do something
            # [...]
        else:
            yes = False
    else:
        # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174