【发布时间】:2014-03-18 07:07:41
【问题描述】:
我在这里看到了一些带有错误的主题:“TypeError: Argument must be rect style object”。我无休止地遇到这个错误。
我已阅读文档:
Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
我有一种从 pygame.Surface 中提取子表面的方法(它使用表面的原始方法):
def getSubSurface(self, rect):
"""
Returns the subsurface of a specified rect area in the grict surface.
"""
return self.surface.subsurface(rect)
问题是当我传递这个矩形时(我已经“取消聚集”了参数以使其更清晰):
sub = []
w = self.tileWidth
h = self.tileHeight
for i in range((self.heightInPixels/self.heightInTiles)):
y = self.grid.getY(i)
for j in range((self.widthInPixels/self.widthInTiles)):
x = self.grid.getX(j)
sub.append(self.tileset.getSubSurface(pygame.Rect(x,y,w,h)))
我已经明确传递了一个有效的 pygame.Rect,但我什么都没有,我得到:
sub.append(self.tileset.getSubSurface(pygame.Rect(x,y,w,h)))
TypeError: Argument must be rect style object
现在,有趣的部分是:如果我将参数更改为任意 int 值:
sub.append(self.tileset.getSubSurface((1,2,3,4)))
完美运行。 pygame subsurfaces 方法将其作为有效的 Rect。问题是:我所有的实例变量都是有效的整数(即使它们不是,如果我显式转换它们也不起作用)。
没有意义。
为什么它采用显式整数,但不采用我的变量? (如果值的类型不正确,我不会收到“rectstyle”错误,就好像我错误地传递了参数一样)。
【问题讨论】:
-
创建一个临时的 pygame.Rect 变量,并使用 pdb 检查发生在哪个 x,y,w,h 值。
-
sub.append(self.tileset.getSubSurface(pygame.Rect((x,y,w,h))))工作吗?