04.字体

import pygame
import sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('fonttext')

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 128)

# (字体文件的一个字符串,字体大小),'freesansbold.ttf'(这是Pygame自带的一种字体)
fontObj = pygame.font.Font('freesansbold.ttf', 32)
# 文本绘制(文本,抗锯齿,颜色,背景色),抗锯齿:通过给文本和图形的边缘添加一些模糊效果,使其看上去不那么块状化
textSurfaceObj = fontObj.render('Hello world', True, GREEN, BLUE)
# 通过修改Rect对象的属性之一,来设置其位置
textRectObj = textSurfaceObj.get_rect()
# 将Rect对象的中心设置为200, 150
textRectObj.center = (200, 150)

while 1:
    DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.blit(textSurfaceObj, textRectObj)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

抗锯齿:

猜你喜欢

转载自www.cnblogs.com/fly-book/p/11757465.html