【问题标题】:Pygame Combine SpritesPygame 组合精灵
【发布时间】:2018-11-09 22:09:37
【问题描述】:

我正在尝试构建一个游戏,其中组合图像(pygame sprites)是必不可少的工具。

我已经设置了我的代码,这样我就可以用鼠标在 x,y 上移动精灵并旋转它们。 sprite 被传送到显示表面,因此可以在屏幕上看到此动作。

一旦用户在一个方形区域内按照他们的意愿安排了两个精灵,我需要他们能够将整个区域保存为一个新的精灵。

我目前在 pygame 上看不到捕获显示区域并将其存储为精灵的方法。这可能吗?为此我应该使用哪些函数?

【问题讨论】:

  • 它可能有一个函数(你查看文档了吗?),但为什么不创建一个自定义类来存储要绘制的精灵列表?
  • 你真的需要 region 成为一个新的精灵,还是只需要将精灵焊接在一起?

标签: python pygame pygame-surface


【解决方案1】:

您可以检查哪些精灵与正方形区域发生碰撞并将它们传递给Combined sprite 类,将矩形与union_ip 方法组合并创建一个具有必要大小的新表面以对单个精灵的表面进行blit到它上面。 (按 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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多