test.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import pygame, sys # 声明 导入需要的模块
  2. from pygame.locals import *
  3. pygame.init() # 初始化pygame
  4. DISPLAYSURF = pygame.display.set_mode((400, 300)) # 设置窗口的大小,单位为像素
  5. pygame.display.set_caption('Font') # 设置窗口的标题
  6. # 定义几个颜色
  7. WHITE = (255, 255, 255)
  8. GREEN = (0, 255, 0)
  9. BLUE = (0, 0, 128)
  10. fontObj = pygame.font.Font('ZKST.ttf', 48) # 通过字体文件获得字体对象
  11. textSurfaceObj = fontObj.render('Hello world!', True, (0,0,0)) # 配置要显示的文字
  12. textRectObj = textSurfaceObj.get_rect() # 获得要显示的对象的rect
  13. textRectObj.center = (200, 150) # 设置显示对象的坐标
  14. DISPLAYSURF.fill(WHITE) # 设置背景
  15. DISPLAYSURF.blit(textSurfaceObj, textRectObj) # 绘制字体
  16. while True: # 程序主循环
  17. for event in pygame.event.get(): # 获取事件
  18. if event.type == QUIT: # 判断事件是否为退出事件
  19. pygame.quit() # 退出pygame
  20. sys.exit() # 退出系统
  21. pygame.display.update() # 绘制屏幕内容