I have a game that I've written for my first project and I'd like to have a system where I can play and pause the game. When you click the unpause button, I want it to call a function every 1 second that advances the date. Time.sleep stops the whole program so it's not useful to me and I cant seem to restart threads after I've started one. Here's the advancing day function.
def time():
global timer
timer = threading.Timer(1.0, time)
timer.start()
thirtyonemonths = [1, 3, 5, 7, 8, 10, 12]
thirtymonths = [4, 6, 9, 11]
globalvars.day = globalvars.day + 1
for self in thirtyonemonths:
if self == globalvars.month:
print "hi"
if globalvars.day == 32:
globalvars.day = 1
globalvars.month = globalvars.month + 1
for self in thirtymonths:
if self == globalvars.month:
print "hi"
if globalvars.day == 31:
globalvars.day = 1
globalvars.month = globalvars.month + 1
if globalvars.month == 2:
print "hi"
if globalvars.day == 29:
globalvars.day = 1
globalvars.month = globalvars.month + 1
if globalvars.month == 13:
globalvars.month = 1
globalvars.year = globalvars.year + 1
threading.Thread.time(self)
timer = threading.Timer(1.0, time)
Later I have the code for when the button is clicked that checks if it's paused or not
if b.collidepoint(pos):
if globalvars.ispaused == 1:
globalvars.ispaused = 0
timer.start()
break
if globalvars.ispaused == 0:
globalvars.ispaused = 1
timer.cancel()
break
Everything works perfectly up until I press the button a third time. Does anyone know a way I can restart the thread or maybe use a different method to do what I want?