Pygame, move a square on the screen, but can not erase the previous movements
<p>This is my first post in StackOverflow, hope you guys can help a newbie programmer. It's Pygame Python simple ask.</p>

<p>I am trying to move a square on the screen, but can not erase the previous movements.</p>

<pre><code>import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100


def desenha():
    quadrado = pygame.Rect(cordX, cordY ,50, 52)

    pygame.draw.rect(screen, (255, 0, 0), quadrado)
    pygame.display.flip()



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento);
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:     
            if evento.key == pygame.K_SPACE:
                   print('GAME BEGIN')
                   desenha()
                   GAME_BEGIN = True;
            if evento.key == pygame.K_LEFT and GAME_BEGIN:   
                   speedX=-3
                   cordX+=speedX
                   desenha()


            if evento.key == pygame.K_RIGHT and GAME_BEGIN:
                   speedX=3
                   cordX+=speedX
                   desenha()   
</code></pre>

<p>You also can draw everything from a previous surface, before the square was in place, then draw the square on it always in a different place.
Then draw the new surface to screen each time.</p>

<p>But not really good strategy, it can be slow and/or induce flickering.</p>

<p>For instance:</p>

<pre><code># Draw anything to the display surface named screen, then do:
# By display surface I mean the surface returned by pygame.display.set_mode()
orig = screen.copy()
# Sometimes it will not be filled with a image from screen that you have drawn before, so you draw
# all to orig surface
# Then you make a surface that will be shown on the screen and do:
s = pygame.surface.Surface()
s.blit(orig, 0, 0)
# Then you draw the rectangle or whatever on s, and then:
screen.blit(s, 0, 0)
pygame.display.flip()
# and you keep orig as a starting point for every move
</code></pre>

<p>If you have many movable objects on the screen simultaneously  this is good, or if you are refreshing restricted areas only, else redraw over the last image as suggested in other answer.</p>

<p>If you go about drawing directly to the display surface flicker will be bigger and sometimes, especially when display is hardware accelerated you will have problems. On non desktop devices like mobile phones especially. Drawing over and adding new usually works fine directly on display surface and would be the correct approach to your problem.</p>

<p>I know I sound contradictive, but some stuff you simply must see for yourself. Get experience by experimenting.</p>

python pygame move rect