Python/Pygame在Pygame中使文本在退出窗口时自动换行

Python/Pygame在Pygame中使文本在退出窗口时自动换行,python,text,pygame,word-wrap,Python,Text,Pygame,Word Wrap,我有一个文本函数来呈现文本 def textFunc(font,msg,color,x,y,center): text_render = font.render(msg,True,color) text_rect = text_render.get_rect() #If center is true, then the X,Y will be used as the center if center == True: text_rect.

我有一个文本函数来呈现文本

def textFunc(font,msg,color,x,y,center):
    text_render = font.render(msg,True,color)
    text_rect = text_render.get_rect()
    
    #If center is true, then the X,Y will be used as the center
    if center == True:
        text_rect.center = (x,y)
    else:
        text_rect = (x,y)
    game_display.blit(text_render,text_rect)
但是,我的msg字符串太长,它将在窗口外呈现

是否有一种方法可以让我的文本注册,因为它对于我的窗口来说太长,所以会继续在文本的开头下方? 与计算机如何自动调用类似


有点像这样:

没有自动解决方案。您必须自己实现文本换行,并逐字逐行分别绘制文本。
幸运的是,PyGame wiki提供了一个用于此任务的函数。请参阅PyGame wiki

我扩展了该函数并添加了一个附加参数,该参数提供左对齐或右对齐文本、居中文本甚至块模式:

textAlignLeft=0
textAlignRight=1
textAlignCenter=2
textAlignBlock=3
def drawText(表面、文本、颜色、矩形、字体、对齐=文本对齐左、aa=假、bkg=无):
行距=-2
spaceWidth,fontHeight=font.size(“”[0],font.size(“Tg”)[1]
listOfWords=text.split(“”)
如果是bkg:
imageList=[font.render(word,1,color,bkg)表示ListoWords中的word]
对于imageList中的图像:image.set_colorkey(bkg)
其他:
imageList=[font.render(word、aa、颜色)表示ListoWords中的word]
maxLen=rect[2]
lineLenList=[0]
lineList=[]]
对于imageList中的图像:
宽度=图像。获取宽度()
lineLen=lineLenList[-1]+len(lineList[-1])*spaceWidth+width
如果len(lineList[-1])==0或lineLen 1:
spaceWidth=(rect[2]-lineLen)/(len(lineImages)-1)
如果lineBottom+fontHeight>rect[1]+rect[3]:
打破
最后一行+=1
对于i,枚举中的图像(线图像):
x、 y=lineLeft+i*空间宽度,线条底部
表面光点(图像,(圆形(x),y))
lineLeft+=图像。获取_宽度()
lineBottom+=字体高度+行距
如果lastLine

最简单的例子:

导入pygame
pygame.init()
font=pygame.font.SysFont(无,40)
msg=“一个简单的函数,它将绘制文本并将其包装以适合传递的rect。如果有任何文本不适合该框,则将返回剩余的文本。”
textRect=pygame.Rect(100100300300)
window=pygame.display.set_模式((500500))
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((255,255,255))
pygame.draw.rect(窗口,(0,0,0),textRect,1)
drawTextRect=textRect.充气(-5,-5)
drawText(窗口,消息,(0,0,0),drawTextRect,字体,textAlignBlock,True)
pygame.display.flip()

没有自动解决方案。您必须自己实现文本换行,并逐字逐行分别绘制文本。
幸运的是,PyGame wiki提供了一个用于此任务的函数。请参阅PyGame wiki

我扩展了该函数并添加了一个附加参数,该参数提供左对齐或右对齐文本、居中文本甚至块模式:

textAlignLeft=0
textAlignRight=1
textAlignCenter=2
textAlignBlock=3
def drawText(表面、文本、颜色、矩形、字体、对齐=文本对齐左、aa=假、bkg=无):
行距=-2
spaceWidth,fontHeight=font.size(“”[0],font.size(“Tg”)[1]
listOfWords=text.split(“”)
如果是bkg:
imageList=[font.render(word,1,color,bkg)表示ListoWords中的word]
对于imageList中的图像:image.set_colorkey(bkg)
其他:
imageList=[font.render(word、aa、颜色)表示ListoWords中的word]
maxLen=rect[2]
lineLenList=[0]
lineList=[]]
对于imageList中的图像:
宽度=图像。获取宽度()
lineLen=lineLenList[-1]+len(lineList[-1])*spaceWidth+width
如果len(lineList[-1])==0或lineLen 1:
spaceWidth=(rect[2]-lineLen)/(len(lineImages)-1)
如果lineBottom+fontHeight>rect[1]+rect[3]:
打破
最后一行+=1
对于i,枚举中的图像(线图像):
x、 y=lineLeft+i*空间宽度,线条底部
表面光点(图像,(圆形(x),y))
lineLeft+=图像。获取_宽度()
lineBottom+=字体高度+行距
如果lastLine

最简单的例子:

导入pygame
pygame.init()
font=pygame.font.SysFont(无,40)
msg=“一个简单的函数,它将绘制文本并将其包装以适合传递的rect。如果有任何文本不适合该框,则将返回剩余的文本。”
textRect=pygame.Rect(100100300300)
window=pygame.display.set_模式((500500))
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((255,255,255))
pygame.draw.rect(窗口,(0,0,0),textRect,1)
drawTextRect=textRect.充气(-5,-5)
drawText(窗口,消息,(0,0,0),drawTextRect,字体,textAlignBlock,True)
pygame.display.flip()