title: How to implement a Pygame timed game loop?
body: <p>I am looking to write a better game loop in Python using <code>pygame.time.Clock()</code>, and I understand the concept of keeping time and de-coupling rendering from the main game loop in order to better utilise the ticks. I also understand about passing time lag into the rendering so that it renders the correct amount of movement, but the only examples I've found are written in C# and although I first thought it fairly simple to convert to Python, it's not behaving.</p>
  
  <p>I've figured out that Pygame's <code>Clock()</code> already works out the milliseconds between the last 2 calls to .tick, and I've tried to adapt the sample code I found, but I really need to see a working example written in Python. Here's what I've come up with so far:</p>
  
  <pre><code>FPS = 60
  MS_PER_UPDATE = 1000 / FPS
  lag = 0.0
  clock = pygame.time.Clock()
  running = True
  
  # Do an initial tick so the loop has 2 previous ticks.
  clock.tick(FPS)
  
  while running:
      clock.tick(FPS)
      lag += clock.get_time()
      user_input()
  
      while lag &gt;= MS_PER_UPDATE:
          update()
          lag -= MS_PER_UPDATE
  
      render(lag / MS_PER_UPDATE)
  </code></pre>
  
  <p>I'm not sure if this is all worth it in Pygame, or if it's already taken care of in some of it's <code>time</code> functions already? My game runs slower on the laptop (expected) but I thought doing this might even out the FPS a bit between my main PC and laptop by de-coupling the rendering. Does anyone have experience doing these advanced game loops in Pygame? I just want it to be as good as it can be...</p>