Table of contents
1.
Introduction
2.
Working with text
3.
Example:
4.
Editing the text using KeyBoard
5.
Frequently Asked Questions
5.1.
What is Pygame?
5.2.
How to write text in Pygame?
5.3.
How to change the size of the font in Pygame?
5.4.
What is the best text editor for Pygame Programming?
5.5.
How to create a rectangle object for text input in Pygame?
6.
Conclusion
Last Updated: Mar 27, 2024

Let us Deal with the text in pygame

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this current blog, we will learn how the text in Pygame and how to deal with it to make our applications much more effective. We will try to start with a brief introduction to working with text in Pygame. Then, we will learn about the various attributes, so let's dive straight into the blog.

We know that the text is a string object. This cannot be written directly in Pygame applications. Then how to do it? So, let’s try to learn this together in this blog.

Working with text

We need to follow around seven steps to get the text displayed in our Pygame application. They are as follows:

Step1: Create the display surface using the display.set_mode().

Step2: We need to create the Font object using the font.Font().

Step3: We need to create the text object on which text to be placed. This can be done by using the render() method on the font object.

Step4: As usual, we need to create a rectangle object from the text object using the get_rect() method.

Step5: We need to use the blit() method to copy the text surface on the display surface.

Step6: It is optional to arrange the rectangle's position on the display surface.

Step7: At last, we need to display the display surface object on the pygame application using the display.update() method.

Example:

import pygame  #importing the pygame library

pygame.init()     #intialising the pygame modules.

disp_surface = pygame.display.set_mode((400, 400))

font = pygame.font.Font('chalkduster.ttf', 32)  #first attribute is the font file, and the #second is the font size.

text_obj = font.render('Sample Text', True)  #creating a text object

rect_obj = text.get_rect() #creating the rectangle object

rect_obj.center = (X // 2, Y // 2)  #positioning the rectangle object

while True:

        disp_surface.blit(text_obj, rect_obj)
        
        pygame.display.update()   #displaying the display surface.
You can also try this code with Online Python Compiler
Run Code

Output: We will get “Sample Text “at the center of the application.

Editing the text using KeyBoard

To edit the text using KeyBoard will be simple in Pygame. First, we need to create a text variable that is used to store a string value. Then, we need to render this text object to an image using the render() method.

After that, we need to create a rectangle object by using the get_rect() method. And then, we can set the position of this created rectangle object on the display surface. Then, we can use event.get() method to control the keyboard input. Example:

if event.type == KEYDOWN:
    if event.key == K_BACKSPACE:
        if len(text)>0:
            text = text[:-1]
    else:
        text += event.unicode
You can also try this code with Online Python Compiler
Run Code

And then, we need to render the text again. And Finally, we need to update the display using the display.update() method in Pygame.

In this way, we can work with text in Pygame. There are many more functionalities that we can play with text in Pygame. We will explore them in the later blogs.

Frequently Asked Questions

What is Pygame?

Pygame is a python programming language library that allows developers to develop applications on gaming themes. It is a well-developed library with many valuable functions to implement several functionalities of our application.

How to write text in Pygame?

We can not be able to write text on Pygame directly, and we need to create particular valuable objects in the Pygame application before writing the text on the screen. The valuable objects are text objects, display objects, rectangle objects, etc.

How to change the size of the font in Pygame?

In Pygame applications, once the text or font is created, then the size of the font cannot be changed. It should always be taken care of at the start of the initialization.

What is the best text editor for Pygame Programming?

There are a lot of text editors that support scripting the pygame programs. One of the editors that ease the pygame application building process is EzText Editor. This will effortlessly allow text inputs to our Pygame application.

How to create a rectangle object for text input in Pygame?

In Pygame, once the text object is generated, we will use the Font object in order to create a rectangle object in order to display text on it. We will use the get_rect() method in Pygame to do so.

Conclusion

In this current article, we have extensively discussed the text in Pygame. We started with a brief introduction to working with text in Pygame, then discussed editing the text using Keyboard. Coding Ninjas has you covered. To learn, see Operating SystemUnix File SystemFile System Routing, and File Input/Output.

Refer to our most valuable Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsJavaScriptSystem DesignCompetitive Programming, and many more! If you want to test your own competency in the coding domain, you may check to participate in the contests and mock test series hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass