The Wayback Machine - https://web.archive.org/web/20240823132629/https://www.geeksforgeeks.org/python-display-text-to-pygame-window/
Open In App

Python | Display text to PyGame window

Last Updated : 09 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 up to the imagination or necessity of the developer, what type of game he/she wants 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 font.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 the display.update() method of pygame.

Below is the implementation:
 

Python3




# 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 surface 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()


Output : 
 

Output - 1

Now We Will See one of the applications of Displaying the texts but in a different way that is by scrolling the text in 6 different ways on the pygame window.

1. Scrolling the text on top of the Screen.

2. Scrolling the text at the bottom of the screen.

3. Scrolling the text on the left side of the Screen

4. Scrolling the text on the right side of the Screen

5. Scrolling the text in diagonal  from left to right side of the Screen

6. Scrolling the text in diagonal from right side to left side of the Screen.

After Seeing the below Code you can implement your own pattern 

Below is the Implementation

Python




# import pygame module in this program
import pygame
 
# activate the pygame library
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
 
# create the display surface object
# (x, y) is the height and width of pygame window
win=pygame.display.set_mode((500, 500))
 
# set the pygame window name
pygame.display.set_caption("Scrolling Text")
 
# setting the pygame font style(1st parameter)
# and size of font(2nd parameter)
Font=pygame.font.SysFont('timesnewroman',  30)
 
# define the RGB value for white,
# green, yellow, orange colour
white=(255, 255, 255)
yellow=(255, 255, 0)
green=(0, 255, 255)
orange=(255, 100, 0)
done=False
 
# Split the text into letters
# 3rd parameter is font colour and
# 4th parameter is Font background
letter1=Font.render("H", False, orange, yellow)
letter2=Font.render("E", False, orange, green)
letter3=Font.render("M", False, orange, yellow)
letter4=Font.render("A", False, orange, green)
letter5=Font.render("N", False, orange, yellow)
letter6=Font.render("T", False, orange, green)
letter7=Font.render("H", False, orange, yellow)
 
# assigning values to
# i and c variable
i=0
c=1
 
# infinite loop
while not done:
    if(i>=820):
        i=0
        c+=1
        pygame.time.wait(500)
         
    # completely fill the surface object
    # with white color
    win.fill(white)
    if(c%6==0):   
        # Scrolling the text in diagonal
        # on right side of the Screen.
        # copying the text surface object
        # to the display surface object 
        # at the center coordinate.
        win.blit(letter1, (662-i, -162+i))
        win.blit(letter2, (639-i, -139+i))
        win.blit(letter3, (608-i, -108+i))
        win.blit(letter4, (579-i, -79+i))
        win.blit(letter5, (552-i, -52+i))
        win.blit(letter6, (529-i, -29+i))
        win.blit(letter7, (500 -i, 0 + i))
        i+=80
    if(c%6==5):
        # Scrolling the text in diagonal on
        # left side of the Screen.
        win.blit(letter1, (-162+i, -162+i))
        win.blit(letter2, (-135+i, -135+i))
        win.blit(letter3, (-110+i, -110+i))
        win.blit(letter4, (-79+i, -79+i))
        win.blit(letter5, (-52+i, -52+i))
        win.blit(letter6, (-27+i, -27+i))
        win.blit(letter7, (0+i, 0+i))
         
        # Decides the speed of
        # the text on screen
        i+=80   
    if(c%6==4):
       
        # Scrolling the text in
        # right side of the Screen.
        win.blit(letter1, (480, -180+i))
        win.blit(letter2, (480, -150+i))
        win.blit(letter3, (480, -120+i))
        win.blit(letter4, (480, -90+i))
        win.blit(letter5, (480, -60+i))
        win.blit(letter6, (480, -30+i))
        win.blit(letter7, (480, 0+i))
         
        # Decides the speed of
        # the text on screen
        i +=80 
    if(c%6==3): 
        # Scrolling the text in left
        # side of the Screen.
        win.blit(letter1, (0, -180+i))
        win.blit(letter2, (0, -150+i))
        win.blit(letter3, (0, -120+i))
        win.blit(letter4, (0, -90+i))
        win.blit(letter5, (0, -60+i))
        win.blit(letter6, (0, -30+i))
        win.blit(letter7, (0, 0+i))
         
        # Decides the speed of
        # the text on screen
        i+=80  
    if(c%6==1):
 
        win.blit(letter1, (-124+i, 0))
        win.blit(letter2, (-102+i, 0))
        win.blit(letter3, (-82+i, 0))
        win.blit(letter4, (-58+i, 0))
        win.blit(letter5, (-40+i, 0))
        win.blit(letter6, (-19+i, 0))
        win.blit(letter7, (0+i, 0))
         
        # Decides the speed of
        # the text on screen
        i +=80   
    if(c%6==2):
       
       # Scrolling the text in bottom of the Screen.
        win.blit(letter1, (-124+i, 470))
        win.blit(letter2, (-102+i, 470))
        win.blit(letter3, (-82+i, 470))
        win.blit(letter4, (-58+i, 470))
        win.blit(letter5, (-40+i, 470))
        win.blit(letter6, (-19+i, 470))
        win.blit(letter7, (0+i, 470))
         
        # Decides the speed
        # of the text on screen
        i+=80
     
    # Draws the surface object to the screen.
    pygame.display.update()
     
    # 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):
            done=True
    #Delay with 5ms
    pygame.time.wait(500)
pygame.quit()


Output:

1. When text is Scrolling on top of Screen

2. When text is Scrolling on bottom of Screen

3. When text is Scrolling on left side of Screen

4. When text is Scrolling on right side of Screen

5. When text is Scrolling on diagonal from left side of Screen

6. When text is Scrolling on diagonal from right side of Screen



Previous Article
Next Article

Similar Reads

Adding Collisions Using pygame.Rect.colliderect in Pygame
Prerequisite: Drawing shapes in Pygame, Introduction to pygame In this article, we are going to use pygame.Rect.colliderect for adding collision in a shape using Pygame in Python. We can easily add collisions in Pygame shapes using the colliderect( ). For this, we are going to draw two rectangles then we will check if the rectangles are colliding o
3 min read
Python | Display images with PyGame
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 up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip in
2 min read
Snowfall display using Pygame in Python
Not everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming. Before starting the topic, it is highly recommended revising the basics of Pygame. Steps for snowfall creation 1. Importing modules First, we need to import the Pygame modu
3 min read
Python | Drawing different shapes on 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 up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip instal
3 min read
How to create an empty PyGame window?
Pygame window is a simple window like any other window, in which we display our game screen. It is the first task we do so that we can display our output onto something. Our main goal here is to create a window and keep it running unless the user wants to quit. To perform these tasks first we need to install pygame package and import some pre-defin
2 min read
Allowing resizing window in PyGame
In this article, we will learn How to allow resizing a PyGame Window. Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the
2 min read
How to get the size of PyGame Window?
In this article, we will learn How to get the size of a PyGame Window. Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the
2 min read
How to make a PyGame Window?
PyGame is a free and open-source cross-platform library for game development in Python. It was officially written by Pete Shinners to replace PySDL it is suitable for the creation of client-side applications and acts as standalone executables. In this article, we are going to see how to make Pygame window in Python. Command to install pygame on Win
2 min read
How to Change the Name of a Pygame window?
PyGame window is a simple window that displays our game on the window screen. By default, pygame uses "Pygame window" as its title and pygame icon as its logo for pygame window. We can use set_caption() function to change the name and set_icon() to set icon of our window. To change the name of pygame window: Syntax: pygame.display.set_caption('Titl
2 min read
How to make a fully transparent window with PyGame ?
Fully transparent windows with PyGame have their own advantage while building many games and apps via Python. Thus in order to achieve the functionality of the transparent window that allows the user to see behind a running screen. For example: Geek want to design an app, in which they can see the background screen of the other windows. Thus in ord
3 min read
How to create a text input box with Pygame?
In this article, we will discuss how to create a text input box using PyGame. Installation Before initializing pygame library we need to install it. This library can be installed into the system by using pip tool that is provided by Python for its library installation. Pygame can be installed by writing these lines into the terminal. We can install
3 min read
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 pygameFont Initialization Now we can proc
5 min read
How to Display Multiple Images in One Window using OpenCV Python?
Prerequisites: Opencv In this article, we will show how to display Multiple Images In One window using OpenCV in Python. Approach Import moduleLoad the Multiple images using cv2.imread()Concatenate the images using concatenate(), with axis value provided as per orientation requirementDisplay all the images using cv2.imshow()Wait for keyboard button
1 min read
PYGLET – Getting Window Display
In this article we will see how we can get the window display in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire
2 min read
PyQtGraph – Getting Window Flags of Plot Window
In this article we will see how we can get window flags of the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) an
3 min read
PyQtGraph – Setting Window Flag to Plot Window
In this article we will see how we can set window flag to the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and
3 min read
How to display text on boxplot in Python?
Boxplot is an important graphical plot that can be used to get a summary of data present in numerical form. The plot can give us information about statistical measures such as percentile, median, minimum and maximum values of the numerical data. In the box plot, the line which passes through the center of the box represents the median value. In thi
3 min read
Python Arcade - Display Text
In this article, we will learn How we can add text to arcade games in Python. Adding Simple Text We can add text in the arcade using the draw_text() function. Syntax: arcade.draw_text(text, x, y, color, size, width, align, font_name) Parameters: text: Text we want to displayx : x coordinatey : y coordinatecolor : color of the textsize : Size of the
3 min read
Python Tkinter – How to display a table editor in a text widget?
In this article, we will discuss how to display a table editor in a text widget. Before moving ahead let's create a simple text widget first with the help of Tkinter. For the link to the Excel file used in the code click here. Text Widget The Text widget is used to show the text editor in the Tkinter window. A user can modify the text in the text e
3 min read
Python | Making an object jump in PyGame
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 up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. So, in this article, we will learn how
3 min read
Python | Playing audio file in Pygame
Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the best modules for doing so. Note: For more information, refer to Introdu
2 min read
Python - Moving an object in PyGame
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 up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this tutorial, we are going to lear
3 min read
Slide Puzzle using PyGame - Python
Slide Puzzle game is a 2-dimensional game i.e the pieces can only be removed inside the grid and reconfigured by sliding them into an empty spot. The slide puzzle game developed here is a 3X3 grid game i.e 9 cells will be there from 1 to 8(9 is not here since a blank cell is needed for sliding the neighboring cells). In this article, we are going t
15 min read
Python - Drawing design using arrow keys in PyGame
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 up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this article we will see how we can
3 min read
Binary Search Visualization using Pygame in Python
An algorithm like Binary Search can be understood easily by visualizing. In this article, a program that visualizes the Binary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sorting algorithm, and fill the pygame window with ba
4 min read
Ternary Search Visualization using Pygame in Python
An algorithm like Ternary Search can be understood easily by visualizing. In this article, a program that visualizes the Ternary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sorting algorithm, and fill the pygame window with
5 min read
Difference between Pygame VS Arcade Library in Python
Game programming is very rewarding nowadays and it can also be used in advertising or as a teaching tool. Game development encompasses mathematics, logic, physics, AI, and much more and it can be amazingly fun. In Python, up until now, Pygame library was employed for the same, but there is a new module: Arcade Library. Here we will discuss how they
3 min read
Building Space Invaders Using PyGame - Python
In this article, we're going to build a Space Bullet shooter game using PyGame in Python. The used file can be downloaded from here. Approach: Import the required module.Initialize the pygame.Create three functions:isCollision(): Which tells us whether the collision has occurred or not?game_over(): Which returns True or False on the basis of which
13 min read
How to create MS Paint clone with Python and PyGame?
In this article, we will create a simple MS paint program with Python and PyGame. MS Paint is a simple program made by Microsoft, it allows users to create basic art and painting. Since its inception, MS Paint has been included with every version of Microsoft Windows. MS Paint provides features for drawing and painting in color and tools to create
9 min read
Differences Between Pyglet and Pygame in Python
In this article, we will see the difference between Pygame and Pyglet gaming libraries. What is Pyglet? Pyglet is a cross-platform window and media library for Python, intended for developing games and other visually rich applications. It supports windows, UI event handling, gamepads, OpenGL graphics, image and video loading, and sound and music pl
4 min read
three90RightbarBannerImg