title: Throttle keypresses in pygame
body: <p>I was recently coding a platformer game, when I faced into a problem: The user could spam space to jump infinitely.</p>
  <p>Here is my code:</p>
  <pre><code>def __init__(self, pos):
      super().__init__()
      self.image = pygame.Surface((32, 64))
      self.image.fill(&quot;green&quot;)
      self.rect = self.image.get_rect(topleft = pos)
      self.direction = pygame.math.Vector2(0, 0)
      self.speed = speed
      self.gravity = gravity
      self.jump_height = jump_height
  
  def get_input(self):
      keys = pygame.key.get_pressed()
      if keys[pygame.K_LEFT] and keys[pygame.K_RIGHT]: pass
      elif keys[pygame.K_LEFT]: self.direction.x = -1
      elif keys[pygame.K_RIGHT]: self.direction.x = 1
      else: self.direction.x = 0
      if keys[pygame.K_SPACE]:
          self.jump()
  </code></pre>
  <p>I tried several things, such as implementing a <code>self.antispam</code> boolean, set to <code>True</code>, at the <code>__init__</code> method that turns into <code>False</code> when the space key is pressed, and then turns <code>True</code> again after the jump method, or turning the <code>jump</code> method into a coroutine to make an asyncio loop, but none of them worked.</p>