Pygame - 在視窗中顯示文字



要在 Pygame 視窗上顯示文字,我們首先需要獲取一個字型物件,這可以透過 pygame.font 模組中定義的 SysFont() 函式來實現。

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

可以使用 get_fonts() 函式獲取當前機器上安裝的字型列表。

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

讓我們定義一個表示 36 號 Arial 字型的字型物件。

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

接下來,我們使用 Font 物件的 render() 方法,使用新建立的字型渲染 "Hello World" 文字,獲取一個新的 Surface 物件。

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

第一個引數是一個單行字串,第二個引數表示抗鋸齒。如果設定為 False,則渲染的影像是 8 點陣圖像,如果為 True,則為 24 點陣圖像。還可以使用可選的背景顏色引數。

現在我們需要將文字 Surface 繪製到螢幕視窗的中心。

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

示例

以下是完整的程式碼:

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

輸出

Surface

除了 SysFont() 方法外,還可以從字型檔案(具有 .ttf 副檔名)或指向 ttf 檔案的 Python 檔案物件獲取 Font 物件。也可以使用 .ttc 檔案構造字型物件。font 類定義了以下方法:

bold() 獲取或設定字型是否應以粗體渲染。
italic() 獲取或設定字型是否應以斜體渲染。
underline() 獲取或設定字型是否應帶下劃線渲染。
render() 在新的 Surface 上繪製文字
size() 計算渲染文字所需的尺寸
set_underline() 控制文字是否帶下劃線渲染
get_underline() 檢查文字是否將帶下劃線渲染
set_bold() 啟用粗體文字的偽渲染
get_bold() 檢查文字是否將以粗體渲染
set_italic() 啟用斜體文字的偽渲染
metrics() 獲取每個字元的度量
get_italic() 檢查文字是否將以斜體渲染
get_linesize() 獲取字型的行距
get_height() 獲取字型的 高度
get_ascent() 獲取字型的 上升高度
get_descent() 獲取字型的 下降高度

下面是使用 ttf 和 ttc 檔案渲染文字的示例。

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

在上面的示例中,預定義的字串已被渲染為 Surface 物件。但是,可以讀取 KEYDOWN 事件的鍵值以互動式地輸入字串並顯示它。

首先,我們渲染一個空字串。接下來,我們定義邊界矩形,然後定義一個游標矩形,該矩形放置在與文字邊界矩形重疊的位置。KEYDOWN 事件中識別的每個按鍵都附加到原始空字串中並重復渲染。

示例

以下程式碼最初顯示一個空白視窗。按下每個字母都將彼此並排顯示。

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

輸出

執行以上程式碼並輸入一些文字。示例輸出如下:

Blank Window
廣告