Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python loop delay without time.sleep()

In an MMO game client, I need to create a loop that will loop 30 times in 30 seconds (1 time every second). To my greatest disappointment, I discovered that I can not use time.sleep() inside the loop because that causes the game to freeze during the loop.

The loop itself is pretty simple and the only difficulty is how to delay it.

limit = 31
while limit > 0 :
  print "%s seconds remaining" % (limit)
  limit = limit -1

The python libs exist in the client as .pyc files in a separate folder and I'm hoping that I can avoid messing with them. Do you think that there is any way to accomplish this delay or is it a dead end?

like image 765
mortaZa Avatar asked May 31 '12 19:05

mortaZa


Video Answer


1 Answers

Your game has a main loop. (Yes, it does.)

Each time through the loop when you go to check state, move the players, redraw the screen, etc., you check the time left on your timer. If at least 1 second has elapsed, you print out your "seconds remaining" quip. If At least 30 seconds has elapsed, you trigger whatever your action is.

like image 128
tylerl Avatar answered Nov 15 '22 00:11

tylerl