Pygame组合精灵

时间:2018-11-09 22:09:38

标签: python pygame pygame-surface

我正在尝试开发一款游戏,其中结合图像(pygame精灵)是必不可少的工具。

我已经设置好代码,以便可以使用鼠标在x,y上移动精灵并将它们旋转。精灵会变白到显示表面,因此可以在屏幕上看到该运动。

一旦用户在一个方形区域中按他们希望的方式排列了两个精灵,我需要它们能够将整个区域另存为新的精灵。

我无法在pygame上看到一种捕获显示区域并将其存储为sprite的方法。这可能吗?我应该为此使用什么功能?

1 个答案:

答案 0 :(得分:0)

您可以检查哪些精灵与正方形区域碰撞,然后将它们传递给Combined精灵类,将rects与union_ip方法结合使用,并创建一个具有必要大小的新曲面以遮蔽曲面它上面的单个精灵。 (按 C 组合精灵。)

import pygame as pg


BLUE = pg.Color('dodgerblue1')
SIENNA = pg.Color('sienna1')
GREEN = pg.Color('green')


class Entity(pg.sprite.Sprite):

    def __init__(self, pos, color):
        super().__init__()
        self.image = pg.Surface((42, 68))
        self.image.fill(color)
        self.rect = self.image.get_rect(topleft=pos)

    def move(self, velocity):
        self.rect.move_ip(velocity)


class Combined(pg.sprite.Sprite):

    def __init__(self, sprites):
        super().__init__()
        # Combine the rects of the separate sprites.
        self.rect = sprites[0].rect.copy()
        for sprite in sprites[1:]:
            self.rect.union_ip(sprite.rect)

        # Create a new transparent image with the combined size.
        self.image = pg.Surface(self.rect.size, pg.SRCALPHA)
        # Now blit all sprites onto the new surface.
        for sprite in sprites:
            self.image.blit(sprite.image, (sprite.rect.x-self.rect.left,
                                           sprite.rect.y-self.rect.top))

    def move(self, velocity):
        self.rect.move_ip(velocity)


def main():
    pg.init()
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    entity = Entity((50, 80), BLUE)
    entity2 = Entity((50, 180), SIENNA)
    all_sprites = pg.sprite.Group(entity, entity2)
    area = pg.Rect(200, 50, 200, 200)
    selected = None

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return
            elif event.type == pg.MOUSEBUTTONDOWN:
                for sprite in all_sprites:
                    if sprite.rect.collidepoint(event.pos):
                        selected = sprite
            elif event.type == pg.MOUSEBUTTONUP:
                selected = None
            elif event.type == pg.MOUSEMOTION:
                if selected:
                    selected.move(event.rel)
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_c:
                    # A 'list comprehension' to find the colliding sprites.
                    colliding_sprites = [sprite for sprite in all_sprites
                                         if sprite.rect.colliderect(area)]
                    combined = Combined(colliding_sprites)
                    all_sprites.add(combined)
                    # Kill the colliding sprites if they should be removed.
                    # for sprite in colliding_sprites:
                    #     sprite.kill()

        all_sprites.update()
        screen.fill((30, 30, 30))
        pg.draw.rect(screen, SIENNA, area, 2)
        all_sprites.draw(screen)
        for sprite in all_sprites:  # Outlines.
            pg.draw.rect(screen, GREEN, sprite.rect, 1)

        pg.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    main()
    pg.quit()

或者,您可以尝试将合并的精灵添加到另一个精灵组或列表中,然后将它们组合并一起移动。