pygame字体

来源:互联网 发布:stc单片机是中国的吗 编辑:程序博客网 时间:2024/05/18 03:39
import pygame,sysfrom 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()
原创粉丝点击