-1

I keep getting this error and I do not know how to fix it, I can link all the files for the code within the link and I'll put the code in the description. 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()

I am trying to make it so that the each line within the text file of the credits goes to a new line within the credits menu, however the credits menu does not load and it gives the error which I cannot understand

IMC364
  • 27
  • 4
  • Can you please post the full error trace? It's hard to guess where the error comes from. – qouify Sep 21 '21 at 13:31
  • oh my bad, The error code is 'File "main.py", line 98, in Credits CreditText = SmallMenufont.render(Text, True, (238, 238, 238)) TypeError: text must be a unicode or bytes' – IMC364 Sep 21 '21 at 13:33
  • `Text = open("Credits.txt").readlines(i)` returns a list. Why do you open the file so many times? Open it once and iterate through the lines. [How to read a file line-by-line into a list?](https://stackoverflow.com/a/3277516) – 001 Sep 21 '21 at 13:33
  • Ohhh, my bad. Is there a way for the credits.txt file to be able to be printed in pygame in separate lines – IMC364 Sep 21 '21 at 13:36
  • You don't need to count the number of lines. Just do: `for Text in open("Credits.txt").readlines():` and remove the `Text = open("Credits.txt").readlines(i)` statement in the loop. – qouify Sep 21 '21 at 13:36

1 Answers1

0

The way how you open the Credits.txt is not really correct. Instead of your code fragment below

# 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))

you can simply open the file and iterate over it line by line:

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))
Rafael-WO
  • 1,434
  • 14
  • 24
  • @IMC364 your welcome :) If it solved your problem, be sure to accept the answer, so that it is clear that your question is solved. – Rafael-WO Sep 21 '21 at 13:56