IndexError: list index out of range pygame
IndexError: list index out of range pygame
它说列表索引超出范围,我不知道为什么。有谁知道如何解决这个问题?
我要导入的东西的代码
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
我正在使用它的代码:
import pygame
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.import_character()
self.frame_index = 0
self.animation_speed = 0.15
self.image = self.animations['run'][self.frame_index]
# Player Movement
self.direction = pygame.math.Vector2(0, 0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16
def import_character(self):
character_path = '../graphics/character/'
self.animations = {'idle':['idle'], 'run':[], 'jump':[], 'fall':[]}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
完整错误代码(从 Traceback 开始):
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\PlatformerGame.py", line
13, in <module>
level = level(level_map, window)
File "C:\Users\Daniel\Desktop\level.py", line 11, in
__init__
self.setup_level(level_data)
File "C:\Users\Daniel\Desktop\level.py", line 27, in
setup_level
player_sprite = Player((x, y))
File "C:\Users\Daniel\Desktop\player.py", line 10, in
__init__
self.image = self.animations['run'][self.frame_index]
IndexError: list index out of range
与“运行”动画的图像数量相比,frame_index
变得太大了。
试试这样的东西:
self.image = self.animations['run'][self.frame_index % len(self.animations['run'])]
if len(self.animations['run'])>0:
self.animations['run'][self.frame_index]
else:
condition
您的图片文件加载不正确。如果文件路径不对错,walk(path)
returns 什么都没有。
将文件放在项目的同一目录或子目录中是不够的。您还需要设置工作目录。
图像文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
可以使用 __file__
and the current working directory can be changed with os.chdir(path)
.
检索文件的名称和路径
在主代码的开头添加以下内容,将工作目录设置为与主脚本的目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
它说列表索引超出范围,我不知道为什么。有谁知道如何解决这个问题?
我要导入的东西的代码
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
我正在使用它的代码:
import pygame
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.import_character()
self.frame_index = 0
self.animation_speed = 0.15
self.image = self.animations['run'][self.frame_index]
# Player Movement
self.direction = pygame.math.Vector2(0, 0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16
def import_character(self):
character_path = '../graphics/character/'
self.animations = {'idle':['idle'], 'run':[], 'jump':[], 'fall':[]}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
完整错误代码(从 Traceback 开始):
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\PlatformerGame.py", line
13, in <module>
level = level(level_map, window)
File "C:\Users\Daniel\Desktop\level.py", line 11, in
__init__
self.setup_level(level_data)
File "C:\Users\Daniel\Desktop\level.py", line 27, in
setup_level
player_sprite = Player((x, y))
File "C:\Users\Daniel\Desktop\player.py", line 10, in
__init__
self.image = self.animations['run'][self.frame_index]
IndexError: list index out of range
与“运行”动画的图像数量相比,frame_index
变得太大了。
试试这样的东西:
self.image = self.animations['run'][self.frame_index % len(self.animations['run'])]
if len(self.animations['run'])>0:
self.animations['run'][self.frame_index]
else:
condition
您的图片文件加载不正确。如果文件路径不对错,walk(path)
returns 什么都没有。
将文件放在项目的同一目录或子目录中是不够的。您还需要设置工作目录。
图像文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
可以使用 __file__
and the current working directory can be changed with os.chdir(path)
.
在主代码的开头添加以下内容,将工作目录设置为与主脚本的目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))