null
null

Reputation: 566

"Invalid destination position for blit" in pygame when loading configuration from file

I am trying unsuccessfully to load a room from a file during runtime. What confuses me the most is that this line of code:

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

successfully creates an object when executed in the main function, but when put not when in the file loading function:

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

That error occurs later on when I try and call one of the objects' variables or functions, of course. Here is the function loading the room:

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))

My file is in the correct format. Note when I call it in main I replace x and y with 32, 32.

Upvotes: 0

Views: 1435

Answers (1)

icedtrees
icedtrees

Reputation: 6466

When reading in data from a file, it is in string format by default. You should convert it into integer format before constructing an object with it:

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

Upvotes: 1

Related Questions