Search code examples
functiontextpygame

How Can I Improve My Pygame Text Function?


I want to improve this text function I use hopefully using a dictionary to store values but I don't know how.

The code:

def text(text, x, y, size, color):
    # the font for the text
    font = pygame.font.Font(None, size)
    # the text itself
    txt = font.render(text, 0, color)
    # draw the text
    screen.blit(txt, (x, y))

Solution

  • You need to specify what font you will use "font_name.ttf"

    font_type = 'font_name.ttf'
    
    def draw_text(text, color, size, x, y):
        font = pygame.font.Font(font_type, size)
        text = font.render(text, False, color)
        text_rect = text.get_rect(center=(x, y))
        screen.blit(text, text_rect)
    

    I hope it will help you, Adam