Pygame – Working with Text
In this article, we will see how to play with texts using the Pygame module. We will be dealing here with initializing the font, rendering the text, editing the text using the keyboard, and adding a blinking cursor note.
Installation
To install this module type the below command in the terminal.
pip install pygame
Font Initialization
Now we can proceed further with the font initialization part. The pygame.font.init() method is used to initialize the font and the pygame.font.get_init() method is used to check whether the font has been initialized or not. Both of the methods do not require any argument. If the font has been initialized successfully then pygame.font.get_init() method returns true.

Printing Text on Window
Here we will see how to get customized font and text on the screen. We will set the position of the texts’ to be displayed on the screen using x and y coordinates. First, we will create the font files and then render the texts. The screen. blit() function is used for copying the text surface objects to the display surface objects at the center coordinates.
Python3
# import pygameimport pygame# initializing pygamepygame.font.init()# check whether font is initialized# or notpygame.font.get_init()# create the display surfacedisplay_surface = pygame.display.set_mode((500, 500))# change the window screen titlepygame.display.set_caption('Our Text')# Create a font file by passing font file# and size of the fontfont1 = pygame.font.SysFont('freesanbold.ttf', 50)font2 = pygame.font.SysFont('chalkduster.ttf', 40)# Render the texts that you want to displaytext1 = font1.render('GeeksForGeeks', True, (0, 255, 0))text2 = font2.render('GeeksForGeeks', True, (0, 255, 0))# create a rectangular object for the# text surface objecttextRect1 = text1.get_rect()textRect2 = text2.get_rect()# setting center for the first texttextRect1.center = (250, 250)# setting center for the second texttextRect2.center = (250, 300)while True: # add background color using RGB values display_surface.fill((255, 0, 0)) # copying the text surface objects # to the display surface objects # at the center coordinate. display_surface.blit(text1, textRect1) display_surface.blit(text2, textRect2) # iterate over the list of Event objects # that was returned by pygame.event.get() # method. for event in pygame.event.get(): if event.type == pygame.QUIT: # deactivating the pygame library pygame.quit() # quitting the program. quit() # update the display pygame.display.update() |
Output:
Cursor Input on Window
We are going to add Blinking Cursor Note here. Our cursor will continue blinking after every 0.5 sec. We can also edit our text.
Python3
# import pygame moduleimport pygame# import time moduleimport time# initialize the pygame modulepygame.init()# set the window screen sizedisplay_screen = pygame.display.set_mode((500, 500))# add some texttext = 'Hello Guys!!'# add default font style with font# sizefont = pygame.font.SysFont(None, 40)# render the textimg = font.render(text, True, (255, 0, 0))rect = img.get_rect()rect.topleft = (20, 20)cursor = pygame.Rect(rect.topright, (3, rect.height))running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # detect if key is physically # pressed down if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: if len(text) > 0: # stores the text except last # character text = text[:-1] else: text += event.unicode img = font.render(text, True, (255, 0, 0)) rect.size = img.get_size() cursor.topleft = rect.topright # Add background color to the window screen display_screen.fill((200, 255, 200)) display_screen.blit(img, rect) # cursor is made to blink after every 0.5 sec if time.time() % 1 > 0.5: pygame.draw.rect(display_screen, (255, 0, 0), cursor) # update display pygame.display.update()pygame.quit() |
Output:

Input Box on Window
Here we will see how to read the text using the keyboard in pygame. We are going to show our text inside a rectangle. When the mouse is taken over the rectangle the color of the rectangle gets changed. Comments have been added to the code for clear understanding.
Python3
# import pygame moduleimport pygame# import sys libraryimport sys# initializing pygamepygame.init()clock = pygame.time.Clock()# Set the window screen sizedisplay_screen = pygame.display.set_mode((500, 500))# add font style and sizebase_font = pygame.font.Font(None, 40)# stores text taken by keyboarduser_text = ''# set left, top, width, height in# Pygame.Rect()input_rect = pygame.Rect(200, 200, 140, 32)color_active = pygame.Color("lightskyblue")color_passive = pygame.Color("gray15")color = color_passiveactive = Falsewhile True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # when mouse collides with the rectangle # make active as true if event.type == pygame.MOUSEBUTTONDOWN: if input_rect.collidepoint(event.pos): active = True # if the key is physically pressed down if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: # stores text except last letter user_text = user_text[0:-1] else: user_text += event.unicode display_screen.fill((0, 0, 0)) if active: color = color_active else: color = color_passive pygame.draw.rect(display_screen, color, input_rect) # render the text text_surface = base_font.render(user_text, True, (255, 255, 255)) display_screen.blit(text_surface, (input_rect.x + 5, input_rect.y + 5)) input_rect.w = max(100, text_surface.get_width() + 10) pygame.display.flip() clock.tick(60) |
Output:







Please Login to comment...