我不断收到 "text must be a unicode or bytes" 错误

I keep getting the "text must be a unicode or bytes" error

我一直收到这个错误,我不知道如何修复它,我可以 link link 中代码的所有文件,我会把代码放在描述中. Link - https://drive.google.com/drive/folders/1aIEG-TE2txwnEl93j82PdcG7d9aTCxVP?usp=sharing

# Importing all necessary scripts
import pygame
import time
import sys


# Initiating pygame
pygame.init()

# Creating colours for the game which will be needed
transparent = (0, 0, 0, 0)

# Creating the fonts for the game
Menufont = pygame.font.SysFont('Bahnschrift SemiBold', 70)
SmallMenufont = pygame.font.SysFont('Banschrift SemiBold', 20)

# Creating the window for where the game is able to be played
screen = pygame.display.set_mode((1280, 720))

# Getting the height and the width of the screen into a variable
width = screen.get_width()
height = screen.get_height()

time.sleep(0.1)

# Creating a Procedure for the Main Menu
def MainMenu():
    # Creating the main menu with a background image which has been modified to provide
    background = pygame.image.load('Main Menu Background.png')
    screen.blit(background, (0, 0))

    # Creating text for the buttons for the main menu of the game
    PlaySolo_text = Menufont.render("Play Solo", True, (3, 3, 116))
    PlayMulti_text = Menufont.render("Play Multi", True, (3, 3, 116))
    Leaders_text = Menufont.render("Leaders", True, (3, 3, 116))
    Credits_text = Menufont.render("Credits", True, (3, 3, 116))
    Options_text = Menufont.render("Options", True, (3, 3, 116))
    Exit_text = Menufont.render("Exit", True, (3, 3, 116))

    # Placing the text for the buttons onto the main menu for the game
    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))

    # Updating the menu for the game
    pygame.display.update()

    # This makes it so the cross in the corner is able to work
    Running = True
    while Running == True:
        # Provides information whilst the programme is running
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
                # This gets the x and y co-ordinates for where the mouse is
                mouse = pygame.mouse.get_pos()
                print(mouse)
                # If the user presses the Play Solo Button
                if mouse[0] > 221 and mouse[0] < 454 and mouse[1] > 199 and mouse[1] < 289:
                    print("Play Solo")
                # If the user presses the Play Multi Button
                elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 199 and mouse[1] < 289:
                    print("Play Multi")
                # If the user presses the Leaders Button
                elif mouse[0] > 221 and mouse[0] < 454 and mouse[1] > 338 and mouse[1] < 428:
                    print("Leaders")
                # If the user presses the Credits Button
                elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 338 and mouse[1] < 428:
                    # Makes the background invisible which allows another image to appear behind it
                    background.fill(transparent)
                    screen.blit(background, (0, 0))
                    pygame.display.update()
                    # This goes to the Credits Procedure
                    Credits()
                # If the user presses the Options button
                elif mouse[0] > 221 and mouse[0] < 451 and mouse[1] > 468 and mouse[1] < 561:
                    print("Options")
                # If the user presses the Exit Button
                elif mouse[0] > 832 and mouse[0] < 1067 and mouse[1] > 468 and mouse[1] < 561:
                    # Makes the application close
                    Running = False
            if event.type == pygame.QUIT:
                Running = False

def Credits():
    # Adding the Credits Background onto the screen
    background = pygame.image.load('Credits Background.png')
    screen.blit(background, (0, 0))
    # Opening the text file so that it can be put onto the screen
    CreditFile = open("Credits.txt", "r")
    CreditFileText = CreditFile.read()
    LineCount = len(open("Credits.txt").readlines())
    for i in range(1, LineCount):
        # Putting the text onto the screen so that it can be read
        Text = open("Credits.txt").readlines(i)
        CreditText = SmallMenufont.render(Text, True, (238, 238, 238))
        screen.blit(CreditText, (0, 0))
    # Updates the screen so everything will appear
    pygame.display.update()
    # This makes it so the cross in the corner is able to work
    Running = True
    while Running == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Running = False


MainMenu()

我试图做到这一点,以便制作人员名单文本文件中的每一行都在制作人员名单菜单中换行,但是制作人员名单菜单没有加载,并且出现了我无法理解的错误

您打开 Credits.txt 的方式并不正确。而不是下面的代码片段

# Opening the text file so that it can be put onto the screen
CreditFile = open("Credits.txt", "r")
CreditFileText = CreditFile.read()
LineCount = len(open("Credits.txt").readlines())
for i in range(1, LineCount):
    # Putting the text onto the screen so that it can be read
    Text = open("Credits.txt").readlines(i)
    CreditText = SmallMenufont.render(Text, True, (238, 238, 238))
    screen.blit(CreditText, (0, 0))

您可以简单地打开文件并逐行遍历它:

with open("Credits.txt", "r") as credits_file:
    for line in credits_file:
        # Do something with 'line' (your code is below)
        CreditText = SmallMenufont.render(line, True, (238, 238, 238))
        screen.blit(CreditText, (0, 0))