Enrique Metner
Enrique Metner

Reputation: 189

pygame: mask non-image type surfaces

I know how to mask images. But if I simply want to create a circle and mask it, it would be annoying to create an image of a simple circle outside of my code, especially when I need to create different kinds of circles. Appearantly you can create Rect objects in pygame but there is no class Circle. pygame.mask.from_surface requires a surface. Can I pass a non-image type surface as a parameter? If so how can I mask circles and/or other objects?

Here's something I imagined which obviously throws an error:

circle = pygame.Circle((10, 10), 5) # (center coordinates), radius
pygame.mask.from_surface(circle)

Upvotes: 1

Views: 930

Answers (1)

Rabbid76
Rabbid76

Reputation: 211277

There is no way to create a circular mask directly. See pygame.mask. You need to draw a circle on a Surface. Create a function that creates a Surface and draw a circle on the Surface:

def circleSurface(color, radius):
    shape_surf = pygame.Surface((radius * 2, radius * 2), pygame.SRCALPHA)
    pygame.draw.circle(shape_surf, color, (radius, radius), radius)
    return shape_surf 

Create the Mask from the Surface:

circle = circleSurface((255, 255, 255), 5) # (center coordinates), radius
pygame.mask.from_surface(circle)

However, if you want to use a circle to clip an area of the display, see the answers to the following questions:

Upvotes: 1

Related Questions