Animated sprite from few images pygame
Code to Create an Animated Sprite from Multiple Images in Pygame
import pygame
import os
# Initialize Pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((800, 600))
# Load images
image1 = pygame.image.load(os.path.join('images', 'image1.png'))
image2 = pygame.image.load(os.path.join('images', 'image2.png'))
image3 = pygame.image.load(os.path.join('images', 'image3.png'))
# Create a list of images
images = [image1, image2, image3]
# Set initial image index
image_index = 0
# Set the clock
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Display the current image
screen.blit(images[image_index], (0, 0))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(10)
# Update the image index for the next frame
image_index = (image_index + 1) % len(images)
# Quit Pygame
pygame.quit()
End of code