我在我的代码中有一个小问题。我有以下代码:奇怪的pygame图像错误
import os,pygame
class npc:
ntype = 0
image = None
x = 0
y = 0
text = ""
name = ""
def Draw(self,screen):
screen.blit(self.image, [self.x*32,self.y*32])
def __init__(self,name,nx,ny):
f = open(name)
z = 0
for line in f:
if z == 0:
name = line
if z == 1:
ntype = line
if z == 2:
text = line
if z == 3:
self.image = pygame.image.load(os.path.join('img', line))
self.x = nx
self.y = ny
z=z+1
其中我从是按以下格式加载文件:
The Shadow
0
Hello. I am evil.
shadow.png
这是有问题的最后一道防线。当我尝试使用pygame.image.load加载该png时,我收到一个错误,说它不能加载该图像。但是,如果我将pygame加载代码更改为self.image = pygame.image.load(os.path.join('img', "shadow.png"))
,它完美地工作。我已经多次查看这些文件,并且找不到此错误的任何原因。有人可以看到我做错了什么吗?
回溯:
Traceback (most recent call last):
File "./main.py", line 26, in <module>
rmap = g.Load_Map("l1.txt",char)
File "/home/josiah/python/rpg/generate.py", line 31, in Load_Map
npcs.append(npc.npc(str.split(bx,',')[1],x,y))
File "/home/josiah/python/rpg/npc.py", line 23, in __init__
self.image = pygame.image.load(os.path.join('img', line))
pygame.error: Couldn't open img/shadow.png
你为什么不使用类似: '[名,ntype,text,self.image] = f.read()。split('\ n')' – joaquin