[英]Why is my text being duplicated instead of wrapped in pygame?
I'm writing a basic text-based game with pygame (I'm not too experienced and this is my first question), and I took word-wrapping and animating the letters in a sequence from the two pages in the class docstring and sort of combined them.我正在用 pygame 编写一个基本的基于文本的游戏(我不是很有经验,这是我的第一个问题),我从类文档字符串的两页中按顺序对字母进行了自动换行和动画处理并排序的结合他们。 Right now, the text animates, but if there's a word that should start a new line it doesn't wrap and behaves as it does in the second attachment (duplicates on new line).现在,文本动画,但如果有一个单词应该开始一个新行,它不会换行并且表现得像第二个附件中的那样(在新行上重复)。 Wasn't sure what to search online for this.不知道在网上搜索什么。 Python 3.8.5;蟒蛇3.8.5; pygame 2.1.2. pygame 2.1.2。
class DynamicText:
"""
Displays and word-wraps text.
https://stackoverflow.com/questions/42014195/rendering-text-with-multiple-lines-in-pygame
https://stackoverflow.com/questions/31381169/pygame-scrolling-dialogue-text
"""
def __init__(self, text, pos, font, color=WHITE, autoreset=False):
self.done = False
self.font = font
self.text = text
self._gen = self.text_generator(self.text)
self.pos = pos
self.color= color
self.autoreset = autoreset
self.update()
def reset(self):
self._gen = self.text_generator(self.text)
self.done = False
self.update()
def update(self):
if not self.done:
try: self.word_wrap()
except StopIteration:
self.done = True
if self.autoreset: self.reset()
def word_wrap(self):
words = [word.split(' ') for word in self.text.splitlines()] # 2D array where each row is a list of words.
space = self.font.size(' ')[0] # The width of a space.
max_width, max_height = textsurface.get_size()
x, y = self.pos
for line in words: #This is what actually renders the text!
word_surface = self.rendered = self.font.render((next(self._gen)), True, self.color)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width:
x = self.pos[0] # Reset the x.
y += word_height # Start on new row.
textsurface.blit(word_surface, (x, y))
x += word_width + space
x = self.pos[0] # Reset the x.
y += word_height # Start on new row.
def text_generator(self,text):
tmp = ""
for letter in text:
tmp += letter
# don't pause for spaces
if letter != " ":
yield tmp
if __name__=='__main__':
running=True
ds=DoStuff() #makes screen, textbox, image rect
msg=DynamicText("Hey, does rendering this work? What about actual multiline text?", (10,10), textdata, WHITE)
while running:
screen.blit(textsurface,(20,10))
screen.blit(imgsurface,(650,10))
clock.tick(60)
pg.display.update()
for event in pg.event.get():
if event.type==QUIT:
running=False
if event.type==pg.USEREVENT:
msg.update()
pg.quit()
sys.exit()
What it does with one line (correct)它对一行的作用(正确)
What it does with text that's supposed to be wrapped (incorrect)它对应该包装的文本有什么作用(不正确)
you should add the line division from the first question linked in this question.您应该从这个问题中链接的第一个问题添加分行。 As well as you can use something like this:除了你可以使用这样的东西:
body = ['Be careful with your inputs, game does not have good error handling!',
'To choose who begins the game input "a" for computer or "b" for yourself',
'Press enter to play']
label = []
for line in range(len(body)):
label.append(word_font.render(body[line], True, yellow)) #creates text lable
for line in range(len(label)):
disp.blit(label[line], [display_w / 5, 150 + 25 * line]) #positions the text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.