Timer for Python game

前端 未结 10 1838
伪装坚强ぢ
伪装坚强ぢ 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:59

    For a StopWatch helper class, here is my solution which gives you precision on output and also access to the raw start time:

    class StopWatch:
        def __init__(self):
            self.start()
        def start(self):
            self._startTime = time.time()
        def getStartTime(self):
            return self._startTime
        def elapsed(self, prec=3):
            prec = 3 if prec is None or not isinstance(prec, (int, long)) else prec
            diff= time.time() - self._startTime
            return round(diff, prec)
    def round(n, p=0):
        m = 10 ** p
        return math.floor(n * m + 0.5) / m
    

提交回复
热议问题