I'm trying to move a monkey with arrow keys but every time I try to move it, the monkeys leaves a black trail behind

import pygame
from pygame.locals import *

pygame.init()

clock = pygame.time.Clock()
fps = 60

screen_width = 864
screen_height = 936

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Flappy Bird')


#define game variables
ground_scroll = 0
scroll_speed = 4
flying = False
game_over = False

#load images
bg = pygame.image.load('forest.png')
ground_img = pygame.image.load('ground.png')
Monkey = pygame.sprite.Sprite()
Monkey.image = pygame.image.load("monkey.png")
#boundaries
Monkey.rect = Monkey.image.get_rect()       
x=400
y=200
movex=0
movey=0

    #draw background
screen.blit(bg, (0,0))

    #draw the ground
screen.blit(ground_img, (ground_scroll, 768))

while(True):
  Monkey.rect.topleft=(x,y)

  screen.blit(Monkey.image, Monkey.rect)
  x=x+movex
  y=y+movey
  pygame.display.update()
  event=pygame.event.poll()
  
  if(event.type==pygame.QUIT):
    break
  elif(event.type==pygame.KEYDOWN):
    #KEYDOWN means a key is pressed
    if(event.key==pygame.K_RIGHT):
      movex=3
    elif(event.key==pygame.K_LEFT):
      movex=-3
    elif(event.key==pygame.K_UP):
        movey=-3
    elif(event.key==pygame.K_DOWN):
      movey=3
    elif(event.type==pygame.KEYUP):
      movex=0
  elif (event.type==pygame.KEYUP): 
    movey = 0
    movex = 0
    pygame.display.update()

  

pygame.quit()

This is my code, it's supposed to be a monkey in a forest and I want to be able to control it with the arrow keys without the monkey leaving black trail behind.For some reason every time I move the monkey, these black like leave a trail and cover the whole screen. I just can't figure out how to get rid of them.


So pygame is interesting in the way it implements animation. It is not moving your monkey but actually redrawing it over and over again on the same screen. What you need to do is redraw your background over and over on top of the old images.

<code>
     #draw background
     screen.blit(bg, (0,0))
     #draw the ground
     screen.blit(ground_img, (ground_scroll, 768))
     while(True):
         Monkey.rect.topleft=(x,y)
</code>

instead

<code>
while(True):
    #draw background
    screen.blit(bg, (0,0))
    #draw the ground
    screen.blit(ground_img, (ground_scroll, 768))
    Monkey.rect.topleft=(x,y)
</code>