代码运行抛出异常

代码运行抛出异常


import pygame, sys

pygame.init()

screen = pygame.display.set_mode((500, 500))

# 加载字体
"""
['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', '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', 'bssym7', 'hakuyoxingshu7000',
'refspcl', 'ygyxsziti2', '04b21', 'arialms', 'bookantiqua', 'centurygothic', 'haettenschweiler', 'tempussansitc', 'pristina',
'papyrus', 'mistral', 'lucidahandwriting', 'kristenitc', 'juiceitc', 'frenchscript', 'freestylescript', 'bradleyhanditc', 'garamond',
 'monotypecorsiva', 'baskervilleoldface', 'bell', 'berlinsansfb', 'bernardcondensed', 'bodonipostercompressed', 'britannic',
 'broadway', 'brushscript', 'californianfb', 'centaur', 'chiller', 'colonna', 'cooperblack', 'footlight', 'harrington',
 'hightowertext', 'jokerman', 'kunstlerscript', 'lucidabright', 'lucidacalligraphy', 'lucidafaxregular', 'magneto',
  'maturascriptcapitals', 'modernno20', 'niagaraengraved', 'niagarasolid', 'oldenglishtext', 'onyx', 'parchment', 'playbill',
   'poorrichard', 'ravie', 'informalroman', 'showcardgothic', 'snapitc', 'stencil', 'vinerhanditc', 'vladimirscript', 'widelatin',
   'twcen', 'twcencondensed', 'script', 'rockwellextra', 'rockwellcondensed', 'rockwell', 'rage', 'perpetuatitling', 'perpetua',
    'palacescript', 'ocraextended', 'maiandragd', 'lucidasanstypewriterregular', 'lucidasansregular', 'imprintshadow', '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', 'bookmanoldstyle', 'bodonicondensed', 'bodoniblack', 'bodoni', 'blackadderitc', 'arialrounded', 'agencyfb', 'berlinsansfbdemi', 'lucidafax', 'twcencondensedextra', 'lucidasanstypewriter', 'lucidasanstypewriteroblique', 'lucidasansroman', 'fzshuti', 'fzyaoti', 'lisu', 'stcaiyun', 'stfangsong', 'sthupo', 'stkaiti', 'stliti', 'stsong', 'stxihei', 'stxingkai', 'stxinwei', 'stzhongsong', 'youyuan', '方正兰亭超细黑简体', 'hpsimplified', 'algerian', 'bauhaus93', 'harlowsolid', 'vivaldi', 'euclid', 'euclidsymbol', 'euclidextra', 'euclidfraktur', 'euclidmathone', 'euclidmathtwo', 'century', 'gadugi', 'leelawadee', 'microsoftjhengheimicrosoftjhengheiui', 'microsoftuighur', 'microsoftyaheimicrosoftyaheiui', 'nirmalaui', 'segoeuisemilight', 'wingdings2', 'wingdings3', 'microsoftjhengheimicrosoftjhengheiuibold', 'microsoftyaheimicrosoftyaheiuibold', 'kingsoftphonetic', 'icomoon', 'bookshelfsymbol7', '等线', 'dubai', 'dubaimedium', 'dubairegular', 'extra', 'msoutlook', 'msreferencesansserif', 'msreferencespecialty', 'acaderef', 'aigdt', 'amdtsymbols', 'geniso', 'amgdt', 'bankgothic', 'bankgothicmedium', 'cityblueprint', 'commercialpi', 'commercialscript', 'countryblueprint', 'dutch801roman', 'dutch801', 'dutch801extra', 'euroroman', 'euroromanoblique', 'monospace821', 'panroman', 'romantic', 'romans', 'sansserif', 'sansserifboldoblique', 'sansserifoblique', 'stylus', 'superfrench', 'swiss721', 'swiss721outline', 'swiss721condensed', 'swiss721condensedoutline', 'swiss721blackcondensed', 'swiss721extended', 'swiss721blackextended', 'swiss721black', 'swiss721blackoutline', 'technicbold', 'techniclite', 'technic', 'universalmath1', 'vineta', 'isocpeur', 'isocteur', 'proxy9', 'proxy8', 'proxy7', 'proxy6', 'proxy5', 'proxy4', 'proxy3', 'symusic', 'symeteo', 'symath', 'symap', 'syastro', 'romant', 'romand', 'romanc', 'italict', 'greeks', 'greekc', 'gothicg', 'gothice', 'txt', 'simplex', 'scripts', 'scriptc', 'proxy2', 'proxy1', 'monotxt', 'italicc', '', 'isoct3', 'isoct2', 'isoct', 'isocp3', 'isocp2', 'isocp', 'gothici', 'gdt', 'complex', 'teamviewer14', '方正粗黑宋简体', 'numberonly']
"""
fonts = pygame.font.get_fonts()
# print(fonts)

red = pygame.Color(255, 0, 0)

# 加粗、斜体
font = pygame.font.SysFont('方正兰亭超细黑简体', 40, True, True)
# 文字对象
text = font.render('得分', True, red)

while True:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           pygame.quit()
           sys.exit()

   screen.blit(text, (20, 20))
   pygame.display.flip()


# 代码运行抛出异常(SyntaxError: Non-UTF-8 code starting with '\xbc')

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好。同学在文件头添加 “# coding=utf-8”,就可以正常运行了。如果设置的中文字体不能正常显示,同学可更换其他字体进行测试。

如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~

  • 侯周武 提问者 #1
    添加“# coding=utf-8"后可以正常运行,请问下添加的这条语句是什么意思,原理是什么?
    2019-06-13 15:44:20
  • 好帮手慕美 回复 提问者 侯周武 #2
    # coding=utf-8 设置编码格式为utf-8,使得注释中可以写中文。在解析时避免出现不必要的错误。
    2019-06-13 19:58:45
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
1.Python零基础入门
  • 参与学习           人
  • 提交作业       2727    份
  • 解答问题       8160    个

想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师