Pygame - How to solve ghost image effect

-1

I'm doing tests on pygame and created a "character" that moves, however it creates a ghost effect on the screen. I would like to know how to solve it.

(Thislandiswhatthecharacterwouldbe)

TheghosteffectthatIamreferringtoisthatwhenyoumove,thecharacter"walks", however it may create a "copy" of it (as shown in the image)

The code I'm going to put from the moving part:

class playerA(pygame.sprite.Sprite):
    velocidade = 20
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.ImagemPlayer = pygame.image.load("dirt.png")

        self.rect = self.ImagemPlayer.get_rect()
        self.rect.centerx = largura / 2
        self.rect.centery = altura - 60
    def colocar(self,sup):
        sup.blit(self.ImagemPlayer, self.rect)

def main():
      while sair != True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sair = True
            if(event.type == pygame.KEYDOWN):
                if event.key == pygame.K_LEFT :
                    player.rect.left -= player.velocidade
                elif event.key == pygame.K_RIGHT :
                    player.rect.right += player.velocidade
                elif event.key == pygame.K_UP :
                    player.rect.top -= player.velocidade
                elif event.key == pygame.K_DOWN :
                    player.rect.top += player.velocidade
    
asked by anonymous 20.05.2017 / 23:59

1 answer

1

Hello. This "error" occurs because there is no screen update at the end of your main loop: while sair != True , where pygame manages the events of your game. Remember that each iteration of the loop draws on the screen the objects added to its display.blit . Therefore, as there is no internal management of these particularities, since pygame is a reimagining of the SDL library applicabilities (a powerful multi-platform multimedia control C library) it is your responsibility to clear the buffer by updating the screen and the positions of your new axes in each "re-draw".

Re-draw your background screen before adding its other elements:

pygame.display.blit(fundo, (0, 0))

where pygame.display is your background variable and fundo its fill. (be it a color, an image ...)

Then, add:

pygame.display.update() at the end of your loop and your problem should be resolved.

One inside your code, there is a redundancy in the clause of your while master loop, since since the for event in pygame.event.get() internal "event" should already correspond to the application closing, the condition of your while loop becomes unnecessary and therefore, you're wasting memory on your game by creating a disabled variable.

from sys import exit
...
    for event in pygame.event.get():
        if event.type is QUIT:
            exit()

If you want some additional behavior in a "post-death", encapsulate these behaviors into a method and call them within the for loop before exit (), it's a personal recommendation only, leaving each part with its responsibilities, much in future expansions and maintenance of their projects.

    
23.05.2017 / 16:15