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 draw text in monogame
// In the LoadContent() method:
SpriteFont font = Content.Load<SpriteFont>("sprite font location");
// In the Draw() method:
spriteBatch.Begin();
spriteBatch.DrawString(font, "Hello World!", position, color);
spriteBatch.End();
Example 3: pygame render text
"""system font"""
font = pygame.font.SysFont("Segoe UI", 35)
"""font from .ttf file"""
font = pygame.font.Font("path/to/font.ttf", 35)
textsurface = font.render("text", False, color)
surface.blit(textsurface, (x, y))
Example 4: how to render text in pygame
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render('Some Text', False, (0, 0, 0))
screen.blit(textsurface,(0,0))