从fi加载配置时pygame中的“blit的目标位置无效”

2024-09-30 20:20:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

File "E:\Fun Stuff\Python Stuff\Python projects\SimpleEngine\Main.py", line 56, in main ObjGlobal.drawObjects(displaySurface) File "E:\Fun Stuff\Python Stuff\Python projects\SimpleEngine\Global.py", line 57, in drawObjects surface.blit(self.instances[i].sprite, (self.instances[i].x, self.instances[i].y)) TypeError: invalid destination position for 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))

我的文件格式正确。注意当我在^{中调用它时,我用32,32替换x和y。在


Tags: 文件theinstances函数inselfdataobject
1条回答
网友
1楼 · 发布于 2024-09-30 20:20:30

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

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

相关问题 更多 >