我有2个pygame表面(技术上是次表面),我需要测试它们的相等性。
这是我的代码:
actor.display(mockSurface, Rect((0, 0, 640, 640)))
assert actor.image == actor.spriteSheet.subsurface(Rect((0, 30, 30, 30)))
这是actor.display背后的代码:
def display(self, screen, camera_location):
self.image = self.spriteSheet.subsurface(Rect((0, 30, 30, 30)))
screen.blit(self.image, (0, 0))
现在这是我从断言行得到的错误:
> assert teste.image == teste.spriteSheet.subsurface(Rect((0, 30, 30, 30)))
E assert <Surface(30x30x32 SW)> == <Surface(30x30x32 SW)>
E + where <Surface(30x30x32 SW)> = <actor.Actor instance at 0x2f78ef0>.image
E + and <Surface(30x30x32 SW)> = <Surface(120x90x32 SW)>.subsurface(<rect(0, 30, 30, 30)>)
E + where <Surface(120x90x32 SW)> = <actor.Actor instance at 0x2f78ef0>.spriteSheet
E + and <rect(0, 30, 30, 30)> = Rect((0, 30, 30, 30))
这些应该是平等的(我错了),但是通过一些偷偷摸摸的蛇技巧,断言失败了。
答案 0 :(得分:1)
使用Surface.get_offset()和Surface.get_parent()来确定两个子表面是否指向其父表面的相同区域。
>>> parent = pygame.image.load("token1.png")
>>> parent
<Surface(128x128x32 SW)>
>>> s1 = parent.subsurface(pygame.Rect(0, 0, 2, 2))
>>> s2 = parent.subsurface(pygame.Rect(0, 0, 2, 2))
>>> s1
<Surface(2x2x32 SW)>
>>> s2
<Surface(2x2x32 SW)>
>>> s1 == s2
False
>>> s1.get_parent() == s2.get_parent()
True
>>> s1.get_offset() == s2.get_offset()
True
>>> s1.get_parent() == s2.get_parent() and s1.get_offset() == s2.get_offset()
True
s1和s2都指的是不同的对象因此不相等。