List index out of range

Hey there! I am currently trying to learn pygame. I am watching a tutorial by Clear Code (Creating a platformer in Pygame with a camera, collisions, animation states and particle effects - YouTube) ← video link, and I am having trouble at (1:11:23-1:11:46) ← timestamp. The error I get when I type in that code on line 10 is IndexError: list index out of range. Any idea’s on how I can fix this and if this is even possible to do on replit? Thanks!


(https://replit.com/@notomen/Mario-Maker#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)

Hi there @SegevGanir, the problem is your line 20, you should remove the leading / on that path, because you want the relative path to your folder, not the absolute. The path you have specified doesn’t exist in your file system, therefore the import_folder() function returns an empty array and then you will have an index error when you try to access the first element of that empty array. :slightly_smiling_face:

Thank you! IT WORKED! I have tried so many things to try and fix it and nothing worked. Thank you so much!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.