Timer for Python game

前端 未结 10 1858
伪装坚强ぢ
伪装坚强ぢ 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 03:04

    New to the python world!
    I need a System Time independent Stopwatch so I did translate my old C++ class into Python:

    from ctypes.wintypes import DWORD
    import win32api
    import datetime
    
    class Stopwatch:
    
        def __init__(self):
            self.Restart()
    
        def Restart(self):
            self.__ulStartTicks = DWORD(win32api.GetTickCount()).value
    
        def ElapsedMilliSecs(self):
            return DWORD(DWORD(win32api.GetTickCount()).value-DWORD(self.__ulStartTicks).value).value
    
        def ElapsedTime(self):
            return datetime.timedelta(milliseconds=self.ElapsedMilliSecs())
    

    This has no 49 days run over issue due to DWORD math but NOTICE that GetTickCount has about 15 milliseconds granularity so do not use this class if your need 1-100 milliseconds elapsed time ranges.

    Any improvement or feedback is welcome!

提交回复
热议问题