Table of contents
1.
Introduction
2.
Displaying text in pygame window
2.1.
Steps
2.2.
Implementation of code
3.
Frequently Asked Questions
3.1.
How would you show text in Python?
3.2.
How does pygame show work?
3.3.
For what reason do we want pygame show update?
3.4.
What does pygame show update do?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Displaying text in pygame window

Author Adya Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pygame is a cross-stage set of Python modules intended for composing computer games. It incorporates PC illustrations and sound libraries designed to be utilized with the Python programming language. In pygame, text can't be written straightforwardly on the screen. The initial step is to make a Font object with a given text dimension. When the textual style is made, its size can't be changed.

Displaying text in pygame window

The initial step is to make a Font object with a given text dimension. The subsequent advance is to deliver the text into a picture with a given tone. The third step is to blit the picture on the screen. These are the means:

font = pygame.font.SysFont(None, 24)
img = font.render('hello', True, BLUE)
screen.blit(img, (20, 20))
You can also try this code with Online Python Compiler
Run Code

Steps

There are 7-fundamental stages to showing Text on the pygame window :

  • Make a showcase surface article utilizing display.set_mode() strategy for pygame.
  • Make a Font object utilizing font.Font() strategy for pygame.
  • Make a Text surface item, i.e.surface object in which Text is drawn on it, utilizing render() strategy for pygame textual style object.
  • Make a rectangular item for the text surface article utilizing get_rect() strategy for the pygame text surface article.
  • Set the place of the Rectangular article by setting the worth of the middle property of the pygame rectangular item.
  • Replicating the Text surface item to the presentation surface article utilizing blit() technique for pygame show surface item.
  • Show the presentation surface item on the pygame window utilizing the display.update() technique for pygame.

Different steps need to be taken, like initializing the font and rendering the text, editing text with a keyboard, and adding a blinker which we will see further in detail using Python.

Implementation of 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 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()
You can also try this code with Online Python Compiler
Run Code

Instating the text style can require a couple of moments on a MacBook Air, the making of a framework Font object.

The capacity get_fonts() returns a rundown of all introduced text styles.

The textual style article can deliver a given text into a picture.

We then make two additional text styles, Chalkduster and Didot, at a size of 72 places.

The console occasion can be utilized to alter a text. First, we make a text that we save in a string variable text and render to a picture.

Then we characterize the jumping square shape and a cursor square shape compared to the text bouncing square shape.

Inside the occasion circle, we lookout for KEYDOWN occasions. Assuming the keypress is a BACKSPACE, and the length of the string is more significant than 0, then, at that point, we eliminate the last person else, and we attach the new person to the text variable.

Then, at that point, we render the altered text, update the jumping square shape, and spot the cursor box toward the finish of the refreshed bouncing square shape:

To make the cursor more noticeable, we let it flicker every 0.5 seconds. We do this utilizing the time.time() drifting point esteem.

Presently We Will See one of the uses of Displaying the messages, however diversely, by looking over the message in 6 distinct ways on the pygame window.

1. Looking over the Text on top of the Screen.

2. Looking over the Text at the lower part of the Screen.

3. Looking over the Text on the left half of the Screen

4. Looking over the Text on the right half of the Screen

5. Looking over the Text in inclining from left to right half of the Screen

6. Looking over the Text from the corner to corner from the right side to the left half of the Screen.

This is what the result will resemble:

Frequently Asked Questions

How would you show text in Python?

In python, the print proclamation is utilized to show text. In python, with the print explanation, you can use Single Quotes(') or Double Quotes(").

How does pygame show work?

The showcase is made utilizing . set_mode(), which returns a Surface addressing the noticeable piece of the window.

For what reason do we want pygame show update?

update() to make the presentation Surface show up on the client's screen.

What does pygame show update do?

It permits just a piece of the screen to refresh, rather than the whole region. Assuming no contention is passed, it restores the entire Surface region like pygame.

Conclusion

In pygame, text can't be composed straightforwardly on the screen. The initial step is to make a Font object with a given text dimension. When the textual style is made, its size can't be changed. In python, the print proclamation is utilized to show text. In python, with the print explanation, you can use Single Quotes(') or Double Quotes(").

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Check out this problem - Smallest Distinct Window .

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