简体   繁体   中英

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.

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 (eg 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'\✓', self.color, position, size) method or directly using write(screen, "✓", self.color, position, size)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM