title: Pygame - Movement Acceleration
body: <p>I want to make a script in python/pygame that will make a character speed up as you hold a movement button (so, when I hold the left key, the character will accelerate to a specific speed and then the speed will even out, when I release the key it will slowly decrease in speed until stopping fully.)</p>
  
  <p>at the moment, my code will recognize that the character must speed up, however the changes in speed only occur every time I press down and release the key (e.g: first key press will have the speed at 1, second press will be two, all the way up to the fifth or sixth press where it's at max speed, before resetting.)</p>
  
  <p>I want to write my code so it means the character will accelerate whilst the key is being held down and not require multiple presses.</p>
  
  <p>here is the movement section (with a few more random bits that were around it) of my code so far:</p>
  
  <pre><code>x = (display_width * 0.45)
  y = display_height * 0.8
  x_change = 0
  negx = 0
  posx = 0
  bun_speed = 0
  
  while not crashed:
          for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                          crashed = True
              if event.type == pygame.KEYDOWN:
                      if event.key == pygame.K_LEFT:
                          posx = 0
                          if negx != -5:
                              negx = negx - 1
                              x_change = negx
                          elif negx == -5:
                              negx = 0
                              x_change = -5
                      elif event.key == pygame.K_RIGHT:
                          negx = 0
                          if posx != 5:
                              posx = posx + 1
                              x_change = posx
                          elif posx == 5:
                              posx = 0
                              x_change = 5
              if event.type == pygame.KEYUP:
                  if event.key == pygame.K_LEFT:
                          x_change = 0
                  elif event.key == pygame.K_RIGHT:
                          x_change = 0
      x += x_change
  
      display.fill(white)
      bunny(x,y)
  
      pygame.display.update()
      clock.tick(60)
  </code></pre>
  
  <p>the two variables negx and posx decrease and increase respectively depending on the key pressed. pressing the key assigned to one variable resets the other to zero, so when that variable is next called when the opposite button is pressed it will accelerate from zero. Once either variable reaches the max speed, it resets for when the button is released meaning the character can accelerate once more.</p>
  
  <p>If there's any way to make this work (as well as neaten up the code, if you're able to) then guidance as to how to get it to function would be much appreciated.</p>