检查图像和线条之间的碰撞 [英] Check collision between a image and a line
本文介绍了检查图像和线条之间的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检查碰撞:
offset = (x0 - x1, y0 - y1)结果 = player1.mask.overlap(player2, offset)
它在两个图像之间工作.
但是如果我想检查图像和 pygame.draw.line(...)
之间的碰撞(我使用
I check collision:
offset = (x0 - x1, y0 - y1)
result = player1.mask.overlap(player2, offset)
Its working between two images.
But if I want to check collision between a image and pygame.draw.line(...)
(I use it for create mask from line). mask.overlap
returns 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)
Sorry for my english.
解决方案
You missed to create a surface with per pixel alpha by .convert_alpha()
, before creating the mask from the "line" Surface:
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)
See the example:
这篇关于检查图像和线条之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文