详解Pygame 在窗口中显示文本

  • Post category:Python

Pygame是一款Python编程语言的游戏开发库,能够很好的处理音效、图形等大部分与游戏制作相关的方面。在Pygame中,要在窗口中显示文本,可以使用内置的font模块。

使用方法

要使用Pygame的font模块,需要先导入相关的类和函数。在程序开始处,可以这样写:

import pygame
from pygame.locals import *
pygame.init()

接着,在设置窗口的时候,需要额外设置一个字体:

font = pygame.font.SysFont('Arial', 30)

这里,我们指定了字体的样式和大小,使用的是系统自带的Arial字体,大小为30。如果需要使用其它字体,需要先安装到系统中。

之后,我们可以通过font.render()函数来创建一个显示文本的Surface对象:

text_surface = font.render('Hello, Pygame!', True, (255, 255, 255))

这里,第一个参数是要显示的文本内容,第二个参数指定是否启用抗锯齿(True指启用,False指禁用),第三个参数是文字颜色(这里是白色)。

最后,我们需要将这个Surface对象绘制到窗口上:

DISPLAYSURF.blit(text_surface, (100, 100))

这里,DISPLAYSURF表示窗口对象,第二个参数是一个元组,表示Surface对象的坐标。这样,就可以在窗口上显示文本了。

示例一

下面是一个简单的示例程序,它会在窗口中央显示一个“Hello, Pygame!”的文本。代码如下:

import pygame
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Pygame Text Demo')

font = pygame.font.SysFont('Arial', 30)

text_surface = font.render('Hello, Pygame!', True, (255, 255, 255))
text_rect = text_surface.get_rect(center=(200, 150))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    DISPLAYSURF.fill((0, 0, 0))
    DISPLAYSURF.blit(text_surface, text_rect)

    pygame.display.update()

在代码中,我们使用get_rect()函数获取了文本所在的矩形区域,然后将这个矩形居中显示在窗口。

示例二

接下来,我们来做一个打字游戏的例子。游戏的规则是,每隔一段时间会在窗口上显示一个随机的英文单词,玩家需要在限定时间内输入正确的单词。代码如下:

import pygame
import random
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 300))
pygame.display.set_caption('Typing Game')

font = pygame.font.SysFont('Arial', 24)
word_font = pygame.font.SysFont('Arial', 40)

WORD_LIST = ['python', 'pygame', 'hello', 'world', 'typing', 'game']
WORD_TIME = 2000
WORD_LIFE = 5

current_word = ''
word_timer = WORD_TIME
word_life = WORD_LIFE
score = 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN and current_word:
            if chr(event.key) == current_word[0]:
                current_word = current_word[1:]
                score += 1
                word_life = WORD_LIFE

    DISPLAYSURF.fill((0, 0, 0))

    word_timer -= pygame.time.get_ticks()
    if word_timer <= 0:
        current_word = random.choice(WORD_LIST)
        word_timer = WORD_TIME
        word_life = WORD_LIFE

    if word_life <= 0:
        current_word = ''
        word_life = WORD_LIFE

    if current_word:
        word_surface = word_font.render(current_word, True, (255, 255, 255))
        word_rect = word_surface.get_rect(center=(250, 150))
        DISPLAYSURF.blit(word_surface, word_rect)

    time_surface = font.render('Time: {}'.format(int(word_timer / 1000)), True, (255, 255, 255))
    score_surface = font.render('Score: {}'.format(score), True, (255, 255, 255))
    life_surface = font.render('Life: {}'.format(word_life), True, (255, 255, 255))

    time_rect = time_surface.get_rect(bottomleft=(10, 290))
    score_rect = score_surface.get_rect(bottomleft=(110, 290))
    life_rect = life_surface.get_rect(bottomleft=(220, 290))

    DISPLAYSURF.blit(time_surface, time_rect)
    DISPLAYSURF.blit(score_surface, score_rect)
    DISPLAYSURF.blit(life_surface, life_rect)

    pygame.display.update()

在游戏中,我们使用了两个不同字体大小的字体。在每个循环中,我们减少计时器的值,然后在计时器到达某一值时显示一个随机单词,限定时间内输入正确即可得到分数,否则生命值减少,生命值为0结束游戏。在游戏循环中,我们使用了很多render()函数和get_rect()函数来实现文本的绘制。