1

I have a pygame code which I wanted to be executed for only X seconds, I tried

sleep(10)
pygame.quit()

It just continues to run.

2 Answers2

0

Change

sleep(10)
pygame.quit()

to

from sys import exit
sleep(10)
pygame.quit()
exit()
Will
  • 4,942
  • 2
  • 22
  • 47
  • This is invalid because `sleep()` comes from `time.sleep()`, which pauses the entire program for the amount of seconds passed – Anthony Pham Jan 12 '17 at 01:42
0

Though the program could exit a few milliseconds off your actual time, you could try to use the method defined in this answer and constantly check by resetting time.time() repeatedly in a while loop which should be fairly easy since you are using PyGame after all:

import time
start_time = time.time()

while True:
    if time.time() - start_time() >= 10:
        pygame.quit()
Community
  • 1
  • 1
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38