title: How to move a Pygame object and erase it in its previous position?
body: <p>I'm trying to build a game that involves a small red player (rectangle) being controlled by the arrow keys. There's a white grid on top of a black background, 2 different coloured objectives (rectangles), and several random red boxes (rectangles).</p>
  
  <p>The part I need help with is moving the small red player. I can move it, but it seems to draw itself in the new position, but the version of the rectangle that was previously drawn stays there, forming a line. I want the entire rectangle to move and not leave any traces/previous versions of itself.</p>
  
  <p>According to some other posts, I've heard that the only way to do this is to fill the screen with the background colour (in this case, black) and redraw the players on top of it; however, this is really hard in my case as I have the red boxes and objectives placed randomly, so every time I draw them again, they draw in a new random position, not in their old positions. I want the red boxes and objectives to stay in the same position, but have the player rectangles move around (while basically deleting the older versions of themselves).</p>
  
  <p>Here's the code I currently have (I've excluded the basics, like defining colours, imports, and setting the screen height/width):</p>
  
  <pre><code>p1_velocity_x = 0
  p1_velocity_y = 0
  p2_velocity_x = 0
  p2_velocity_y = 0
  
  
  def grid():
      for col in range(0, screen_width - 100, 10):
          for row in range(0, screen_height, 10):
              screen.set_at((row, col), white)
  
  
  def red_boxes():
      for i in range(100):
          pygame.draw.rect(screen, red, (randrange(1, 1200), randrange(1, 470), 25, 25))
  
  
  def blue_score_box():
      pygame.draw.rect(screen, blue, (randrange(1, 1200), randrange(1, 470), 25, 25))
  
  
  def yellow_score_box():
      pygame.draw.rect(screen, yellow, (randrange(1, 1200), randrange(1, 470), 25, 25))
  
  
  class Player:
      color = (0, 0, 0)
  
      def __init__(self, color):
          self.x = 0
          self.y = 0
          self.color = color
  
  
  p1 = Player(red)
  p1.x = randrange(1, 1200, 10)
  p1.y = randrange(1, 470, 10)
  
  p2 = Player(yellow)
  p2.x = randrange(1, 1200, 10)
  p2.y = randrange(1, 470, 10)
  
  
  while True:
      for event in pygame.event.get():
          if event.type == QUIT:
              pygame.quit()
              sys.exit()
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_RIGHT:
                  p1.x += 10
  
      screen.fill(black)
      grid()
      red_boxes()
      yellow_score_box()
      blue_score_box()
  
      pygame.draw.rect(screen, p1.color, (p1.x, p1.y, 10, 10))
      pygame.draw.rect(screen, p2.color, (p2.x, p2.y, 10, 10))
  
      pygame.display.update()
  </code></pre>
  
  <p>The result of the above code is that I'm able to move the red rectangle as I want, but since I call the red_boxes(), yellow_score_box(), and blue_score_box() methods in the while loop, they keep calling indefinitely, drawing random red, blue, and yellow rectangles all over the screen almost every second. I want them to stay in one place while having the functionality of moving the player as I do now.</p>