欢迎来到pygame大讲堂,这次我们来讲字体:
pygame提供了绘制文本的函数,以至于你不需要用几十次draw.line
(),我们可以看一下以下实例:
import pygame,sys
from pygame.locals import *
pygame.init()
Surface=pygame.display.set_mode((400,300))
pygame.display.set_caption('fonttext')
White=(255,255,255)
Green=(0,255,0)
Blue=(0,0,128)
fonttext=pygame.font.Font('freesansbold.ttf',24)
text=fonttext.render('Hello World!',True,Blue,Green)
textrect=text.get_rect()
textrect.center=(250,200)
While True:
Surface.fill(White)
Surface.blit(text,textrect)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
那我们来概括一下如何讲文本绘制到屏幕上:
-
创建pygame.font.Font对象
-
创建一个Surace对象
-
调用render()来将文本绘制到Surface对象上
-
调用get_rect()来创建一个rect对象。
<