first time poster here, so I hope you guys can help me :)
I'm working on a project in which I want to play a game of SET. It all works (JEEJ), however I want to be able to use some sort of time function. This will do the following:
at start of game the time starts running
if x:
y
reset timer to zero
elif not x and time == 30:
do some action
I tried a lot of things; using time.time(), but this cannot be reset as far as I know; I found a few stopwatch like classes which I tried using, but they were slow (?); I tried perf_counter()... But now I'm at loss, so I hope any of you may know what to do... Note that I want to "play" the game and do actions while the time runs... Many thanks in advance!
There are a few ways to solve this problem. One is using time:
import time
timer_start = time.time()
if x:
y
timer_start = time.time()
if not x and time.time() >= timer_start + 30:
do some action
Note that I use >= because the time is highly unlikely to be exactly 30.0, better to fire it the first time after.
Another way is to use pygame.time.set_timer()
:
pygame.time.set_timer(pygame.USEREVENT, 30000) #milliseconds
# You cal also use USEREVENT+1, USEREVENT+2 etc. if you want multiple timers
if x:
pygame.time.set_timer(pygame.USEREVENT, 30000) #milliseconds
for event in pygame.event.get(): #merge this with your actual event loop
if event.type == pygame.USEREVENT:
if not x:
y
# reset the timer since it repeats by default
pygame.time.set_timer(pygame.USEREVENT, 0)