1

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.

mikro45
  • 63
  • 1
  • 1
  • 9
  • If you only want to render the lives and the score, I think the simplest solution would be to create and blit two text surfaces separately. – skrx Dec 17 '17 at 11:05
  • True that, but that’s the way I would do it as I mentioned without Sprites and I do not want to do it that way. As the program / game is actually pretty complex and I am rendering other objects this way. Plus it would probably slow it down. – mikro45 Dec 17 '17 at 11:36

1 Answers1

1

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()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • 1
    Thank you a lot for your help. It worked. I didn't realize that I could use "Surface.blit(surface, rect)" there. – mikro45 Dec 17 '17 at 13:44
  • [`pygame.Font.render`](https://pyga.me/docs/ref/font.html#pygame.font.Font.render) in `pygame-ce` since `2.1.4` supports newlines so no need for workarounds like this anymroe, more info about the fork can be found [here](https://www.reddit.com/r/pygame/comments/1112q10/pygame_community_edition_announcement/) – Matiiss Mar 07 '23 at 21:43