Timer for Python game

前端 未结 10 1811
伪装坚强ぢ
伪装坚强ぢ 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:09
    1. Asks you when to stop [seconds]
    2. Adds '0' at starting [1-9]
    import time
    import sys
    
    stop = int(input('> '))
    second = 0
    print('> Stopwatch Started.')
    
    while stop > second:
        if second < 9:
            second = second + 1
            time.sleep(1)
            sys.stdout.write('\r> ' + '0' + str(second))
        else:
            second += 1
            time.sleep(1)
            sys.stdout.write('\r' + '> ' + str(second))
    
    print('\n> Stopwatch Stopped.')
    
    0 讨论(0)
  • 2021-01-31 03:10

    This is the Shortest Way I know of doing it:

    def stopWatch():
            import time
            a = 0
            hours = 0
            while a < 1:
                for minutes in range(0, 60):
                    for seconds in range(0, 60):
                        time.sleep(1)
                        print(hours,":", minutes,":", seconds)
            hours = hours + 1
    
    0 讨论(0)
  • 2021-01-31 03:11

    Using time.time()/datetime.datetime.now() will break if the system time is changed (the user changes the time, it is corrected by a timesyncing services such as NTP or switching from/to dayligt saving time!).

    time.monotonic() or time.perf_counter() seems to be the correct way to go, however they are only available from python 3.3. Another possibility is using threading.Timer. Whether or not this is more reliable than time.time() and friends depends on the internal implementation. Also note that creating a new thread is not completely free in terms of system resources, so this might be a bad choice in cases where a lot of timers has to be run in parallel.

    0 讨论(0)
  • 2021-01-31 03:19

    As a learning exercise for myself, I created a class to be able to create several stopwatch timer instances that you might find useful (I'm sure there are better/simpler versions around in the time modules or similar)

    import time as tm
    class Watch:
        count = 0
        description = "Stopwatch class object (default description)"
        author = "Author not yet set"
        name = "not defined"
        instances = []
        def __init__(self,name="not defined"):
            self.name = name
            self.elapsed = 0.
            self.mode = 'init'
            self.starttime = 0.
            self.created = tm.strftime("%Y-%m-%d %H:%M:%S", tm.gmtime())
            Watch.count += 1
    
        def __call__(self):
            if self.mode == 'running':
                return tm.time() - self.starttime
            elif self.mode == 'stopped':
                return self.elapsed
            else:
                return 0.
    
        def display(self):
            if self.mode == 'running':
                self.elapsed = tm.time() - self.starttime
            elif self.mode == 'init':
                self.elapsed = 0.
            elif self.mode == 'stopped':
                pass
            else:
                pass
            print "Name:       ", self.name
            print "Address:    ", self
            print "Created:    ", self.created
            print "Start-time: ", self.starttime
            print "Mode:       ", self.mode
            print "Elapsed:    ", self.elapsed
            print "Description:", self.description
            print "Author:     ", self.author
    
        def start(self):
            if self.mode == 'running':
                self.starttime = tm.time()
                self.elapsed = tm.time() - self.starttime
            elif self.mode == 'init':
                self.starttime = tm.time()
                self.mode = 'running'
                self.elapsed = 0.
            elif self.mode == 'stopped':
                self.mode = 'running'
                #self.elapsed = self.elapsed + tm.time() - self.starttime
                self.starttime = tm.time() - self.elapsed
            else:
                pass
            return
    
        def stop(self):
            if self.mode == 'running':
                self.mode = 'stopped'
                self.elapsed = tm.time() - self.starttime
            elif self.mode == 'init':
                self.mode = 'stopped'
                self.elapsed = 0.
            elif self.mode == 'stopped':
                pass
            else:
                pass
            return self.elapsed
    
        def lap(self):
            if self.mode == 'running':
                self.elapsed = tm.time() - self.starttime
            elif self.mode == 'init':
                self.elapsed = 0.
            elif self.mode == 'stopped':
                pass
            else:
                pass
            return self.elapsed
    
        def reset(self):
            self.starttime=0.
            self.elapsed=0.
            self.mode='init'
            return self.elapsed
    
    def WatchList():
        return [i for i,j in zip(globals().keys(),globals().values()) if '__main__.Watch instance' in str(j)]
    
    0 讨论(0)
提交回复
热议问题