import pygame,sys
from pygame.locals import *
pygame.init()
DISPLAYSURF=pygame.display.set_mode((400,300))
pygame.display.set_caption("Hello World")
WHITE=(255,255,255)
GREEN=(0,255,0)
BLUE=(0,0,128)
fontObj=pygame.font.Font("freesansbold.ttf",50)
#创建一个Font对象
#freesansbold是Python自带的一种字体
textSurfaceObj=fontObj.render("Hello world!",True,GREEN,BLUE)
#render表达
#fontObj.render(字符串,True或False(指定是否要抗锯齿),字体颜色,[背景底色])的返回一个Surface对象
#背景颜色(可选)如果想要一个透明的背景,就直接省略这个参数
textRectObj=textSurfaceObj.get_rect()
#Surface对象的get_rect()方法,从Surface对象创建一个Rect对象
#这个Rect对象可以计算出文本的正确坐标
textRectObj.center=(200,150)
#将Rect对象的中心设置为(200,250)
while True:
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(textSurfaceObj,textRectObj)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
pygame字体
最新推荐文章于 2025-06-12 20:28:25 发布