python pygame text in GUI code example
Example 1: python how to write text in pygame
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
def set_text(string, coordx, coordy, fontSize):
font = pygame.font.Font('freesansbold.ttf', fontSize)
text = font.render(string, True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (coordx, coordy)
return (text, textRect)
window.fill((255, 255, 255))
totalText = set_text("Text in Pygame!", 250, 250, 60)
window.blit(totalText[0], totalText[1])
pygame.display.update()
Example 2: how to write a font in pygame
font = pygame.font.SysFont(None, 24)
img = font.render('hello', True, BLUE)
screen.blit(img, (20, 20))