Python 模糊Pygame错误

Python 模糊Pygame错误,python,pygame,rect,Python,Pygame,Rect,我在这里看到了一些有错误的主题:“TypeError:参数必须是rect样式的对象”。我一直在犯这个错误 我读过文件: Rect(left, top, width, height) -> Rect Rect((left, top), (width, height)) -> Rect Rect(object) -> Rect 我有一种从pygame.Surface中提取子曲面的方法(它使用该曲面的原始方法): 问题是当我通过这个rect时(我已经“取消聚集”参数以使其更清晰):

我在这里看到了一些有错误的主题:“TypeError:参数必须是rect样式的对象”。我一直在犯这个错误

我读过文件:

Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
我有一种从pygame.Surface中提取子曲面的方法(它使用该曲面的原始方法):

问题是当我通过这个rect时(我已经“取消聚集”参数以使其更清晰):

我已经明确通过了一个有效的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子曲面方法将其视为有效的Rect。问题是:我所有的实例变量都是有效的整数(即使不是,如果显式转换它们也不起作用)

这毫无意义


为什么它接受显式整数,但不接受我的变量?(如果值的类型不正确,则不会出现“rectstyle”错误,就像我错误地传递参数一样)。

如果传递给
Rect()
的任何参数不是数值,则会发生此错误

要查看错误,请将以下代码添加到方法中:

import numbers
...
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)
        # be 100% sure x,y,w and h are really numbers
        assert isinstance(x, numbers.Number)
        assert isinstance(y, numbers.Number)
        assert isinstance(w, numbers.Number)
        assert isinstance(h, numbers.Number)
        sub.append(self.tileset.getSubSurface(pygame.Rect(x,y,w,h)))

我找到了问题的根源。我已将变量显式转换为整数:

sub.append(self.tileset.getSubSurface((int(x),int(y),int(w),int(h))))
并且得到了一个“TypeError:int()参数必须是字符串或数字,而不是‘NoneType’”,这变得很清楚。迭代中的“x”和“y”变量在最后返回一个“None”(因为它们是从字典中获取值的,并且,由于它们停止查找键,它们开始返回一个NoneType)

我已经解决了修复getX和getY方法的问题:

def getX(self, pos):

    """
    The getX() method expects a x-key as an argument. It returns its equivalent value in pixels.
    """

    if self.x.get(pos) != None:
        return self.x.get(pos)
    else:
        return 0 # If it is NoneType, it returns an acceptable Rect int value.

创建一个临时pygame.Rect变量,并使用pdb检查出现这种情况的x、y、w、h值。Does
sub.append(self.tileset.getSubSurface(pygame.Rect((x、y、w、h))
work?有必要挖掘以发现这是一个“非类型”问题。但是开发人员不可能预测到这一点,并在参数错误中添加了一个“NoneType”(可能是NoneType输入了代码的“else”块,它被视为“rectstyle错误”)。
sub.append(self.tileset.getSubSurface((int(x),int(y),int(w),int(h))))
def getX(self, pos):

    """
    The getX() method expects a x-key as an argument. It returns its equivalent value in pixels.
    """

    if self.x.get(pos) != None:
        return self.x.get(pos)
    else:
        return 0 # If it is NoneType, it returns an acceptable Rect int value.