title: Python and tile based game: limiting player movement speed
body: <p>I've been putting together an isometric tile-based RPG using Python and the Pyglet library. I've run into the following problem, however:</p>
  
  <p>My player movement is based on positions on the three-dimensional array that consists of tiles. To limit movement speed, I use a global variable: TILE_TO_TILE_DELAY = 200.</p>
  
  <p>TILE_TO_TILE_DELAY is supposed to be the amount of time in milliseconds it takes for the player to move from one tile to another. During this time, they should not be able to make a new movement.</p>
  
  <p>The system I've been using is that I have a timer class, like this:</p>
  
  <pre><code>import time
  
  def GetSystemTimeInMS(): #Return system time in milliseconds
      systime = round((time.clock()*1000), 0)
      return systime
  
  class Timer:
  def __init__(self,time):
      #time = time for which the timer runs
      self.time = time
      self.start_time = 0
      self.stop_time  = 0
  
  def StartTimer(self):
      #start_time = time at which the timer was started
      self.start_time = GetSystemTimeInMS()
      self.stop_time  = self.start_time + self.time
  
  def TimerIsStopped(self):
      if GetSystemTimeInMS() &gt;= self.stop_time:
          return True
      else:
          return False
  </code></pre>
  
  <p>The player class has a Timer object:</p>
  
  <pre><code>self.MoveTimer = Timer(TILE_TO_TILE_DELAY)
  </code></pre>
  
  <p>When the player presses the W-key, a function is called that checks for player.MoveTimer.TimerIsStopped(). If it returns True, it calls player.MoveTimer.StartTimer() and starts a new movement to the next position.</p>
  
  <p>In the even loop, the update(dt) function is set to happen 30 times a second:</p>
  
  <pre><code>def update(dt):
      if player.MoveTimer.TimerIsStopped()
          player.UpdatePosition()
  
  pyglet.clock.schedule_interval(update, 1/30)
  </code></pre>
  
  <p>Now, by my logic, update(dt) should check 30 times a second whether or not enough time has passed to warrant the player a new movement event. However, for some reason the player moves much faster at lower FPS.</p>
  
  <p>When my FPS is around 30, the player moves much faster than in areas where there are less tile sprites, pumping the framerate to 60. In areas where FPS is high, the player indeed by my measurements moves almost twice as slowly.</p>
  
  <p>I just cannot figure it out, nor did I find anything off the internet after a day of searching. Some help would be much appreciated.</p>
  
  <p>Edit: The code that starts the MoveTimer:</p>
  
  <pre><code>def StartMovement(self, new_next_pos):
      self.RequestedMovement = False
      if self.GetCanMoveAgain():
  
          self.SetNextPos(new_next_pos)
          self.SetMoveDirection(self.GetDirection())
  
  
          #Start the timer for the movement
          self.MoveTimer.StartTimer()
          self.SetIsMoving(True)
          self.SetStartedMoving(True)
  
          self.SetWalking(True)
  
          self.SetNeedUpdate(True)
  
          self.MOVE_EVENT_HANDLER.CreateMoveEvent()
  </code></pre>
  
  <p>GetCanMoveAgain() returns the value of player.can_move_again, which is set back to True by the UpdatePosition() in update(dt)</p>