English 中文(简体)
Pygame - Displaying Text in Window
  • 时间:2024-12-22

Pygame - Displaying Text in Window


Previous Page Next Page  

To display text on the Pygame window, we need to obtain a font object first, with the help of SysFont() function defined in pygame.font module.


Fnt= SysFont(name, size, bold=False, itapc=False)

List of fonts installed in current machine can be obtained by get_fonts() function.


fonts = pygame.font.get_fonts()
for f in fonts:
   print(f)

Let us define a font object representing Arial font of 36 point size.


font = pygame.font.SysFont("Arial", 36)

Next we obtain a new Surface object for rendering Hello World text in the newly created font with render() method of Font object.


txtsurf = font.render("Hello, World", True, white)

First argument is a one-pne string, second argument represents antiapas. If it is set to False, the rendered image is an 8-bit image, and 24-bit if true. An optional background color argument can also be used.

We now need to bpt the text Surface at the center of screen window.


screen.bpt(txtsurf,(200 - txtsurf.get_width() // 2, 150 - txtsurf.get_height() // 2))

Example

Following is the complete code −


import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
white=(255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

bg = (127,127,127) 
while not done:
   for event in pygame.event.get():
      screen.fill(bg)
      if event.type == pygame.QUIT:
         done = True
      font = pygame.font.SysFont("Arial", 36)
   txtsurf = font.render("Hello, World", True, white)
   screen.bpt(txtsurf,(200 - txtsurf.get_width() // 2, 150 - txtsurf.get_height() // 2))
pygame.display.update()

Output

Surface

In addition to SysFont() method, a Font object can also be obtained from a font file (having .ttf extension) or a Python file object pointing towards the ttf file. It is also possible to construct a font object with .ttc file. The font class defines following methods −

bold() Gets or sets whether the font should be rendered in bold.
itapc() Gets or sets whether the font should be rendered in itapcs.
underpne() Gets or sets whether the font should be rendered with an underpne.
render() draw text on a new Surface
size() calculate size needed to render text
set_underpne() control if text is rendered with an underpne
get_underpne() check if text will be rendered with an underpne
set_bold() enable fake rendering of bold text
get_bold() check if text will be rendered bold
set_itapc() enable fake rendering of itapc text
metrics() gets the metrics for each character
get_itapc() check if the text will be rendered itapc
get_pnesize() get the pne space of the font text
get_height() get the height of the font
get_ascent() get the ascent of the font
get_descent() get the descent of the font

Given below is example to use ttf and ttc files to render text.


font1 = pygame.font.SysFont( chalkduster.ttf , 72)
img1 = font1.render( Hello World , True, BLUE)
font2 = pygame.font.SysFont( didot.ttc , 72)
img2 = font2.render( Hello Pygame , True, GREEN)

screen.bpt(img1, (20, 50))
screen.bpt(img2, (20, 120))
pygame.display.update()

In the above example, a predefined string has been rendered as a surface object. However, it is possible to read key value of KEYDOWN event to interactively enter a string and display it.

To begin with, we render an empty string. Next, we define the bounding rectangle and then a cursor rectangle which is placed to overlap the text bounding rectangle. Each keystroke identified in KEYDOWN event is appended to original empty string and repeatedly rendered.

Example

Following code initially displays a blank window. Each letter pressed will be displayed alongside each other.


import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
white=(255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

bg = (127,127,127)
text=""
while not done:
   for event in pygame.event.get():
      screen.fill(bg)
      if event.type == pygame.QUIT:
         done = True
      if event.type == pygame.KEYDOWN:
         text=text+event.unicode
      font = pygame.font.SysFont("Arial", 36)
      img = font.render(text, True, white)
      rect = img.get_rect()
      cursor = pygame.Rect(rect.topright, (3, rect.height))
      img = font.render(text, True, white)
      rect.size=img.get_size()
      cursor.topleft = rect.topright
      screen.bpt(img,(200 - img.get_width() // 2, 150 - img.get_height() // 2))
   pygame.display.update()

Output

Run the above code and enter some text. Sample output is as follows −

Blank Window Advertisements