【问题标题】:Text in pygame thats formatted as a paragraph?pygame中的文本格式化为段落?
【发布时间】:2014-05-25 18:18:45
【问题描述】:

试图写一个 pygame 程序,但我似乎无法弄清楚如何让 pygame 将文本格式化为没有第三方模块的段落。例如,我想要:

"Hello World, how are you doing today? I'm fine actually, thank you."

更接近于:

"Hello World, how are
 you doing today? I'm
fine actually, thank you."

这是我的代码:

def instructions(text):
    pressKey, pressRect = makeText('Last Block is a survival game. Every ten lines that you clear will shorten the screen, clear as many lines as possible before you run out of space!', smallFont, bgColor)
    pressRect.center = (int(windowWidth/ 2), int(windowHeight / 2) + 100)
    displaySurf.blit(pressKey, pressRect)
    while press() == None:
        pygame.display.update()

makeText 函数:

def makeText(text, font, color):
    surf = font.render(text, True, color)
    return surf, surf.get_rect()

这样 pygame 可以将一长串文本分成 x 个单词的部分,并将其格式化为看起来有点像一个段落。现在,我正在使用大量的表面对象和 blits 让它看起来像这样,这看起来很乏味。还有什么方法可以达到效果?

【问题讨论】:

  • “外部模块”是指第三方模块还是不是pygame 的每个模块?换句话说,您可以使用内置模块吗?因为,如果是这样,您可能会对内置的textwrap 模块感兴趣。
  • 第 3 方 = 外面。如果可能,您能快速解释一下textwrap 模块吗?

标签: python text pygame


【解决方案1】:

如果我理解正确,您需要一些代码以在多行段落中显示文本。如果是这样,我在这里使用了代码:http://www.pygame.org/pcr/text_rect/index.php

不需要任何额外的模块。该方法采用以下参数:

def render_textrect(string, font, rect, text_color, background_color, justification=0):
    """Returns a surface containing the passed text string, reformatted
    to fit within the given rect, word-wrapping as necessary. The text
    will be anti-aliased.

    Takes the following arguments:

    string - the text you wish to render. \n begins a new line.
    font - a Font object
    rect - a rectstyle giving the size of the surface requested.
    text_color - a three-byte tuple of the rgb value of the
                 text color. ex (0, 0, 0) = BLACK
    background_color - a three-byte tuple of the rgb value of the surface.
    justification - 0 (default) left-justified
                    1 horizontally centered
                    2 right-justified

    Returns the following values:

    Success - a surface object with the text rendered onto it.
    Failure - raises a TextRectException if the text won't fit onto the surface.
    """

很抱歉再次发帖 - 误点击了社区 wiki。

【讨论】:

  • recttype 参数是什么?
  • 不确定你从哪里得到recttype。我已经更新了答案以显示您需要传递的参数。如果您指的是rect,这只是一个 pygame.Rect 对象 (pygame.org/docs/ref/rect.html),它基本上是您希望文本所在区域的大小。
【解决方案2】:

听起来您正在寻找textwrap.fill提供的功能:

from textwrap import fill
mystr = "Hello World, how are you doing today? I'm fine actually, thank you."
print(fill(mystr, 20))
print()
print(fill(mystr, 40))
print()
print(fill(mystr, 10))

输出:

你好,世界,你好 你今天在做什么?我是 其实很好,谢谢 你。 你好,世界,你今天好吗? 其实我很好,谢谢。 你好 世界,如何 你是 正在做 今天?我是 美好的 实际上, 谢谢你。

textwrap.fill 的第一个参数是您要拆分的字符串。第二个是行的最大长度(以字符为单位)。

【讨论】:

  • 嗨,我在创建 pygame 文本对象的函数中尝试了这个。 fill 没有分割线,而是在它们之间创建了框。我将编辑我的问题以包含完整的功能。我不相信我在 pygame 中使用字符串,而是将它作为另一个参数传递给函数。
猜你喜欢
  • 1970-01-01
  • 2017-05-24
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多