How to configure pygame in English?

Asked

Viewed 275 times

4

I’m playing a game with pygame. I want to show a text in Portuguese on the screen, but the same exchange "ç", "ã", "[", and "<" for "?" (question mark).

import pygame
class TelaInicial():
    def __init__(self, logoTipo = None, lagura_do_logo_tipo = None, altura_do_logo_tipo = None, posicao_x_do_logo_tipo = None, posicao_y_do_logo_tipo = None):
        self.altura_do_logo_tipo = 350
        self.lagura_do_logo_tipo = 250
        self.posicao_x_do_logo_tipo = pygame.display.Info().current_w - 700
        self.posicao_y_do_logo_tipo = pygame.display.Info().current_h - 450
        self.logoTipo = pygame.transform.scale(pygame.image.load('./imagens/logo-tipo.png'), (self.lagura_do_logo_tipo, self.altura_do_logo_tipo))

    def logo_tipo(self, tela):
        tela.blit(self.logoTipo, (self.posicao_x_do_logo_tipo, self.posicao_y_do_logo_tipo))

    def aperteEspaco(self, tela):
        font = pygame.font.Font("./fonts/smbfont.ttf", 45)
        texto = font.render("APERTE ESPAÇO", True, (255, 255, 255))
        tela.blit(texto, (pygame.display.Info().current_w - 700, pygame.display.Info().current_h - 80))

    def background(self):
        pass

inserir a descrição da imagem aqui

  • 1

    Really put your code and not a print of it, explain your question better since it is too wide, and make explicit the point to which you had difficulty.

3 answers

5


If the symbols appear as ? it simply means the source file you are using - smbfont.ttf does not include these -symbols and they are replaced by "?".

This is a very common problem with low quality sources, such as those distributed by the thousands on CD-Roms on newsstands in the 1990s and 2000s.

When using a source in an "normal" application of the operating system, the system itself takes care of doing this substitution, when it is like this. In Pygame, the control is more manual - you have to do everything by hand. Unfortunately, the API that Pygame presents does not allow introspection in the loaded fonts to know if a symbol exists or not.

This type of accent error and etc... is common in applications that use the terminal (cmd) in Windows, or Pygame applications using Python 2, because of different text encodings (e.g. "utf-8", "CP1252", etc...) - but in Python3, where the program text is all treated as Unicode, this problem no longer exists - using Pygame, if a character exists in the font you are using, it will be drawn correctly.

In short: what will fix the thing for you is to use a font that includes accented characters and symbols, as well as only uppercase letters. I recommend you look at the fonts that Google makes available on https://fonts.google.com/ : are all permitted use and very high quality (it’s just a little boring to browse until you can download them as a TTF file, but it’s possible)

2

Try specifying a font instead of the one you are using (are you using the default?) - it is more likely to be a problem in the source.

Words of the original author

  • It worked for me with all the fonts I tried on my system.

For full games, an ideal solution is to distribute the source file next to your project - as open Pygame fonts are named ". ttf".

To use a system source, you have to chain calls pygame.font.get_fonts() to get source names, to pygame.font.match_font to get the path to the source file given the source name and finally, pygame.font. Font passing this path and a point size to get a usable source object (with the rendering method)

Source

Similar problems

Rendering Unicode in pygame

Rendering Unicode font with pygame

  • Thank you for quoting my answer there - but unfortunately the problem there is another - the A.P. is no longer using the original source.

-1

I looked at a code I have and simply adding in Unicode at the beginning of the code and starting the source it displays the characters normally.

# -*- coding: UTF-8 -*-


#Init the font module
        pygame.font.init()
        #Instance of Font
        font = pygame.font.SysFont('Arial', 25)
  • 1

    This statement is not required in Python 3. But she has never done any magic - she only warns the Python complator about the accentuation encoding used in the actual arqiuvo in which it appears. If this were missing, (in Python 2), the author of the code would have had a syntax error, not incorrect characters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.