检查图像和线条之间的碰撞| PYGAME

时间:2019-11-01 15:55:32

标签: python pygame

我检查碰撞:

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)

对不起,我的英语。

1 个答案:

答案 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)