Exemplo n.º 1
0
    def test_count(self):
        """Ensure a mask's set bits are correctly counted."""
        side = 67
        mask = pygame.mask.Mask((side, side))
        expected_count = 0

        for i in range(side):
            expected_count += 1
            mask.set_at((i, i))

        self.assertEqual(mask.count(), expected_count)
Exemplo n.º 2
0
def maskFromSurface(surface, threshold = 127):
    mask = pygame.Mask(surface.get_size())
    key = surface.get_colorkey()
    if key:
        for y in range(surface.get_height()):
            for x in range(surface.get_width()):
                if surface.get_at((x+0.1,y+0.1)) != key:
                    mask.set_at((x,y),1)
    else:
        for y in range(surface.get_height()):
            for x in range (surface.get_width()):
                if surface.get_at((x,y))[3] > threshold:
                    mask.set_at((x,y),1)
    return mask
Exemplo n.º 3
0
def maskFromSurface(surface, threshold = 127):
    mask = pygame.Mask(surface.get_size())
    key = surface.get_colorkey()
    if key:
        for y in range(surface.get_height()):
            for x in range(surface.get_width()):
                if surface.get_at((x+0.1,y+0.1)) != key:
                    mask.set_at((x,y),1)
    else:
        for y in range(surface.get_height()):
            for x in range (surface.get_width()):
                if surface.get_at((x,y))[3] > threshold:
                    mask.set_at((x,y),1)
    return mask
Exemplo n.º 4
0
def from_surface(surface, threshold=127):
    key = surface.get_colorkey()
    if key:
        new = Mask(surface.get_size())
        for x in xrange(surface.get_width()):
            for y in xrange(surface.get_height()):
                new.set_at((x, y), surface.get_at((x, y)) != key)
        return new
    mask = Mask(surface.get_size())
    for x in xrange(surface.get_width()):
        for y in xrange(surface.get_height()):
            a = surface.get_at((x,y))
            if len(a) == 4:
                mask.set_at((x, y), int(a[3] > threshold))
            else:
                mask.set_at((x, y), 1)
    return mask
Exemplo n.º 5
0
    def test_set_at__out_of_bounds(self):
        """Ensure set_at() checks bounds."""
        width, height = 11, 3
        mask = pygame.mask.Mask((width, height))

        with self.assertRaises(IndexError):
            mask.set_at((width, 0))

        with self.assertRaises(IndexError):
            mask.set_at((0, height))

        with self.assertRaises(IndexError):
            mask.set_at((-1, 0))

        with self.assertRaises(IndexError):
            mask.set_at((0, -1))