Pygame - 使用Sprites渲染多行文本

问题描述 投票:1回答:1

斗争是我能够在Pygame中使用Sprites渲染一行文本,我也能够渲染多行文本,但没有Sprites,但我无法弄清楚如何使用Sprites渲染多行文本。

我有这个用于使用Sprites渲染一行文本(我将跳过基本的运行Pygame代码 - 背景,init等):

class Score(pygame.sprite.Sprite):
    .
    .
    def update(self, lives, score):
        .
        .
        self.text = "LIVES: %d  SCORE: %d" % (lives, score)
        self.image = self.font.render(self.text, True, self.color)
        self.rect = self.image.get_rect()
        .
        .

.
.
def main():
    .
    .
    score = pygame.sprite.RenderUpdates()
    score.add(Score())
    .
    .
    while 1:
        .
        .
        score.clear(screen, background)
        score_list = score.draw(screen)
        pygame.display.update(score_list)

        score.update(lives, score)
    .
    .

我只想知道是否可以使用我的渲染方法来实现它,或者我是否应该专注于以其他方式实现它?

也许是一个相关的问题;这种方法是在Pygame中渲染对象(图像)的正确方法吗?

感谢您的任何帮助。

python-2.7 pygame rendering sprite multiline
1个回答
1
投票

您可以先使用FONT.render渲染字符串,然后创建一个透明曲面并将单独的文本曲面blit到其上。要计算透明基础图像的宽度,可以使用单独文本曲面的.get_width()方法,并使用FONT.get_height()的高度。

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
FONT = pg.font.Font(None, 40)
BLUE = pg.Color('dodgerblue1')


class Score(pg.sprite.Sprite):

    def __init__(self, pos):
        super(Score, self).__init__()
        self.lives = 3
        self.score = 0
        self.rect = pg.Rect(pos, (1, 1))
        self.update_image()

    def update_image(self):
        height = FONT.get_height()
        # Put the rendered text surfaces into this list.
        text_surfaces = []
        for txt in ('lives {}'.format(self.lives),
                    'score {}'.format(self.score)):
            text_surfaces.append(FONT.render(txt, True, BLUE))
        # The width of the widest surface.
        width = max(txt_surf.get_width() for txt_surf in text_surfaces)

        # A transparent surface onto which we blit the text surfaces.
        self.image = pg.Surface((width, height*2), pg.SRCALPHA)
        for y, txt_surf in enumerate(text_surfaces):
            self.image.blit(txt_surf, (0, y*height))
        # Get a new rect (if you maybe want to make the text clickable).
        self.rect = self.image.get_rect(topleft=self.rect.topleft)


def main():
    score = Score((100, 100))
    all_sprites = pg.sprite.Group(score)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_s:
                    # Increment the score and update the image.
                    # It would be nicer to turn the score into a
                    # property and update the image automatically.
                    score.score += 1
                    score.update_image()

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

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


if __name__ == '__main__':
    main()
    pg.quit()
© www.soinside.com 2019 - 2024. All rights reserved.