Pygame自动换行

时间:2014-10-10 18:42:38

标签: python python-2.7 pygame

我正在努力在我的游戏中显示文字我正在创作。我在文本之间绘制了一个很好的空间,但是用文字包装文本重叠只是一点点。以下是我如何包装文字。

def reOrderChat(nText, nColor):
    ''' Reorders the chat text on screen and creates new lines '''

    # rearrange the lines first so they stay correct.
    for c in range(19, 0, -1):
        DrawnChat[c + 1].text = DrawnChat[c].text
        DrawnChat[c + 1].color = DrawnChat[c].color
        DrawnChat[c + 1].y = DrawnChat[c].y - 8
        #print "Text at " + str(c + 1) + " is " + DrawnChat[c + 1].text

    # check if textlength is greater than the chat list length
    textLength = g.chatFont.size(nText)[0]

    if textLength > 459:
        # todo: allow custom size chat list (459 is found with GIMP)
        # todo: optimize the loop

        lines = ['']

        words = nText.split()
        curLine = 0
        curLineLength = 0

        for i in range(len(words)):
            word = words[i]

            # if adding the new word to the current line would be too long,
            # then put it on a new line
            wordLength = g.chatFont.size(word)[0]

            if (curLineLength + wordLength) > 400: # todo: why does it only work with 400?

                # only move down to a new line if we have text on the current line
                lines.append('')
                curLine += 1
                curLineLength = 0

            lines[curLine] += word + ' '
            curLineLength += wordLength

        curLine +=1

        for line in lines:
            print line
            print str(curLine)
            # here is where it creates new chat lines. This is where the issue is.
            DrawnChat[curLine].text = line
            DrawnChat[curLine].color = nColor
            DrawnChat[curLine].y += 8
            curLine -= 1


    else:
        # text fits, so just print it
        # lastly add the new text and color at position 1
        DrawnChat[1].text = nText
        DrawnChat[1].color = nColor
        DrawnChat[1].y  += 8

以下是文字的屏幕截图。在前两行之间没有足够的空间。通过前两行到MOTD后的间距:很好。当有多行时,我是否设置了y值错误?

Text to screen issue

1 个答案:

答案 0 :(得分:0)

您是否尝试增加线条之间的间距?

DrawnChat[curLine].y += 12

如果这不起作用;如果表面高度合适,请检查。

如果您在任何时候,请认为有必要更改字体大小。请考虑使用变量而不是硬编码DrawnChat[curLine].y += 8