pygame字体
import pygame pygame.init() SCREEN_SIZE = (960, 600) screen = pygame.display.set_mode(SCREEN_SIZE,0) pygame.display.set_caption("字体") f=pygame.font.get_fonts() #获取当前系统所有的可用字体--列表 #['arial', 'batangbatangchegungsuhgungsuhche', 'couriernew', 'daunpenh', 'dokchampa', 'estrangeloedessa', 'euphemia', 'gautami', 'vani', 'gulimgulimchedotumdotumche', 'impact', 'iskoolapota', 'kalinga', 'kartika', 'khmerui', 'laoui', 'latha', 'lucidaconsole', 'malgungothic', 'mangal', 'meiryomeiryomeiryouimeiryouiitalic', 'meiryomeiryoboldmeiryouiboldmeiryouibolditalic', 'microsofthimalaya', 'microsoftjhenghei', 'microsoftyahei', 'mingliupmingliumingliuhkscs', 'mingliuextbpmingliuextbmingliuhkscsextb', 'mongolianbaiti', 'msgothicmspgothicmsuigothic', 'msminchomspmincho', 'mvboli', 'microsoftnewtailue', 'nyala', 'microsoftphagspa', 'plantagenetcherokee', 'raavi', 'segoescript', 'segoeui', 'segoeuisemibold', 'segoeuisymbol', 'shruti', 'simsunnsimsun', 'simsunextb', 'sylfaen', 'microsofttaile', 'timesnewroman', 'tunga', 'vrinda', 'shonarbangla', 'microsoftyibaiti', 'tahoma', 'microsoftsansserif', 'angsananew', 'aparajita', 'cordianew', 'ebrima', 'gisha', 'kokila', 'leelawadee', 'microsoftuighur', 'moolboran', 'symbol', 'utsaah', 'vijaya', 'wingdings', 'andalus', 'arabictypesetting', 'simplifiedarabic', 'simplifiedarabicfixed', 'sakkalmajalla', 'traditionalarabic', 'aharoni', 'david', 'frankruehl', 'levenim', 'miriam', 'miriamfixed', 'narkisim', 'rod', 'fangsong', 'simhei', 'kaiti', 'angsanaupc', 'browallianew', 'browalliaupc', 'cordiaupc', 'dilleniaupc', 'eucrosiaupc', 'freesiaupc', 'irisupc', 'jasmineupc', 'kodchiangupc', 'lilyupc', 'dfkaisb', 'lucidasans', 'arialblack', 'calibri', 'cambriacambriamath', 'cambria', 'candara', 'comicsansms', 'consolas', 'constantia', 'corbel', 'franklingothicmedium', 'gabriola', 'georgia', 'palatinolinotype', 'segoeprint', 'trebuchetms', 'verdana', 'webdings', 'hpsimplified', 'bookantiqua', 'bookmanoldstyle', 'bradleyhanditc', 'bookshelfsymbol7', 'century', 'freestylescript', 'frenchscript', 'gadugi', 'garamond', 'centurygothic', 'kristenitc', 'juiceitc', 'lucidahandwriting', 'mistral', 'microsoftjhengheimicrosoftjhengheiui', 'microsoftjhengheimicrosoftjhengheiuibold', '微软雅黑microsoftyaheiui', '微软雅黑microsoftyaheiuibold', 'monotypecorsiva', 'papyrus', 'pristina', 'msreferencesansserif', 'msreferencespecialty', 'segoeuisemilight', 'tempussansitc', 'wingdings2', 'wingdings3', '等线', '方正舒体', '方正姚体', '隶书', '幼圆', '华文彩云', '华文仿宋', '华文琥珀', '华文楷体', '华文隶书', '华文宋体', '华文细黑', '华文行楷', '华文新魏', '华文中宋', 'fzshuti', 'fzyaoti', 'lisu', 'stcaiyun', 'stfangsong', 'sthupo', 'stkaiti', 'stliti', 'stsong', 'stxihei', 'stxingkai', 'stxinwei', 'stzhongsong', 'youyuan', 'extra', 'arialms', 'microsoftyaheimicrosoftyaheiui', 'msoutlook', 'algerian', 'baskervilleoldface', 'bauhaus93', 'bell', 'berlinsansfb', 'bernardcondensed', 'bodonipostercompressed', 'britannic', 'broadway', 'brushscript', 'californianfb', 'centaur', 'chiller', 'colonna', 'cooperblack', 'footlight', 'harlowsolid', 'harrington', 'hightowertext', 'jokerman', 'kunstlerscript', 'lucidabright', 'lucidacalligraphy', 'lucidafaxregular', 'magneto', 'maturascriptcapitals', 'modernno20', 'niagaraengraved', 'niagarasolid', 'oldenglishtext', 'onyx', 'parchment', 'playbill', 'poorrichard', 'ravie', 'informalroman', 'showcardgothic', 'snapitc', 'stencil', 'vinerhanditc', 'vivaldi', 'vladimirscript', 'widelatin', 'twcen', 'twcencondensed', 'script', 'rockwellextra', 'rockwellcondensed', 'rockwell', 'rage', 'perpetuatitling', 'perpetua', 'palacescript', 'ocraextended', 'maiandragd', 'lucidasanstypewriterregular', 'lucidasansregular', 'imprintshadow', 'haettenschweiler', 'goudystout', 'goudyoldstyle', 'gloucesterextracondensed', 'gillsansultracondensed', 'gillsansultra', 'gillsanscondensed', 'gillsans', 'gillsansextcondensed', 'gigi', 'franklingothicmediumcond', 'franklingothicheavy', 'franklingothicdemicond', 'franklingothicdemi', 'franklingothicbook', 'forte', 'felixtitling', 'erasmediumitc', 'erasitc', 'erasdemiitc', 'engravers', 'elephant', 'edwardianscriptitc', 'curlz', 'copperplategothic', 'centuryschoolbook', 'castellar', 'calisto', 'bodonicondensed', 'bodoniblack', 'bodoni', 'blackadderitc', 'arialrounded', 'agencyfb', 'microsoftyaheimicrosoftyaheiuibold', 'berlinsansfbdemi', 'lucidafax', 'twcencondensedextra', 'lucidasanstypewriter', 'lucidasanstypewriteroblique', 'lucidasansroman', 'nirmalaui', 'segoeuiemoji', 'icomoon', '方正小标宋简体', '方正粗黑宋简体', 'numberonly'] print(f) my_font = pygame.font.SysFont("arial", 16) #创建一个Font对象(系统自带) #第一个参数是字体名,第二个自然就是大小 #my_font1 = pygame.font.Font("my_font.ttf", 16) #创建一个ttf的Font对象 #这个语句使用了一个叫做“my_font.ttf”,这个方法之所以好是因为你可以把字体文件随游戏文件一起分发,避免用户机器上没有需要的字体 background =pygame.image.load('2.jpg') text_surface = my_font.render("wo is liming", True, (0, 0, 0), (255, 255, 255)) #写字 #第一个参数是写的文字 #第二个参数是个布尔值(代表是否开启抗锯齿) #第三个是字体颜色 #第四个是背景色,如果你不想有背景色(也就是透明),那么可以不加这第四个参数 pygame.image.save(text_surface, 'name.png') #把字体保存为图片
图片效果:
import pygame pygame.init() SCREEN_SIZE = (960, 600) screen = pygame.display.set_mode(SCREEN_SIZE,0) pygame.display.set_caption("字体") my_font = pygame.font.SysFont('SimHei',64) #创建一个Font对象(系统自带) background =pygame.image.load('2.jpg') text_surface = my_font.render("我是李明", True, (0, 0, 0), (255, 255, 255)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() screen.blit(background,(0,0)) screen.blit(text_surface, (100, 50)) #把字体放到窗口 #参数2:位置 pygame.display.update()