I am working on a 2d platformer game and I am having an issue where the list index cannot get the image for the player. Here is my player.py:
import pygame
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.import_character_assets()
self.frame_index = 0
self.animation_speed = 0.15
self.image = self.animations['idle'][self.frame_index]
self.rect = self.image.get_rect(topleft=pos)
# PLAYER MOVEMENT
self.direction = pygame.math.Vector2(0, 0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16
def import_character_assets(self):
character_path = '../graphics/character'
self.animations = {'idle': [], 'run': [], 'jump': [], 'fall': []}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
and here is where I use os to select the directory and images:
from os import walk
import pygame
def import_folder(path):
surface_list = []
for _, __, img_files in walk(path):
for image in img_files:
full_path = path + '/' + image
image_surf = pygame.image.load(full_path).convert_alpha()
surface_list.append(image_surf)
return surface_list
Here is the Index error:
Traceback (most recent call last):
File "C:\Users\ejvid\PycharmProjects\Platformer1\Platformer\1 - Basic platformer\code\main.py", line 10, in <module>
level = Level(level_map, screen)
File "C:\Users\ejvid\PycharmProjects\Platformer1\Platformer\1 - Basic platformer\code\level.py", line 11, in __init__
self.setup_level(level_data)
File "C:\Users\ejvid\PycharmProjects\Platformer1\Platformer\1 - Basic platformer\code\level.py", line 26, in setup_level
player_sprite = Player((x, y))
File "C:\Users\ejvid\PycharmProjects\Platformer1\Platformer\1 - Basic platformer\code\player.py", line 11, in __init__
self.image = self.animations['idle'][self.frame_index]
IndexError: list index out of range
Process finished with exit code 1