How to improve the given code to display multiple images from a list in an explosion

Viewed 37

I recently saw a question on animation in pygame and in the answer, a code was given which works perfectly, by a user @Rabbid76 which is:

explosionList = []

while run:
    current_time = pygame.time.get_ticks()
    # [...]

    for missile_idx, missile_val in enumerate(missiles)
        # [...]

        if missile_rect.colliderect(rock_rect):
            explosion_sound.play()

            explosion_pos_x = missile_pos_x
            explosion_pos_y = missile_pos_y
            end_time = current_time + 2000 # 2000 milliseconds = 2 seconds
            explosionList.insert(0, (end_time, explosion_pos_x, explosion_pos_y))

            # [...]

    for i in range(len(explosionList)):
        if current_time < explosionList[i][0]:
            screen.blit(explosion, (explosionList[i][1], explosionList[i][2]))
        else:
            explosionList = explosionList[:i]
            break
    
    # [...]

This, as I mentioned, works well... thanks @Rabbid76 But can someone improve this code so that a list of 10 images can be displayed on the screen using the same format, with a 100-millisecond gap?? I tried to do it, but the images are printing spontaneously and stacked on each other...

1 Answers
class AnimationClass():
    def __init__(self):
        self.x = 0
        self.y = 0
        self.frame_count = 0
        self.image_list = []

    def update(self):

        self.frame_count += 1

        for num in range(1,5): # SET THE NUM OF FRAMES
            image = pygame.image.load(f"YOURE IMAGE FILE{num}.png") # loading youre image file
            self.image_list.append(image) # appending youre image file

            window.blit(self.image_list[int(self.frame_count)],(self.x , self.y)) # rendering youre image

            
animation = AnimationClass()

Loop = True
while Loop == True:
    for event in pygame.event.get():
        if event.type == pg.QUIT:
            Loop = False

    animation.update() # CALLING THE UPDATE IN LOOP

    pygame.display.update()
Related