Jul-20-2022, 02:37 AM
By using pygame.time.get_ticks.
This is how I normal handle it. This handle three different ways. one shot timer, repeat timer, and trip counter timer.
This is how I normal handle it. This handle three different ways. one shot timer, repeat timer, and trip counter timer.
class Ticker: def __init__(self, ticks, interval): self.tick = ticks + interval self.interval = interval self.count = 0 # One Shot Ticker def elapsed(self, ticks): return ticks >= self.tick def reset(self, ticks, interval=None): if interval: self.interval = interval self.tick = ticks + self.interval # Repeat Ticker def tick(self, ticks): if ticks >= self.tick: self.tick = ticks + self.interval return True return False # Trip Counter. Keep track when interval are low. def tick_count(self, ticks): self.count = 0 while ticks >= self.tick: self.tick += self.interval self.count += 1 return bool(self.count)In main loop. You can do something like this.
ticks = pygame.get_ticks() ticker = Ticker(ticks, 2000) # 2000 milliseconds equals 2 seconds. # In Main Loop ticks = pygame.get_ticks() if ticker.elasped(ticks): ticker.count += 1 if ticker.count < 2: # set next ticker if needed ticker.reset(ticks, 3000) if ticker.count == 0: # draw your stuff elif ticker.count == 1: # draw your stuff
99 percent of computer problems exists between chair and keyboard.