How to display text on the screen with Pygame

Another video tutorial of coding with russ about an interesting topic: how to display text with pygame

  1. create a font (font = pygame.font.SysFont(“Arial”,20))
  2. render it (text = font.render(“Hello World”,1,(255,255,255))
  3. display it (screen.blit(text, (100,100)) put this into the while loop in which you show things

go and check the video for more details. I could suggest to make a function to make it easier.

import pygame
import random

# Initialize Pygame
pygame.init()

# Set the window size
window_size = (600, 400)
screen = pygame.display.set_mode(window_size)
clock = pygame.time.Clock()
def text(words, fnt="Arial", size=20, color=(255,255,255)):
	font = pygame.font.SysFont(fnt, size)
	txt = font.render(words, 1, color)
	return txt

# Set the title of the window
pygame.display.set_caption("Fraction Calculator")
running = True
helloworld = text("Hello World")

def update(speed):
	pygame.display.update()
	clock.tick(speed)

def show(writing, pos):
	screen.blit(writing, pos)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    show(helloworld, (100, 100))
    update(60)

Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts