I have this function that renders text in multiple lines in pygame and does not allow the width of the text to exceed a certain width. Latter in the script, inside the while loop, I am going to pass some random size text that I get from websockets inside this function so that it is rendered on screen. I want to know how can I acess inside the while loop the height of any specific text that is rendered by this function.
def display_text(surface,text,pos,font,color=(250,250,250)):
collection = [word.split(' ') for word in text.splitlines()]
space = font.size(' ')[0]
x,y = pos
for lines in collection:
for words in lines:
word_surface = font.render(words, 0, color)
word_width, word_height = word_surface.get_size()
if x + word_width >= screen_w-75:
x = pos[0]
y += word_height
surface.blit(word_surface, (x,y))
x += word_width + space
x = pos[0]
y += word_height