我正在开发一个游戏(一个 missile command 克隆),我需要在其中检查一个 surface
(建筑物)与 点
(导弹)相撞。
如何检查导弹是否击中建筑物?
A Rect
有 collidepoint
方法,但我希望它是像素完美的。
=> 使用 Rect
=> => 但它应该是像素完美 =>
最佳答案
Returns nonzero if the bit at (x,y) is set.
通过调用 pygame.mask.from_surface
创建掩码非常简单
Returns a Mask from the given surface.
Makes the transparent parts of the Surface not set, and the opaque parts set.
因此,给定以下一些向量数学的辅助方法:
def sub(u, v):
return [ u[i]-v[i] for i in range(len(u)) ]
使用以下代码检查给定点是否在掩膜/表面内:
# create mask from surface
mask = pygame.mask.from_surface(building.surface)
# translate the position of the missile,
# since the top left coordinate of the mask is always (0, 0)
rel_point = sub(missile.position, building.position)
try:
if mask.get_at(rel_point):
# point in mask
do_something()
except IndexError:
pass
关于python - 检查一个点是否与pygame中的表面(像素完美)碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557314/