Python pygame添加文字窗口的方法

 时间:2026-02-14 09:42:16

1、首先,要定义模块pygame,并初始化pygame.display:

import  pygame

pygame.display.init()

Python pygame添加文字窗口的方法

2、然后,设置pygame窗口大小:

win = pygame.display.set_mode([500,500])

Python pygame添加文字窗口的方法

3、然后,定义函数:

def  drawText(content):

    pygame.font.init()

    font  =  pygame.font.Font("msyhbd.ttf",58)

pygame.font.Font(字体,大小)

Python pygame添加文字窗口的方法

4、然后,返回text_sf参数

  text_sf  =  font.render(content,True,pygame.Color(0,255,0),pygame.Color(0,0,130))

font.render参数意义:.render(内容,是否抗锯齿,字体颜色,字体背景颜色)

Python pygame添加文字窗口的方法

5、然后,调用函数drawText。

win.blit(drawText("    Hi !   龙市唐川   "),(5,  190))

pygame.display.update()

并更新显示

Python pygame添加文字窗口的方法

6、最后,运行程序:

import  pygame

pygame.display.init()

win = pygame.display.set_mode([500,500])

win.fill(pygame.Color(255,255,255))

def  drawText(content):

    pygame.font.init()

    font  =  pygame.font.Font("msyhbd.ttf",58)

    text_sf  =  font.render(content,True,pygame.Color(0,255,0),pygame.Color(0,0,130))

    return  text_sf 

win.blit(drawText("    Hi !   龙市唐川   "),(5,  190))

pygame.display.update()

结果如下图。

Python pygame添加文字窗口的方法

  • 如何在vs中修改QT窗口图标和名称?
  • 如何在HTML中插入图片
  • entry怎么获得输入的值
  • VisualStudio如何配置符号选项
  • java中怎样获取静态变量的值
  • 热门搜索
    最漂亮的手抄报花边 小学四年级英语手抄报 关于童话故事的手抄报 敬廉崇洁手抄报资料 安全手抄报的内容 生活处处有语文手抄报 法制手抄报资料 关于运动会的手抄报 感恩的心手抄报内容 八十天环游地球手抄报