1

I tried to make an animation of the frog (like in this video: https://www.youtube.com/watch?v=MYaxPa_eZS0&list=PL8ui5HK3oSiHnIdi0XIAVXHAeulNmBrLy&index=4). I almost succeeded. But with my code, the old images remain on screen. I can't really find where I made a mistake. Can someone take a look at my code and tell me where I made a mistake? This is my code:

import pygame
import sys
from os import path

pygame.init()

screen_width=600
screen_height=400

screen=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('animation')

clock=pygame.time.Clock()

#classes
class Player(pygame.sprite.Sprite):
    def __init__(self,images):
        pygame.sprite.Sprite.__init__(self)
        image_list=images
        self.index=0
        self.animation_state=False
        self.image=pygame.image.load(image_list[self.index])
        self.rect=self.image.get_rect()
        self.rect.center=((screen_width//2),(screen_height//2))

    def animation(self):
        self.animation_state=True
            
    def update(self):
        if self.animation_state==True:
            self.index+=0.07
            if self.index >=10:
                self.index=0
                self.animation_state=False
        self.image=pygame.image.load(image_list[int(self.index)])
        


#images
dir=path.dirname(__file__)
img_dir=path.join(dir,'img')
image_list=[]

for i in range (1,11):
    image_list.append(path.join(img_dir,f'attack_{i}.png'))

player=Player(image_list)
player_group=pygame.sprite.Group()
player_group.add(player)


while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_SPACE:
                player.animation()
            
    player_group.update()            
    player_group.draw(screen)
    pygame.display.flip()
    clock.tick(60)
            
pygame.quit()
sys.exit()
qouify
  • 3,698
  • 2
  • 15
  • 26
lila
  • 13
  • 2
  • On a side note it's extremely inefficient to call `pygame.image.load` in `update`. The image will be reloaded at each iteration of your main loop. It's better to load all images before the main loop and memorise it in a list and then pick surfaces from that list in `update`. – qouify Dec 30 '21 at 09:54

1 Answers1

1

You have to fill the screen with some color first so that all previous images are removed with:

screen.fill((r, g, b))

Then you can draw things to screen again. Hopefully this works for you now.

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_SPACE:
                player.animation()
    screen.fill((255, 255, 255))        
    player_group.update()            
    player_group.draw(screen)
    pygame.display.flip()
    clock.tick(60)
            
pygame.quit()
sys.exit()
Yenz1
  • 84
  • 1
  • 9