Python | Display text to PyGame window

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s upto the imagination or necessity of developer, what type of game he/she want to develop using this toolkit.

Command to install pygame on windows based system :

pip install pygame

 
There are 7-basic steps to displaying Text on the pygame window :

  • Create a display surface object using display.set_mode() method of pygame.
  • Create a Font object using fony.Font() method of pygame.
  • Create a Text surface object i.e.surface object in which Text is drawn on it, using render() method of pygame font object.
  • Create a rectangular object for the text surface object using get_rect() method of pygame text surface object.
  • Set the position of the Rectangular object by setting the value of the center property of pygame rectangular object.
  • Copying the Text surface object to the display surface object using blit() method of pygame display surface object.
  • Show the display surface object on the pygame window using display.update() method of pygame.

Below is the implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pygame module in this program
import pygame
  
# activate the pygame library
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
  
# define the RGB value for white,
#  green, blue colour .
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
  
# assigning values to X and Y variable
X = 400
Y = 400
  
# create the display surface object
# of specific dimension..e(X, Y).
display_surface = pygame.display.set_mode((X, Y ))
  
# set the pygame window name
pygame.display.set_caption('Show Text')
  
# create a font object.
# 1st parameter is the font file
# which is present in pygame.
# 2nd parameter is size of the font
font = pygame.font.Font('freesansbold.ttf', 32)
  
# create a text suface object,
# on which text is drawn on it.
text = font.render('GeeksForGeeks', True, green, blue)
  
# create a rectangular object for the
# text surface object
textRect = text.get_rect() 
  
# set the center of the rectangular object.
textRect.center = (X // 2, Y // 2)
  
# infinite loop
while True :
  
    # completely fill the surface object
    # with white color
    display_surface.fill(white)
  
    # copying the text surface object
    # to the display surface object 
    # at the center coordinate.
    display_surface.blit(text, textRect)
  
    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get() :
  
        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT :
  
            # deactivates the pygame library
            pygame.quit()
  
            # quit the program.
            quit()
  
        # Draws the surface object to the screen.  
        pygame.display.update() 

chevron_right


Output :
Output - 1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.