Beispiel #1
0
    def test_flip_sprite(self):
        surfaces = [load_image("assets/buttons/button1.png"),
                    load_image("assets/buttons/button2.png")]

        sprite_list = Sprite()
        sprite_list.images = [item for item in surfaces]

        sprite_dict = Sprite()
        sprite_dict.images = {key: surfaces[key] for key in
                              range(len(surfaces))}

        sprite_list = flip_sprite(sprite_list, 1, 0)
        self.assertIsInstance(sprite_list, Sprite)
        for image, rect in sprite_list.images:
            index = sprite_list.images.index((image, rect))
            self.assertEqual(rect, surfaces[index][1])

            width, height = image.get_size()
            pixel_array_base = pygame.PixelArray(surfaces[index][0])
            pixel_array_flip = pygame.PixelArray(image)
            for column in range(width - 1, -1, -1):
                for row in range(height):
                    self.assertEqual(pixel_array_base[column, row],
                                     pixel_array_flip[width - 1 - column, row])

        sprite_dict = Sprite()
        sprite_dict.images = {key: surfaces[key]
                              for key in range(len(surfaces))}

        sprite_dict = flip_sprite(sprite_dict, 0, 1)
        self.assertIsInstance(sprite_dict, Sprite)
        for key in sprite_dict.images:
            image, rect = sprite_dict.images[key]
            self.assertEqual(rect, surfaces[key][1])

            width, height = image.get_size()
            pixel_array_base = pygame.PixelArray(surfaces[key][0])
            pixel_array_flip = pygame.PixelArray(image)
            for column in range(width):
                for row in range(height - 1, -1, -1):
                    self.assertEqual(pixel_array_base[column, row],
                                     pixel_array_flip[column,
                                                      height - 1 - row])

        sprite_with_image = Sprite()
        sprite_with_image.image, sprite_with_image.rect = surfaces[0]

        sprite_with_image = flip_sprite(sprite_with_image, 1, 1)
        self.assertIsInstance(sprite_with_image, Sprite)
        self.assertEqual(sprite_with_image.rect, surfaces[0][1])
        width, height = sprite_with_image.image.get_size()
        pixel_array_base = pygame.PixelArray(surfaces[0][0])
        pixel_array_flip = pygame.PixelArray(sprite_with_image.image)
        for column in range(width - 1, -1, -1):
            for row in range(height - 1, -1, -1):
                self.assertEqual(pixel_array_base[column, row],
                                 pixel_array_flip[width - 1 - column,
                                                  height - 1 - row])
Beispiel #2
0
    def test_scale_sprite(self):
        surfaces = [pygame.Surface((0, 0)), pygame.Surface((0, 298)),
                    pygame.Surface((10, 0)), pygame.Surface((13, 7))]

        sprite_list = Sprite()
        sprite_list.image = pygame.Surface((1000, 1000))
        sprite_list.rect = sprite_list.image.get_rect()
        sprite_list.rect.topleft = (7, 17)
        sprite_list.images = [(surface, surface.get_rect())
                              for surface in surfaces]

        sprite_dict = Sprite()
        sprite_dict.image = pygame.Surface((23, 29))
        sprite_dict.rect = sprite_dict.image.get_rect()
        sprite_dict.rect.topleft = (0, 0)
        sprite_dict.images = {key: (surfaces[key], surfaces[key].get_rect())
                              for key in range(len(surfaces))}

        sprite_list = scale_sprite(sprite_list, (0, 0))
        self.assertIsInstance(sprite_list, Sprite)
        self.assertIsInstance(sprite_list, Sprite)
        self.assertEqual(sprite_list.image.get_size(), (0, 0))
        self.assertEqual(sprite_list.rect, pygame.Rect(7, 17, 0, 0))
        for image, rect in sprite_list.images:
            self.assertEqual(image.get_size(), (0, 0))
            self.assertEqual(rect, pygame.Rect(0, 0, 0, 0))

        sprite_dict = scale_sprite(sprite_dict, (2, 1))
        self.assertIsInstance(sprite_dict, Sprite)
        self.assertIsInstance(sprite_dict, Sprite)
        self.assertEqual(sprite_dict.image.get_size(), (23 * 2, 29))
        self.assertEqual(sprite_dict.rect, pygame.Rect(0, 0, 23 * 2, 29))
        for key, tuple in sprite_dict.images.items():
            self.assertEqual(tuple[0].get_size(),
                             Vec2D(surfaces[key].get_size()) * Vec2D(2, 1))
            self.assertEqual(tuple[1], pygame.Rect(0, 0,
                             surfaces[key].get_size()[0] * 2,
                             surfaces[key].get_size()[1]))