我检查碰撞:
offset = (x0 - x1, y0 - y1)
result = player1.mask.overlap(player2, offset)
它在两个图像之间工作。
但是,如果我想检查图像和pygame.draw.line(...)
之间的冲突(我使用it从行创建蒙版)。 mask.overlap
返回None
:
surface = self.gameDisplay.subsurface(pygame.draw.line(self.gameDisplay, colors.GREEN, [100, 100], [200, 200], 5))
line_mask = pygame.mask.from_surface(surface)
pygame.draw.line(self.gameDisplay, colors.GREEN, [100, 100], [200, 200], 5)
offset = (x0 - x1, y0 - y1)
result = player1.mask.overlap(mask, offset)
对不起,我的英语。
答案 0 :(得分:1)
在从“线”表面创建遮罩之前,您错过了.convert_alpha()
来创建具有每个像素alpha的表面的方法:
line_rect = pygame.draw.line(gameDisplay, colors.GREEN, [100, 100], [200, 200], 5)
line_surf = gameDisplay.subsurface(line_rect)
line_mask = pygame.mask.from_surface(line_surf.convert_alpha())
x0, y0 = line_rect.topleft
x1, y1 = player1.rect.topleft
offset = (x0 - x1, y0 - y1)
if player1.mask.overlap(line_mask, offset):
print("hit : ", count)