Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 奇怪的游戏图像错误_Python_Image_Pygame - Fatal编程技术网

Python 奇怪的游戏图像错误

Python 奇怪的游戏图像错误,python,image,pygame,Python,Image,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):

我的代码中有一个小问题。我有以下代码:

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
回溯(最近一次呼叫最后一次):
文件“/main.py”,第26行,在
rmap=g.Load\u映射(“l1.txt”,char)
文件“/home/josiah/python/rpg/generate.py”,第31行,在加载映射中
npcs.append(npc.npc(str.split(bx,,')[1],x,y))
文件“/home/josiah/python/rpg/npc.py”,第23行,在__
self.image=pygame.image.load(os.path.join('img',line))
pygame.error:无法打开img/shadow.png

您可能有一个尾随的换行符。尝试剥离该行:

self.image = pygame.image.load(os.path.join('img', line.strip()))
更好的是,以不同的方式加载文件。代替循环,您可以这样做(假设每个文件的格式相同,并且至少具有相同的行数):


谢谢:)不过我不知道那是怎么来的。文件中没有任何换行符。为什么不使用类似以下内容:
[name,ntype,text,self.image]=f.read().split('\n')
name, ntype, text, filename = [l.strip() for l in open(name).readlines()[:4]]

# Now use the variables normally, for example:
self.image = pygame.image.load(os.path.join('img', filename))