Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame: How to draw in non rectangle clipping area

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.

Is there any option?

thanks a lot

like image 357
Mr.Smith Avatar asked May 09 '11 18:05

Mr.Smith


1 Answers

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?

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.

like image 56
Nurbldoff Avatar answered Sep 22 '22 01:09

Nurbldoff