I'm working on a project, and I'm rendering text to display on the window, but the text does not fit the screen when rendered in a single line, so I wrote a script to append a newline character whenever the text jumps out of the screen and it's working.
def display_text_animation(string,x,y): 32 text = '' 31 next_width = 0>> 30 for i in range(len(string)): 29 text += string[i] 28 if string[i] == '' and next_width == 0: 27 pass 26 try: 25 24 if string[i] == '' and i < len(string):>> 23 next_width = textSur_font.render(text + string[i + 1:string.find(" ", i + 1) if string.find(" ", i + 1) != -1 else len(string)], True, "#FFFFFF").ge t_width()>> 22⚠ 21 except: 20 pass 19>> 18 if (next_width >= SCREEN_WIDTH +100 ): 17 text += "\n" 16 next_width = 0 15 if text[-1] == "\n": 14 next_width = 0 13 text_surface = desc_font.render(text, True, "#FFFFFF", backGroundCol) 12 text_rect = text_surface.get_rect() 11 text_rect.x = x 10 text_rect.y = y 9 screen.blit(text_surface, text_rect)>> 8 7 pygame.display.update() 6 pygame.time.wait(40)
but I just noticed in the documentation that it's not suppose to work.and when i change the environment, it does not work and render everything in a single line.someone knows what's going on?
I'm running on
windows 11
python 3.11.5pygame-ce 2.4.1