Timer for Python game

前端 未结 10 1953
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 02:38

I\'m trying to create a simple game where the point is to collect as many blocks as you can in a certain amount of time, say 10 seconds. How can I get a timer to begin ticking a

10条回答
  •  野性不改
    2021-01-31 02:57

    import time
    
    now = time.time()
    future = now + 10
    while time.time() < future:
        # do stuff
        pass
    

    Alternatively, if you've already got your loop:

    while True:
        if time.time() > future:
            break
        # do other stuff
    

    This method works well with pygame, since it pretty much requires you to have a big main loop.

提交回复
热议问题