简体   繁体   English

在pygame中编写文本时出现奇怪的矩形

[英]Strange rectangles appearing when writing text in pygame

I'm new to pygame and python in general, so I suppose there's a possibility for major errors here. 我是pygame和python的新手,所以我认为这里可能会出现重大错误。

I'm trying to make text appear in a dialog box. 我正在尝试使文本出现在对话框中。 I've worked out the system and the text appears correctly, but when the text loads, a vertical rectangle symbol appears at the end of the text. 我已经设计好系统,并且文本可以正确显示,但是当文本加载时,文本的结尾会出现一个垂直的矩形符号。

It seems clear to me that this symbol represents the 'return' key. 在我看来,这个符号代表“返回”键。 Here's the text file I'm pulling from: 这是我从中提取的文本文件:

00*It’s your favorite game console.
01*-Player's House-

Here's how the text appears in-game. 这里的文本如何出现在游戏中。

And here's my code to break down the text file into segments: 这是我的代码,用于将文本文件分解为段:

with open("textList.txt", "r") as f:
    data = f.readlines()
    for line in data:
        cutInfo = line.split("*")
        if dblock.textnum == cutInfo[0]:
            self.text = cutInfo[1]

I'm wondering if there's a way to get rid of the rectangle without simply cutting off the end of the string, and I also want to preserve the returns within the text file so it's more readable (And isn't condensed into a single line). 我想知道是否有一种摆脱矩形的方法,而不必简​​单地切断字符串的结尾,而且我还想将返回值保留在文本文件中,以使其更具可读性(并且不会压缩成一行) )。 However, if there's no workaround, I suppose I could make those changes. 但是,如果没有解决方法,我想我可以进行这些更改。

for line in file: retains the trailing newlines. for line in file:保留尾随的换行符。

You can use line.strip() to get rid of leading and trailing whitespace. 您可以使用line.strip()摆脱前导和尾随空格。

cutInfo = line.strip().split("*")

If you don't want to get rid of all whitespace, 如果您不想摆脱所有空白,

  • .strip('\\r\\n') gets rid of newline characters only. .strip('\\r\\n')仅摆脱换行符。
  • .rstrip('\\r\\n') gets rid of trailing newline characters only. .rstrip('\\r\\n')仅消除尾随换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM