从文件加载配置时在 pygame 中"Invalid destination position for blit"



>我试图在运行时从文件加载房间失败。最让我困惑的是,这行代码:

ObjGlobal.instances.append(oPlayer.oPlayer(x, y))

在 main 函数中执行时成功创建对象,但在文件加载函数中不放置时

文件 "E:\Fun Stuff\Python Stuff\Python 项目\简单引擎\Main.py",第 56 行,在主 ObjGlobal.drawObjects(displaySurface) 文件 "E:\Fun Stuff\Python Stuff\Python projects\SimpleEngine\Global.py",第 57 行,in drawObjects surface.blit(self.instances[i].sprite, (self.instances[i].x, self.instances[i].y)) TypeError: blit 的目标位置无效

当然,当我尝试调用对象的变量或函数之一时,该错误会在以后发生。这是加载房间的函数:

def loadRoom(ObjGlobal, fname):
    # Get the list of stuff
    openFile = open(fname, "r")
    data = openFile.read().split(",")
    openFile.close()
    # Loop through the list to assign said stuff
    for i in range(len(data) / 3):
        # Create the object at the position
        x = data[i * 3 + 1]
        y = data[i * 3 + 2]
        # Temporary object string
        tempObject = data[i * 3]
        # Create object
        if (tempObject == "oPlayer"):
            ObjGlobal.instances.append(oPlayer.oPlayer(x, y))
        elif (tempObject == "Wall"):
            ObjGlobal.instances.append(CommonObjects.Wall(x, y))
        else: # Error found
            print ("Error: No object with name '%s'" % (tempObject))

我的文件格式正确。请注意,当我调用它时main我将 x 和 y 替换为 32、32。

从文件中读取数据时,默认情况下为字符串格式。在使用它构造对象之前,您应该将其转换为整数格式:

    x = int(data[i * 3 + 1])
    y = int(data[i * 3 + 2])

最新更新