Pygame - How to solve the image’s phantom effect

Asked

Viewed 729 times

-1

I am doing some tests in pygame and created a "character" that moves, but it creates a ghost effect on the screen. I’d like to know how to solve. inserir a descrição da imagem aqui

(This land is what the character would be)

The phantom effect I refer to is that when moving, the character "walks", but may create a "copy" of it (as shown in the image)

The code I’ll put on 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

1 answer

1


Hello. This "error" occurs because there is no screen update at the end of your main loop: while sair != True, where the pygame manages the events of your game. Remember that each loop iteration draws on the screen the added objects in your display.blit. Therefore, since there is no internal management of these particularities, since pygame is a reimagination of the applications of the SDL library(A powerful multimedia and cross-platform 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 your other elements:

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

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

Then add:

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

One inside your code, there’s a redundancy in your bond clause while as the internal "event" for event in pygame.event.get() should already match the closure of the application, the condition of your while loop becomes unnecessary and therefore you are wasting memory in your game creating an unused variable.

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

If you want additional behavior in a "postmortem", encapsulate these behaviors in a method and call them within the loop for before Exit(), is a personal recommendation only, leaving each party with its responsibilities, will greatly improve in future expansions and maintenance of its projects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.