title: sprite won't move left or right in pygame
body: <p>I am trying to move a sprite (called Player) to the left or right by pressing the arrow keys. The movement to the right or left would be made by changing the center of the rectangle of the sprite.  As you might be able to see, I am trying to add/subtract 8 from the sprite's x coordinates in order to move it right or left. However, the sprite doesn't move when I hit an arrow key. How can I fix this? </p>
  
  <pre><code>import pygame
  import random
  import time
  import pygame as pg
  
  
  # set the width and height of the window
  width = 800
  height = 370
  groundThickness = 30
  pheight = 50
  pwidth = 50
  playerx = 0+pwidth
  playery = height-groundThickness-pheight/2
  fps = 30
  # define colors
  white = (255, 255, 255)
  black = (0, 0, 0)
  red = (5, 35, 231)
  # initialize pygame 
  pygame.init()
  
  # initialize pygame sounds
  pygame.mixer.init()
  # create window
  screen = pygame.display.set_mode((width, height))
  pygame.display.set_caption("my game")
  clock = pygame.time.Clock()
  
  class Player(pygame.sprite.Sprite):
          def __init__(self):
              pygame.sprite.Sprite.__init__(self)
              self.image = pygame.Surface((pwidth, pheight))
              self.image.fill(red)
              self.rect = self.image.get_rect()
              self.rect.center = (playerx, playery)
  
  all_sprites = pygame.sprite.Group()
  player = Player()
  all_sprites.add(player)
  
  # Game loop
  running = True
  x_change = 0
  while running:
      # keep loop running at the right speed
      clock.tick(fps)
      # Process input (events)
      for event in pygame.event.get():
          # check for closing window
          if event.type == pygame.QUIT:
              running = False
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_LEFT:
                  print("left")
                  x_change = -8
              elif event.key == pygame.K_RIGHT:
                  print("right")
                  x_change = 8
          if event.type == pygame.KEYUP:
              if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                  x_change = 0
          print(x_change)
          playerx += x_change
          all_sprites.update()
  
  
  
      #ground
      pygame.draw.rect(screen, (0,255,0), ((0, height-groundThickness), (width, groundThickness)))
  
  
  
      # Update
      all_sprites.update()
  
  
  
      # Draw / render
      all_sprites.draw(screen)
      pygame.display.update()
      # AFTER drawing everything, flip the display
      pygame.display.flip()
  
  pygame.quit()
  </code></pre>