如何在Sprite的底部放置其他图像或矩形?

时间:2018-08-12 16:42:54

标签: python python-3.x pygame

我需要在Sprite worker的底部添加一个图标,然后在每次迭代时随机更改此图标。请注意,精灵worker具有2种状态:RUNNINGIDLE。在每种状态下,worker都有一个特定的图像。我现在需要的是在工人的底部上放置一个其他小图像,以指定情感状态HAPPY或{ {1}}。

在类ANGRY中,我创建了数组Worker,并指定了变量emo_images。此变量表示emo_state的情绪状态:高兴还是生气。每个情感状态的图像都存储在worker中。

在代码中,我随机生成变量emotional_images。如果大于9,则state_indicator的情绪状态将更改为worker。否则,很高兴。

ANGRY

但是,我不知道如何将情感图像放在 state_indicator = random.randint(0,10) if state_indicator > 9: print(state_indicator) self.alert_notif_worker() def alert_notif_worker(self): self.emo_state = Worker.ANGRY 图像的底部,因为我不想替换worker图像(空闲,正在运行)。我只需要在底部添加另一个图像,此附加图像应与worker一起移动。

如果很难做到,那么也可以使用红色和绿色两种颜色的矩形代替图像来表示情绪状态。

完整代码:

worker

2 个答案:

答案 0 :(得分:2)

只需在特定位置相对于Worker的{​​{1}}和{{1 }}坐标。

不幸的是,我无法测试以下代码示例,因为您的代码使用了很多我没有的图像。我确实看到一个问题,您在尝试实施此代码之前需要先修复该问题,因为您正在初始化的Worker对象已添加到rect.x中,因此您可以考虑更改rect.yBackground,然后将all_sprites添加到其他组。

您还需要将all_spritesall_workers初始化为适合您的值。下面使用的值会将图像移动到工作人员的左下角。

这是示例代码:

Background

希望此答案对您有所帮助!请让我知道这是否适合您,如果您还有其他疑问,请随时在下面发表评论。

答案 1 :(得分:2)

我要么使用Micheal O'Dwyer的解决方案,然后在单独的for循环中平白图标图像,要么创建一个Icon精灵类,可以将其作为属性添加到Worker类中。然后,您只需在update方法中更新图标精灵的位置,并在工作人员状态更改时交换图像即可。

您需要一个LayeredUpdates组,以便该图标显示在工作人员画面上方。

import pygame as pg
from pygame.math import Vector2


pg.init()
WORKER_IMG = pg.Surface((30, 50))
WORKER_IMG.fill(pg.Color('dodgerblue1'))
ICON_HAPPY = pg.Surface((12, 12))
ICON_HAPPY.fill(pg.Color('yellow'))
ICON_ANGRY = pg.Surface((10, 10))
ICON_ANGRY.fill(pg.Color('red'))


class Worker(pg.sprite.Sprite):

    def __init__(self, pos, all_sprites):
        super().__init__()
        self._layer = 0
        self.image = WORKER_IMG
        self.rect = self.image.get_rect(center=pos)
        self.state = 'happy'
        self.emo_images = {'happy': ICON_HAPPY, 'angry': ICON_ANGRY}
        # Create an Icon instance pass the image, the position
        # and add it to the all_sprites group.
        self.icon = Icon(self.emo_images[self.state], self.rect.bottomright)
        self.icon.add(all_sprites)

    def update(self):
        # Update the position of the icon sprite.
        self.icon.rect.topleft = self.rect.bottomright

    def change_state(self):
        """Change the state from happy to angry and update the icon."""
        self.state = 'happy' if self.state == 'angry' else 'angry'
        # Swap the icon image.
        self.icon.image = self.emo_images[self.state]


class Icon(pg.sprite.Sprite):

    def __init__(self, image, pos):
        super().__init__()
        self._layer = 1
        self.image = image
        self.rect = self.image.get_rect(topleft=pos)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.LayeredUpdates()
    worker = Worker((50, 80), all_sprites)
    all_sprites.add(worker)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEMOTION:
                worker.rect.center = event.pos
            elif event.type == pg.KEYDOWN:
                worker.change_state()

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

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


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