I am having trouble loading the image from sprite sheet overlapping-ly to create an animation for my character in a fighting game. [enter image description here](https://i.stack.imgur.com/E5Uh8.png)
The following are the function for loading the image and the animation list. I will also attach my sprite sheets as well. Please let me know if you want the entire file of code.
def load_images(self, sprite_sheet, animation_steps):
animation_list = []
for y, animation in enumerate(animation_steps):
temp_img_list = []
for x in range(animation):
temp_img = sprite_sheet.subsurface(x * self.size, y * self.size, self.size, self.size)
scaled_img = pygame.transform.scale(temp_img, (self.size * self.image_scale, self.size * self.image_scale))
temp_img_list.append(scaled_img)
animation_list.append(temp_img_list)
return animation_list
def update_animation(self):
animation_cooldown = 500
self.image = self.animation_list[self.action][self.frame_index]
# Check if time has passed for new update
if pygame.time.get_ticks() - self.update_time > animation_cooldown:
self.frame_index += 1
self.update_time = pygame.time.get_ticks()
# Check if the frame index has finished to the end of the screen
if self.frame_index >= len(self.animation_list[self.action]):
self.frame_index = 0
And the following is how I loaded in the main.py file.
# define fighter variable
CAT_SIZE = 40
CAT_SCALE = 5
CAT_OFFSET = [17, 15]
CAT_DATA = [CAT_SIZE, CAT_SCALE, CAT_OFFSET]
GIRLGUN_SIZE = 50
GIRLGUN_SCALE = 5
GIRLGUN_OFFSET = [20, 17]
GIRLGUN_DATA = [GIRLGUN_SIZE, GIRLGUN_SCALE, GIRLGUN_OFFSET]
# Uploading the background image
background_image = pygame.image.load("assets/backgrounds/university_background.jpg").convert_alpha()
# loading the character
cat_sheet = pygame.image.load("assets/characters/cat.png").convert_alpha()
girlgun_sheet = pygame.image.load("assets/characters/girlgun_bg.png").convert_alpha()
# Define number of steps in each animation
CAT_ANIMATION_STEPS = [10, 10, 10, 10, 10, 10]
GIRLGUN_ANIMATION_STEPS = [5, 5, 8, 8, 5, 5, 8, 8]
while running:
clock.tick(FPS)
draw_bg()
# show the player's health
draw_health_bar(player1.health, 20, 20)
draw_health_bar(player2.health, 580, 20)
# move player
player1.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, player2)
# player2.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, player1)
# update animation
player1.update_animation()
player2.update_animation()
# draw player
player1.draw(screen)
player2.draw(screen)