1

I've been trying to make a multi-line text renderer that renders in real-time with pygame. I've basically successfully made one, however the text-renderer I've made MUST ALWAYS have 3 lines of text otherwise it won't render.

What I'd like to do is to have a text renderer that can render as many lines that's inputted into it then have those lines render in real time.

additionally, if it's possible, I'd also like the renderer to split lines based on the maximum size of an image. namely, a textBox for a Visual Novel

The function will be used as a text renderer for a visual novel to be clear

Old text renderer

def textRendererOld(string, char):
        text1, text2, text3 = string.split('\n', 3)
        blit1, blit2, blit3 = "", "", ""
    
        # remember to add a confirm token so that the text renders consecutively from text1 to text3
    
        for i in range(len(text1)):
            blit1 += text1[i]
            textSurface = charFonts[char].render(blit1, True, WHITE)
    
            DISPLAY.blit(textSurface, (TEXT_X_POS, TEXT_Y_POS))
            pygame.display.update()
            pygame.time.wait(TEXT_RENDER_SPEED)
    
        for i in range(len(text2)):
            blit2 += text2[i]
            textSurface = charFonts[char].render(blit2, True, WHITE)
    
            DISPLAY.blit(textSurface, (TEXT_X_POS, TEXT_Y_POS + PARAGRAPH_BREAK_GAP))
            pygame.display.update()
            pygame.time.wait(TEXT_RENDER_SPEED)
    
        for i in range(len(text3)):
            blit3 += text3[i]
            textSurface = charFonts[char].render(blit3, True, WHITE)
    
            DISPLAY.blit(textSurface, (TEXT_X_POS, TEXT_Y_POS + PARAGRAPH_BREAK_GAP * 2))
            pygame.display.update()
            pygame.time.wait(TEXT_RENDER_SPEED)

My attempt at a multi-line text rederer

def textRenderer(dialogue, character):

    textLines = []
    talkText = dialogue.split('\n', -1)
    textLines.append(talkText)



 for line in len(textLines):
        text = ''
        ParagraphBreak = 0
        for i in range(len(textLines)):
            text += textLines[line]
            textSurface = charFonts[character].render(text, True, WHITE)

            DISPLAY.blit(textSurface, (TEXT_X_POS, TEXT_Y_POS))
            pygame.display.update()
            pygame.time.wait(TEXT_RENDER_SPEED)

            ParagraphBreak = ParagraphBreak + PARAGRAPH_BREAK_GAP
  • 3
    Is there a specific reason why you aren't using the standard Pygame font rendering functions? – scotty3785 May 06 '21 at 10:59
  • Split the total text into parts that have a `width` less than the window width. To do that, you can for example check each word one by one, then add it to the last line if is does fit in the window and otherwise create a new line (you could use a list for that). – D_00 May 06 '21 at 17:17
  • Yeah, I want the text to render in real time and from my understanding, the regular render function only renders texts all at once. – Karl Mark Funcion May 08 '21 at 03:21
  • Well, yes, but you can draw "M" "My" "My "My t" "My te" "My tex" "My text" etc., to simulate scrolling text. My GUESS is it would be less work than doing your own text. – Tim Roberts May 08 '21 at 03:30

0 Answers0