Python - Display text to PyGame window

Pygame is a multimedia library for Python for making games and multimedia applications. In this article we will see how to use the pygame module to get customized font and text on the screen taking into consideration, its height, width, and position in the pygame window.

In the below program we initialize the pygame module and then define the mode and the caption for the image. Next we add the font text and define the coordinates for the font. The screen.blit function paints the screen while the while loop keeps listening the end of the game is clicked.


import pygame
import sys
# Initialize pygame
#scren dimension
# Screen caption
pygame.display.set_caption("Text in Pygame")
# Render the objects
text_obj=font_obj.render("Welcome to Pygame",True,font_color)
while True:
   for eve in pygame.event.get():
      if eve.type==pygame.QUIT:


Running the above code gives us the following result −