Pygame 简明教程

Pygame - Displaying Text in Window

要在 Pygame 窗口中显示文本,我们需要先获取一个字体对象,借助 pygame.font 模块中定义的 SysFont() 函数。

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, italic=False)

可以使用 get_fonts() 函数获取当前计算机中安装的字体列表。

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)

我们定义一个表示 36 磅大小 Arial 字体的字体对象。

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

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

接下来,我们通过使用 Font 对象的 render() 方法,为新创建的字体中的“Hello World”文本获取一个新的 Surface 对象。

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)

第一个参数是一个单行字符串,第二个参数表示抗锯齿。如果将其设置为 False,渲染的图像将是一个 8 位图像,如果为 true,则为 24 位。还可以使用可选的背景颜色参数。

First argument is a one-line string, second argument represents antialias. 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.

现在我们需要将文本 Surface 模糊地放置在屏幕窗口的中心。

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

screen.blit(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.blit(txtsurf,(200 - txtsurf.get_width() // 2, 150 - txtsurf.get_height() // 2))
pygame.display.update()

Output

surface

除了 SysFont() 方法,还可以在从字体文件(具有 .ttf 扩展名)或指向 ttf 文件的 Python 文件对象获取 Font 对象。还可以使用 .ttc 文件构建字体对象。font 类定义了以下方法 −

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.

italic()

Gets or sets whether the font should be rendered in italics.

underline()

Gets or sets whether the font should be rendered with an underline.

render()

draw text on a new Surface

size()

calculate size needed to render text

set_underline()

control if text is rendered with an underline

get_underline()

check if text will be rendered with an underline

set_bold()

enable fake rendering of bold text

get_bold()

check if text will be rendered bold

set_italic()

enable fake rendering of italic text

metrics()

gets the metrics for each character

get_italic()

check if the text will be rendered italic

get_linesize()

get the line 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

下面是使用 ttf 和 ttc 文件来渲染文本的示例。

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.blit(img1, (20, 50))
screen.blit(img2, (20, 120))
pygame.display.update()

在上面的示例中,一个预定义的字符串已经被渲染为一个表面对象。不过,可以读取 KEYDOWN 事件的键值来交互式地输入一个字符串并显示它。

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.

首先,我们渲染一个空字符串。接下来,我们定义边界矩形,然后定义光标矩形,该矩形被放置在文本边界矩形之上。KEYDOWN 事件中标识的每个击键都会附加到原始空字符串并重复渲染。

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.blit(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