Home > database >  How can I visualize the overlapping area of 2 masks in pygame?
How can I visualize the overlapping area of 2 masks in pygame?

Time:12-26

When i create a mask in Pygame, for example:

self.mask = pygame.mask.from_surface(self.image) 

Can i see the result, the grid with Boolean values?

And question 2, can I make an overlap visible when two mask overlap?

CodePudding user response:

Can i see the result, the grid with Boolean values.

You can only see what you are drawing on the screen. A mask cannot be drawn on the screen so you cannot see it.

Can I make an overlap visible when two mask overlap?

Create a Mask containing the overlapping set bits between 2 Masks with

import pygame, math

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

image1 = pygame.image.load("Banana.png")
image2 = pygame.image.load("Bird.png")
rect1 = image1.get_rect(center = (165, 150))
rect2 = image1.get_rect(center = (135, 150))
mask1 = pygame.mask.from_surface(image1)
mask2 = pygame.mask.from_surface(image2)

angle = 0
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    angle  = 0.01
    rect1.centery = 150   round(60 * math.sin(angle))        

    offset_x = rect2.x - rect1.x
    offset_y = rect2.y - rect1.y
    overlap_mask = mask1.overlap_mask(mask2, (offset_x, offset_y))
    overlap_surf = overlap_mask.to_surface(setcolor = (255, 0, 0))
    overlap_surf.set_colorkey((0, 0, 0))

    window.fill(0)
    window.blit(image1, rect1)
    window.blit(image2, rect2)
    window.blit(overlap_surf, rect1)
    pygame.display.flip()

pygame.quit()
exit()
  • Related