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()