1import pygame
2pygame.init()
3window = pygame.display.set_mode((500, 500))
4
5def set_text(string, coordx, coordy, fontSize): #Function to set text
6
7 font = pygame.font.Font('freesansbold.ttf', fontSize)
8 #(0, 0, 0) is black, to make black text
9 text = font.render(string, True, (0, 0, 0))
10 textRect = text.get_rect()
11 textRect.center = (coordx, coordy)
12 return (text, textRect)
13
14window.fill((255, 255, 255)) #Fills the whole window with white
15#Places "Text in Pygame!" with an x,y coord of 250, 250 and 60 font size
16totalText = set_text("Text in Pygame!", 250, 250, 60)
17window.blit(totalText[0], totalText[1])
18pygame.display.update()
1def writeText(string, coordx, coordy, fontSize):
2 #set the font to write with
3 font = pygame.font.Font('freesansbold.ttf', fontSize)
4 #(0, 0, 0) is black, to make black text
5 text = font.render(string, True, (0, 0, 0))
6 #get the rect of the text
7 textRect = text.get_rect()
8 #set the position of the text
9 textRect.center = (coordx, coordy)
10 #add text to window
11 window.blit(text, textRect)
12 #update window
13 pygame.display.update()
1"""system font"""
2font = pygame.font.SysFont("Segoe UI", 35)
3
4"""font from .ttf file"""
5font = pygame.font.Font("path/to/font.ttf", 35)
6
7textsurface = font.render("text", False, color) # "text", antialias, color
8surface.blit(textsurface, (x, y))
1font = pygame.font.SysFont(None, 24)
2img = font.render('hello', True, BLUE)
3screen.blit(img, (20, 20))