title: pygame - on hold button down
body: <p>I found a solution to make a sprite move when you hold a key down.  The problem is that it forces writing ugly duplicated code.  The current solution I found is:</p>
  
  <pre><code>       for event in pygame.event.get():
              if event.type == KEYDOWN:
                  keystate = pygame.key.get_pressed()
                  while keystate[K_RIGHT]:
                      screen.fill((255,255,255))
                      pygame.event.get()
  
                      for sprite in sprites:
                          rimage = sprite[1].getimage()
  
                          if sprite[2] is None:
                              x+=3
                              sprite[1].update(time)
                              screen.blit(rimage, (x,y))
                              if sprite[1].isfinished() == True:
                                  sprite[1].reset()
                              last_dir = "right"
                              if x &gt;= screen_width - rimage.get_width():
                                  x = screen_width - rimage.get_width()
  
                      #update player sprite movement
                      #update player sprite animation
                      #update rest of game map
  
                      keystate = pygame.key.get_pressed()
                      time = pygame.time.get_ticks()
  
  
                      pygame.display.update()
  </code></pre>
  
  <p>The problem is that the while keystate block.  It has to be repeated for each direction and the game world needs to be updated in each while block.  That is five places where the same code needs to be duplicated....4 directions plus if a key is not pressed.  I could wrap it in a function but I was wondering if there was a better way to handle holding down a button in pygame.</p>