Pygame运行报'text must be a unicode or bytes'错误如何修复
Pygame菜单"text must be a unicode or bytes"错误修复方案
错误原因
- 核心错误在
Credits()函数的文本渲染逻辑:readlines()方法返回的是由所有行组成的列表,而pygame.font.render()要求第一个输入参数为字符串类型,传入列表直接触发类型不匹配报错 - 附带问题:
- 重复打开读取
Credits.txt文件,逻辑冗余效率低 - 所有行渲染坐标都固定为
(0,0),文本会完全重叠无法正常查看 - 小字体名称拼写错误:
Banschrift应为Bahnschrift,可能导致字体加载异常
- 重复打开读取
修复后完整代码
# 导入依赖 import pygame import time import sys # 初始化pygame pygame.init() # 定义所需颜色 transparent = (0, 0, 0, 0) # 定义游戏字体,修正了小字的字体拼写错误 Menufont = pygame.font.SysFont('Bahnschrift SemiBold', 70) SmallMenufont = pygame.font.SysFont('Bahnschrift SemiBold', 20) # 创建游戏窗口 screen = pygame.display.set_mode((1280, 720)) # 获取窗口宽高 width = screen.get_width() height = screen.get_height() time.sleep(0.1) # 主菜单逻辑 def MainMenu(): # 加载主菜单背景 background = pygame.image.load('Main Menu Background.png') screen.blit(background, (0, 0)) # 渲染主菜单按钮文本 PlaySolo_text = Menufont.render("单人游戏", True, (3, 3, 116)) PlayMulti_text = Menufont.render("多人游戏", True, (3, 3, 116)) Leaders_text = Menufont.render("排行榜", True, (3, 3, 116)) Credits_text = Menufont.render("制作人员", True, (3, 3, 116)) Options_text = Menufont.render("设置", True, (3, 3, 116)) Exit_text = Menufont.render("退出", True, (3, 3, 116)) # 放置按钮文本 screen.blit(PlaySolo_text, (229, 221)) screen.blit(PlayMulti_text, (833, 221)) screen.blit(Leaders_text, (243, 359)) screen.blit(Credits_text, (863, 359)) screen.blit(Options_text, (241, 492)) screen.blit(Exit_text, (906, 492)) # 更新界面 pygame.display.update() # 主菜单事件循环 Running = True while Running == True: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: # 获取鼠标点击坐标 mouse = pygame.mouse.get_pos() print(mouse) # 点击单人游戏按钮 if mouse[0] > 221 and mouse[0] < 454 and mouse[1] > 199 and mouse[1] < 289: print("Play Solo") # 点击多人游戏按钮 elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 199 and mouse[1] < 289: print("Play Multi") # 点击排行榜按钮 elif mouse[0] > 221 and mouse[0] < 454 and mouse[1] > 338 and mouse[1] < 428: print("Leaders") # 点击制作人员按钮 elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 338 and mouse[1] < 428: background.fill(transparent) screen.blit(background, (0, 0)) pygame.display.update() # 跳转制作人员界面 Credits() # 点击设置按钮 elif mouse[0] > 221 and mouse[0] < 451 and mouse[1] > 468 and mouse[1] < 561: print("Options") # 点击退出按钮 elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 468 and mouse[1] < 561: Running = False if event.type == pygame.QUIT: Running = False def Credits(): # 加载制作人员界面背景 background = pygame.image.load('Credits Background.png') screen.blit(background, (0, 0)) # 单次读取credits文件所有行,自动关闭文件 with open("Credits.txt", "r", encoding="utf-8") as f: # 过滤空行,去除每行首尾的换行符和空白 credit_lines = [line.strip() for line in f.readlines() if line.strip()] # 逐行渲染文本,纵向偏移避免重叠 y_pos = 50 # 初始纵向位置,可自行调整 for line in credit_lines: CreditText = SmallMenufont.render(line, True, (238, 238, 238)) screen.blit(CreditText, (100, y_pos)) # 左侧留100像素边距,可自行调整 y_pos += 25 # 行间距25像素,可自行调整 # 更新界面 pygame.display.update() # 制作人员界面事件循环 Running = True while Running == True: for event in pygame.event.get(): if event.type == pygame.QUIT: Running = False MainMenu()
内容的提问来源于stack exchange,提问作者IMC364
相关产品推荐
相关产品推荐

