Чтобы написать текст с помощью Pygame, вы можете использовать различные методы и функции, предоставляемые библиотекой Pygame. Вот несколько подходов, которые вы можете использовать:
-
Использование модуля
pygame.font:import pygame pygame.init() # Set up the display screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Pygame Text") # Set up the font font = pygame.font.Font(None, 36) # Render the text text = font.render("Hello, Pygame!", True, (255, 255, 255)) # Blit the text onto the screen screen.blit(text, (200, 200)) # Update the display pygame.display.flip() # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() -
Использование файла шрифта TrueType:
import pygame pygame.init() # Set up the display screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Pygame Text") # Load the font font_path = "font.ttf" font_size = 36 font = pygame.font.Font(font_path, font_size) # Render the text text = font.render("Hello, Pygame!", True, (255, 255, 255)) # Blit the text onto the screen screen.blit(text, (200, 200)) # Update the display pygame.display.flip() # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() -
Использование системных шрифтов:
import pygame pygame.init() # Set up the display screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Pygame Text") # Get a system font font_name = pygame.font.get_default_font() font_size = 36 font = pygame.font.SysFont(font_name, font_size) # Render the text text = font.render("Hello, Pygame!", True, (255, 255, 255)) # Blit the text onto the screen screen.blit(text, (200, 200)) # Update the display pygame.display.flip() # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit()
Эти методы демонстрируют различные способы рендеринга и отображения текста в Pygame. Не забудьте настроить параметры, положение и цвет шрифта в соответствии со своими требованиями.