Bonjour je débute en programmation sur python, j'essaye de faire un premier projet dans lequel je me base sur une vidéo qui donne beaucoup de bases, mais j'ai un problème lors de l'exécution de mon main, voici l'erreur :
self.image = self.animations[self.status][self.frame_index]
IndexError: list index out of range
Chaque dossier ici comportent toutes les images nécessaires
L'erreur doit venir du fichier player.py ci-dessous :
import pygame
from settings import *
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos, groups, obstacle_sprites):
super().__init__(groups)
# Image
self.import_player_assets()
self.status = 'down'
self.frame_index = 0
self.image = self.animations[self.status][self.frame_index]
self.rect = self.image.get_rect(topleft=pos)
self.hitbox = self.rect.inflate(0, -26)
# Obstacles
self.obstacle_sprites = obstacle_sprites
# Mouvements
self.direction = pygame.math.Vector2()
self.speed = 5
# Stats
self.health = 100
self.energy = 60
self.attack_damage = 10
self.magic_power = 4
self.attack_speed = 400
self.magic_speed = 400
self.invulnerability_duration = 500
self.exp = 123
# Attaque
self.attacking = False
self.attack_cooldown = 400
self.attack_time = None
def import_player_assets(self):
character_path = 'graphics/player/'
self.animations = {
'up': [], 'down': [], 'left': [], 'right': [],
'right_idle': [], 'left_idle': [], 'up_idle': [], 'down_idle': [],
'right_action': [], 'left_action': [], 'up_action': [], 'down_action': []
}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
def input(self):
keys = pygame.key.get_pressed()
if not self.attacking:
# Mouvement vertical
if keys[pygame.K_UP]:
self.direction.y = -1
self.status = 'up'
elif keys[pygame.K_DOWN]:
self.direction.y = 1
self.status = 'down'
else:
self.direction.y = 0
# Mouvement horizontal
if keys[pygame.K_LEFT]:
self.direction.x = -1
self.status = 'left'
elif keys[pygame.K_RIGHT]:
self.direction.x = 1
self.status = 'right'
else:
self.direction.x = 0
# Attaque physique
if keys[pygame.K_SPACE]:
self.attacking = True
self.attack_time = pygame.time.get_ticks()
# Magie
if keys[pygame.K_LCTRL]:
self.attacking = True
self.attack_time = pygame.time.get_ticks()
def get_status(self):
# Idle
if self.direction.x == 0 and self.direction.y == 0:
if 'idle' not in self.status and 'attack' not in self.status:
self.status += '_idle'
# Attacking
if self.attacking:
self.direction.x = 0
self.direction.y = 0
if 'attack' not in self.status:
if 'idle' in self.status:
self.status = self.status.replace('_idle', '_attack')
else:
self.status += '_attack'
else:
if 'attack' in self.status:
self.status = self.status.replace('_attack', '')
def cooldowns(self):
current_time = pygame.time.get_ticks()
if self.attacking and current_time - self.attack_time >= self.attack_cooldown:
self.attacking = False
def animate(self, dt):
animation = self.animations[self.status]
self.frame_index += 4 * dt
if self.frame_index >= len(animation):
self.frame_index = 0
self.image = animation[int(self.frame_index)]
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.hitbox.x += self.direction.x * speed
self.collision('horizontal')
self.hitbox.y += self.direction.y * speed
self.collision('vertical')
self.rect.center = self.hitbox.center
def collision(self, direction):
for sprite in self.obstacle_sprites:
if sprite.hitbox.colliderect(self.hitbox):
if direction == 'horizontal':
if self.direction.x > 0: # va à droite
self.hitbox.right = sprite.hitbox.left
if self.direction.x < 0: # va à gauche
self.hitbox.left = sprite.hitbox.right
if direction == 'vertical':
if self.direction.y > 0: # va en bas
self.hitbox.bottom = sprite.hitbox.top
if self.direction.y < 0: # va en haut
self.hitbox.top = sprite.hitbox.bottom
def update(self, dt):
self.input()
self.cooldowns()
self.get_status()
self.animate(dt)
self.move(self.speed)
Partager