Pygame:如何绘制非矩形剪裁区域

Mr.*_*ith 5 python pygame draw clip

嗨我想在pygame非矩形剪裁区域(在这种情况下作为字符"P")设置,在那里它将严格限制,在哪里绘制另一个对象.

有什么选择吗?

非常感谢

Nur*_*off 5

让我们看看我是否正确理解了您的问题:您想将图像“白化”到表面上,但是要通过遮罩来实现,它只允许光源的某些像素实际最终出现在表面上?

我遇到了这个确切的问题,起初我认为只有通过PIL才能实现。然而,经过一些阅读和试验,事实证明,实际上可以借助pygame相当模糊的“特殊标志”来完成此操作。下面是一个函数,希望可以完成您想要的。

def blit_mask(source, dest, destpos, mask, maskrect):
    """
    Blit an source image to the dest surface, at destpos, with a mask, using
    only the maskrect part of the mask.
    """
    tmp = source.copy()
    tmp.blit(mask, maskrect.topleft, maskrect, special_flags=pygame.BLEND_RGBA_MULT)
    dest.blit(tmp, destpos, dest.get_rect().clip(maskrect))
Run Code Online (Sandbox Code Playgroud)

如果希望蒙版透明,则蒙版应为白色,否则应为黑色。