title: How to make a character move while key is held down
body: <p>I'm trying to create a "player" - black square - which moves when you hold down WASD. I tried looking around here, on google and on youtube on how to make this work, but every solution I've tried has the same problem: instead of moving it while I hold down the key, I have to tap the key constantly to make it move in small bits. I've no clue what I'm doing wrong. Here's the code (using python 3.3 - pygame 1.9):</p>
  
  <pre><code>import pygame
  from pygame.locals import *
  from pygame.time import *
  import sys
  pygame.init()
  
  velX = 0
  velY = 0
  
  
  running = True
  clock = pygame.time.Clock()
  
  def draw():
      global velX
      global velY
  
      playerx = 20
      playery = 20
  
      screen = pygame.display.set_mode((700,300))
      pygame.display.set_caption('something')
  
      background = pygame.Surface(screen.get_size())
      background = background.convert()
      background.fill((255,255,255))
      screen.blit(background, (0,0))
  
      playerx = playerx + velX
      playery = playery + velY
      player_filename = 'player.png'
      player = pygame.image.load(player_filename)
      screen.blit(player, (playerx,playery))
  
      pygame.display.flip()
  
  def main():
      global velX
      global velY
      global running
  
      while running:
  
          keys_down = pygame.key.get_pressed()
          pygame.key.set_repeat(1, 50)
          time = 50/1000
  
          for event in pygame.event.get():
              if event.type == QUIT:
                  pygame.quit()
                  sys.exit()
              elif event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_ESCAPE:
                      running=False
  
          if keys_down[K_d]:
              velX += 50*time
          if keys_down[K_w]:
              velY -= 50*time
          if keys_down[K_s]:
              velY += 50*time
          if keys_down[K_a]:
              velX -= 50*time
  
          clock.tick(50)
          draw()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>I already tried the set repeat command, but it didn't seem to do much anything. I also tried directly copying from a few solutions I found here on stackoverflow, but none of them worked. I suppose there is something else wrong in the code.</p>