How To Make A Timer In Python Without Freezing Up The Entire Code
I am coding Mario using Pygame and I'm coding the blocks right now. I want to make a timer so when I hit the block after a second the block goes back to its original position to ma
Solution 1:
from threading import Timer
def timeout():
# do your stuff here
t = Timer(number_of_seconds, timeout)
t.start()
Solution 2:
I would suggest you use the threading module and thread two functions.
At the end, you can format as such:
from threading import Thread
if __name__ =='__main__':
Thread(target = game()).start()
Thread(target = timer()).start()
Another option is the timeit module. You can set a timer and check when a certain time is hit with a if/while statement.
A third option is with the time module you used for sleep:
import time
max_time = #the time you want
start_time = time.time()
while (time.time() - start_time) < max_time:
game()
Solution 3:
import sys
import time
import os
counter=0
s = 0
m = 0
n = int(input("Till How Many Seconds do you want the timer to be?: "))
print("")
while counter <= n:
sys.stdout.write("\x1b[1A\x1b[2k")
print(m, 'Minutes', s, 'Seconds')
time.sleep(1)
s += 1
counter+=1if s == 60:
m += 1
s = 0print("\nTime Is Over Sir! Timer Complete!\n")
beepPath = ("C:\\Users\\Farzeen Zargar\\Desktop\\Fizz Folder\\Videos\\Youtube Video Edits")
songs = os.listdir(beepPath)
os.startfile(os.path.join(beepPath, songs[13]))
Post a Comment for "How To Make A Timer In Python Without Freezing Up The Entire Code"