Jan-05-2023, 01:59 PM
(This post was last modified: Jan-06-2023, 12:47 AM by Yoriz.
Edit Reason: removed unnecessary quote of previous post
)
Okay, thanks again.
Can I display text in multiple lines in pygame somehow so I can avoid writing a large function like this?
I want to explain the rules of the game.
Let's say in an easy way that I want to display "game over" like this:
Can I display text in multiple lines in pygame somehow so I can avoid writing a large function like this?
I want to explain the rules of the game.
def blit_text(surface, text, pos, font, color=pygame.Color('black')): words = [word.split(' ') for word in text.splitlines()] # 2D array where each row is a list of words. space = font.size(' ')[0] # The width of a space. max_width, max_height = surface.get_size() x, y = pos for line in words: for word in line: word_surface = font.render(word, 0, color) word_width, word_height = word_surface.get_size() if x + word_width >= max_width: x = pos[0] # Reset the x. y += word_height # Start on new row. surface.blit(word_surface, (x, y)) x += word_width + space x = pos[0] # Reset the x. y += word_height # Start on new row.
Let's say in an easy way that I want to display "game over" like this:
Game
over!
over!