【问题标题】:Pygame - rendering multiline text using SpritesPygame - 使用 Sprites 渲染多行文本
【发布时间】:2017-12-17 09:57:47
【问题描述】:

斗争是我能够在 Pygame 中用 Sprites 渲染一行文本,我也能够渲染多行文本,但是没有 Sprites,但我不知道如何渲染多行使用 Sprite 的文本。

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

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 中渲染对象(图像)的正确方法吗?

感谢您的帮助。

【问题讨论】:

  • 如果你只想渲染生命和分数,我认为最简单的解决方案是分别创建和blit两个文本表面。
  • 确实如此,但这就是我在没有 Sprites 时提到的那样做的方式,我不想那样做。由于程序/游戏实际上非常复杂,我正在以这种方式渲染其他对象。另外,它可能会减慢速度。

标签: python-2.7 pygame rendering sprite multiline


【解决方案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()

【讨论】:

  • 非常感谢您的帮助。有效。我没有意识到我可以在那里使用“Surface.blit(surface, rect)”。
猜你喜欢
  • 1970-01-01
  • 2017-06-20
  • 2014-07-13
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多