"Invalid destination position for blit" in pygame 从文件加载配置时
"Invalid destination position for blit" in pygame when loading configuration from file
我试图在运行时从文件加载房间失败。最让我困惑的是这行代码:
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))
我的文件格式正确。请注意,当我在 main
中调用它时,我将 x 和 y 替换为 32、32。
从文件中读取数据时,默认为字符串格式。在使用它构造对象之前,您应该将其转换为整数格式:
x = int(data[i * 3 + 1])
y = int(data[i * 3 + 2])
我试图在运行时从文件加载房间失败。最让我困惑的是这行代码:
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))
我的文件格式正确。请注意,当我在 main
中调用它时,我将 x 和 y 替换为 32、32。
从文件中读取数据时,默认为字符串格式。在使用它构造对象之前,您应该将其转换为整数格式:
x = int(data[i * 3 + 1])
y = int(data[i * 3 + 2])