user2855624
user2855624

Reputation: 13

Pygame - blitting to screen producing weird results?

I have blitted an image to the screen of my program, which worked fine, but then when I tried to 'animate' it, it produces weird results. It seems to be that updating its position produces another image and so on...

Code input:

screen=pygame.display.set_mode((640,360),0,32)
star=pygame.image.load("star.png").convert_alpha()

x = randint(80,700)
y = randint(-200,-200)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    x-=0.1
    y+=0.1
    screen.blit(star,(x,y))

    if y > 360:
        y = randint(-200,-200)
        x = randint(80,700)
        screen.fill((0,0,0))

    pygame.display.update()

Outcome:

https://i.sstatic.net/1Ha0n.jpg

I would appreciate any feedback as to why this happens and how I can fix it.

Upvotes: 1

Views: 58

Answers (1)

McAden
McAden

Reputation: 13970

You don't get a blank canvas every frame. You need to erase the old star before drawing the new one.

You can see they account for this in the pygame tutorials.

Upvotes: 0

Related Questions