Pygame:如何在非矩形裁剪区域中绘制 [英] Pygame: How to draw in non rectangle clipping area

查看:151
本文介绍了Pygame:如何在非矩形裁剪区域中绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pygame的非矩形剪切区域(在本例中为字符 P)中进行设置,在该区域中将严格限制绘制其他对象的位置。

Hi I would like to set in pygame non rectangle clipping area (in this case as character "P"), where it would be strict limited, where to draw another objects.

有什么选择吗?

非常感谢

推荐答案

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

Let's see if I correctly understand your question: you want to "blit" an image onto a surface, but do it through a mask which would only allow certain pixels of the source to actually end up on the surface?

我遇到了这个确切的问题,起初我认为这只能通过PIL来实现。但是,经过一番阅读和试验后,事实证明,实际上可以借助pygame相当模糊的特殊标志来完成此操作。

I had this precise problem and at first I thought it would only be doable through PIL. However after some reading and experimentation, it turns out that it can actually be done with the help of pygame's rather obscure "special flags". Below is a function which hopefully does what you want.

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

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

The mask should be white where you want it to be transparent and black otherwise.

这篇关于Pygame:如何在非矩形裁剪区域中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆