Search code examples
pythonpygametext-rendering

How to display an utf-8 character in pygame?


Is there a way to render a tick mark using pygame? I tried using the unicode directly like this:

def write(screen, text, color, position, size):
    font = pygame.font.Font(pygame.font.get_default_font(), size)# Defining a font with font and size
    text_surface = font.render(text, True, color)# Defining the text color which will be rendered
    screen.blit(text_surface, (position[0], position[1])) # Rendering the font

write(window, u'\u2713', self.color, position, size) 

But this just draws a rectangle.


Solution

  • This 'rectangle' that is being written is in fact the representation of the sign using a pygame.font.get_default_font(). In order to display a sign you need to be sure, that this sign is included in a font that you are using. I propose the following solution:

    1. Download the font that includes this symbol (e.g. Segoe UI Symbols from here)
    2. Unzip the font package into the data folder (or main applicaation folder)
    3. Change the font that you are using to the downloaded one
        font = pygame.font.Font("seguisym.ttf", size)
    
    1. Use a write function either using a write(window, u'\u2713', self.color, position, size) method or directly using write(screen, "✓", self.color, position, size)