Pygame fonts and text

Pygame fonts and text

Fonts also play an important role in games. Such as displaying scores, dialogue between characters, and so on.
Fonts in Pygame use ".ttf" files, which stand for True Type File.

fonts and text

There are generally two different ways to use fonts in pygame.

  1. pygame.font.Font()
  2. pygame. font. SysFont().
    The difference between them is that pygame.font.Font() requires the file path of the font to be passed as its parameter, while pygame.font.SysFont() only requires the name of the font.

Pygame.font.Font()

pygame.font.Font() is mainly used to use font files outside the system. For example, if you have a file xx.ttf in the same directory as the python file, you can use it with the following code.

pygame.font.Font("xx.ttf", 20)

The first parameter is the file path and the second is the font size.

The obvious benefit of this is that there is no chance that your chosen font will not be available.

Another thing you can do is make your program use the system default font from the start. This method is 100% bug-free and not prone to any "missing font" issues.

font = pygame.font.Font(None, size)

Keep in mind that system default fonts vary from system to system.

It may happen that the font type you specified cannot be found or some error occurs. In this case pygame will fall back to the default system font.

Pygame.font.SysFont()

If you don't include any ttf files, you can use pygame.font.SysFont().

A good strategy is to first find a font supported by the system the code is executing on. The pygame.font.get_fonts() function will return a list of all font names it can find on your system. We run the code on the Windows desktop and receive the following output.

['arial', 'arialblack', 'bahnschrift', 
'calibri', 'cambriacambriamath', 'cambria', 
......
]

Different systems will have different output results. Next you can simply select one and pass it to the SysFont() function.

Use system fonts

chat_font = pygame.font.SysFont('arial', 15)
name_font = pygame.font.SysFont('Helvetica', 20)
game_over_font = pygame.font.SysFont('Verdana', 60)

The SysFont() function requires only the name of the font, not the file path. The second parameter remains unchanged and represents the font size.

As you can see, we have created several types of fonts. The reason is that in normal games, many different types of fonts are used for different parts of the text. It is very unusual to use the same type of font family and size throughout the game.

For example, we created a font type for dialogue using a fairly small font size. For the "GAME OVER" text that many games have, we used a larger font. We even created a different font for the names with a slightly larger font size than the text (to make it stand out a bit).

chat = chat_font.render("Hey there, Beautiful weather today!",
                                True, (0,0,0))
name = name_font.render("John Hubbard", True, (0,0,255))
game_over = game_over_font.render("Game Over", True, (255,0,0))

Defining a font is only the first step. Next, create a Surface object with the font. You can also decide the color at render time. The True parameter represents anti-aliasing, which is used to make the edges smoother. Pass True if you want to use it, otherwise pass False.

screen.blit(dialogue, (40,40))
screen.blit(name, (40,140))
screen.blit(game_over, (40,240))

The last step is to actually display the object on the screen. For this, we use the surface.blit() function.

Pygame font example

Now we actually put all the code shown above in a comprehensive program and show you the output.

The code of our program.

import pygame

pygame.init()
#定义颜色
CHAT_COLOR = (0,0,0)
NAME_COLOR = (0,0,255)
GAME_OVER_COLOR = (255,0,0)

screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

chat_font = pygame.font.SysFont('arial', 15)
name_font = pygame.font.SysFont('Helvetica', 20)
game_over_font = pygame.font.SysFont('Verdana', 60)

chat = dialogue_font.render("Hello,nict to meet you!",True, CHAT_COLOR)
name = name_font.render("ubuntu", True,NAME_COLOR )
game_over = game_over_font.render("Game Over", True, GAME_OVER_COLOR)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    screen.fill((255, 255, 255))
    screen.blit(dialogue, (40,40))
    screen.blit(name, (40,140))
    screen.blit(game_over, (40,240))
    
    pygame.display.flip()
    clock.tick(60)

Guess you like

Origin blog.csdn.net/qq_67984864/article/details/128972214