簡體   English   中英

為什么我的文本被復制而不是包裝在 pygame 中?

[英]Why is my text being duplicated instead of wrapped in pygame?

我正在用 pygame 編寫一個基本的基於文​​本的游戲(我不是很有經驗,這是我的第一個問題),我從類文檔字符串的兩頁中按順序對字母進行了自動換行和動畫處理並排序的結合他們。 現在,文本動畫,但如果有一個單詞應該開始一個新行,它不會換行並且表現得像第二個附件中的那樣(在新行上重復)。 不知道在網上搜索什么。 蟒蛇3.8.5; 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()

它對一行的作用(正確)

它對一行的作用(正確)

它對應該包裝的文本有什么作用(不正確)

它對應該包裝的文本有什么作用(不正確)

您應該從這個問題中鏈接的第一個問題添加分行。 除了你可以使用這樣的東西:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM