我有一个程序循环遍历目录,使用文件名的键改变字典,在本例中为'black_arrow',但是当我测试它时,我得到了这个模糊的错误:
Traceback (most recent call last):
File "C:\Users\Michael\Desktop\game\dicts.py", line 17, in <module>
print(GetPics())
File "C:\Users\Michael\Desktop\game\dicts.py", line 13, in GetPics
load_attacks(dic)
File "C:\Users\Michael\Desktop\game\dicts.py", line 12, in load_attacks
dictionary [img[:-4]]=pg.image.load(img)
pygame.error: Couldn't open black_arrow.png
我试过这个通过python shell,它工作得很好。我也试过调用pygame.init(),我认为它不会做任何事情,但事实并非如此。 这是主要代码:
def GetPics(dic={}):
'''
return a dictionary whose keys are the filename, and values are
he image itself.
'''
import pygame as pg, os
try:os.chdir('data')
except FileNotFoundError:
pass
def load_attacks(dictionary):
'''
adds keys for attacks to dictionary.
'''
Cpath='Attacks\\'
DirList=os.listdir(Cpath)
for Dir in DirList:
for img in os.listdir(Cpath+Dir):
dictionary [img[:-4]]=pg.image.load(img)
load_attacks(dic)
if __name__ == '__main__':
a={}
GetPics(a)
print(a)
(放入一个文件)