我为textarea创建了一个类,并创建了将文本分成几行的函数。每次文本变量的长度达到矩形的末端时,都会添加一个'\ n'。但是该函数似乎增加了太多换行符!我该如何解决?
我的代码如下:
def blittxt(surface, text, pos, font, color=pygame.Color('white')):
words = [word.split(' ') for word in text.text.splitlines()]
space = font.size(' ')[0]
max_width, max_height = surface.get_size()
x, y = pos
for line in words:
for word in line:
word_surface = font.render(word, 0, color)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width-60:
text.text += '\n'
surface.blit(word_surface, (x, y))
x += word_width + space
x = pos[0]
y += word_height
谢谢!