Search code examples
python-2.7pygamerenderingspritemultiline

Pygame - rendering multiline text using Sprites


Struggle is that I am capable of rendering one line of text with Sprites in Pygame, I am also capable of rendering multi-line text, but without the Sprites, but I can't figure out way how to render multi-line text using Sprites.

I've got this for rendering one line text using Sprites (I am going to skip the basic for running Pygame code - background, init etc.):

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)
    .
    .

I just want to know if it's even possible to do it using my render method or if I should focus on doing it some other way?

And maybe one related question; Is this method the right way to render objects (images) in Pygame?

Thank you for any help.


Solution

  • You could render the strings with FONT.render first, then create a transparent surface and blit the separate text surfaces onto it. To figure out the width of the transparent base image, you can use the .get_width() method of the separate text surfaces and for the height you can use 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()