1

I was wondering if it is possible to display text from a txt file onto a pygame screen. I am working on a game, and I am attempting to display the instructions from a text file in my game.

Here is what I did below:

def instructions():
    instructText = instructionsFont.render(gameInstructions.txt, True, WHITE)
    screen.blit(instructText, ((400 - (instructText.get_width()/2)),(300 - (instructText.get_height()/2))))

However I get the error:

line 356, in instructions
    instructText = instructionsFont.render(pongInstructions.txt, True, WHITE)
NameError: name 'pongInstructions' is not defined

My attempt however, is all trial and error because I am actually unsure of how to do this... Any help is greatly appreciated!

Student
  • 39
  • 1
  • 11

1 Answers1

0

gameinstructions is not defined since python thinks it is a variable.

To tell python it is a string you need to put it in quotes:

instructText = instructionsFont.render("gameInstructions.txt", True, WHITE)

However that is probably not what you want. What you want to do is read the file. For that you should use the with statement to safely open and close the file:

with open("gameInstructions.txt") as f:
    instructText = instructionsFont.render(f.read(), True, WHITE)

I can not currently try the code out, but you might need to loop through the lines instead, if pygame can not handle several lines of text at once:

with open("gameInstructions.txt") as f:
    for line in f:
        instructText = instructionsFont.render(line, True, WHITE)
Azsgy
  • 3,139
  • 2
  • 29
  • 40
  • I tried this code, however I get an error saying: line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 397: ordinal not in range(128) – Student May 29 '17 at 23:25
  • what version of python are you on? It seems to expect ascii, which the "â" character is not part of. From other posts I can see that it should support unicode https://stackoverflow.com/questions/668359/unicode-fonts-in-pygame#668596 – Azsgy May 29 '17 at 23:31
  • I'm running this in python 3.6.0 – Student May 29 '17 at 23:37
  • This is often caused by a wrong locale. Try forcing the encoding of your file with `open(filename, encoding='utf-8')` or checking if your locale is set properly – Azsgy May 29 '17 at 23:46
  • another related question: https://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte#21190382 – Azsgy May 29 '17 at 23:49
  • Thank you for helping out so much, however after forcing the encoding- nothing happens at all. My program freezes. At his point my code is `def instructions(): screen.fill(TEAL) with open("pongInstructions.txt", encoding = 'utf-8') as f: for line in f: instructText = instructionsFont.render(line, True, WHITE) screen.blit(instructText, ((400 - (instructText.get_width()/2)),(300 - (instructText.get_height()/2)))) pygame.display.update()` Any other suggestions? – Student May 30 '17 at 02:02
  • I don't know, I am not very familiar with pygame. Since you solved the original issue (instructions not defined), I recommend you move on and start a new question. – Azsgy May 30 '17 at 11:29
  • Alright, thank you so so much for all of your help and time! – Student May 30 '17 at 11:55