02-pygame显示文字

2018-07-27  本文已影响0人  阅心_5cc2
import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))

设置窗口背景颜色

    screen.fill((255,255,255))

1.创建字体对象
创建系统字体

    def SysFont(name, size, bold=False, italic=False, constructor=None):

    name -- 字体名
    size -- 字体大小
    bold -- 加粗
    italic -- 倾斜 
 font =  pygame.font.SysFont('microsoft Yahei',60)

创建自定义字体
Font(字体文件路径,字体大小)

 font = pygame.font.Font('./font/aa.ttf',30)

2.根据字体去创建显示对象(文字)()

  def render(self, text, antialias, color, background=None):
  text -- 要显示的文字内容(str)
  color -- 计算机三原色(红,绿,蓝),RGB颜色,值的范围都是0-255
  (255,0,0)--- 红色
  (0,255,0)--- 绿色
  (0,0,255)--- 蓝色
  """
  surface = font.render('hello python!',False,(255,200,10))
  print(type(surface))

3.将内容添加到窗口上(画到纸上)

    """
    blit(需要显示的对象,显示位置)
    需要显示的对象 ---  surface数据类型的数据
    显示位置  --- 坐标(x,y)
    """
    screen.blit(surface,(100,100))

4.将窗口上的内容展示出来(将画有文字的纸贴出来)

    pygame.display.flip()

    # 游戏循环
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
上一篇 下一篇

猜你喜欢

热点阅读