1
votes

I have been working on a small project in Pygame, but I have reached an issue. When moving a character, it seems to leave a trail behind it.

    while 1:
    movey=0
    movex=0
    x=0
    y=0
    while True:
        for event in pygame.event.get():   
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:

                if event.key == K_w:
                    movey = -1
                elif event.key == K_s:
                    movey = +1
                elif event.key == K_a:
                    movex = -1
                elif event.key == K_d:
                    movex = +1

            if event.type == KEYUP:

                if event.key == K_w:
                    movey = 0
                elif event.key == K_s:
                    movey = 0
                elif event.key == K_a:
                    movex = 0
                elif event.key == K_d:
                    movex = 0
        x=x+movex
        y=y+movey

        functions_for_game.character(char, display, x, y)
        pygame.display.flip()


if __name__=='__main__':

  main()

functions_for_game.character(char, display, x, y) contains

def character(char, screen, x, y):
    char_main = pygame.image.load(char).convert_alpha()
    screen.blit(char_main, (x, y))
    pygame.display.update()  

By 'Trail' I mean this. I apologize for the noobish question, but I have been unable to find a solution. Thanks in advanced!

1

1 Answers

2
votes

You should blit your background image to the screen every frame (there are more advanced techniques to only update the "dirty" part of the screen, but that's another topic).

If you repeatly draw your player image to the screen while moving it around, the old images don't get "deleted" from the screen, hence it looks like it leaves a trail.

There are some more issues with your code, but your main problem is that you don't redraw your background image. It should look something like:

while True:
    for event in pygame.event.get():  
        ...
    ...
    screen.blit(*your background surface here*)
    functions_for_game.character(char, display, x, y)
    pygame.display.flip()