0

I am working on a scoreboard function for my game in which the player inputs there name and then that is written to a text file as the scores. I have managed write to the file and display it again. But it only writes one name which currently gets overwritten every time a new name is entered. So how would I fix this? I have tried doing:

f = open('names.txt')
f.writeline(str(name))
f.close()

Here is what I am trying to get:

Name1
Name2
Name3

So how would I do a different line for each name? Thank you

I can add in multiple names but here is what happens: enter image description here The text file appears correctly but in pygame it appears incorrectly. I want it to be on separate lines like the text file.

GhostFrag1
  • 987
  • 2
  • 12
  • 19
  • Are you trying to append information to a file? Check out this post: http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python – Bobort Dec 27 '13 at 15:12

2 Answers2

2

@Maxime Lorant answer is what you want. Suppose you have a list with your names.

names = ["Name1", "Name2", "Name3"]
f = open("names.txt", "a")
for i in names:
    f.write(i + "\n")
f.close()

If names.txt was a blank file, now the content of it should like:

Name1
Name2
Name3

EDIT

Now I see what you are trying to achieve. Please see this link : http://sivasantosh.wordpress.com/2012/07/18/displaying-text-in-pygame/

Basically, the newline character won't work in pygame - you have to change the coordinates of your text rectangle. I am kinda new to pygame but I managed to do what you want and here's my naive approach (I edited code from the link above):

#...
f = open("t.txt")
lines = f.readlines()
f.close()

basicfont = pygame.font.SysFont(None, 48)
text = basicfont.render('Hello World!', True, (255, 0, 0), (255, 255, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery

screen.fill((255, 255, 255))
for i in lines:
    # each i has a newline character, so by i[:-1] we will get rid of it
    text = basicfont.render(i[:-1], True, (255, 0, 0), (255, 255, 255))
    # by changing the y coordinate each i from lines will appear just
    # below the previous i
    textrect.centery += 50
    screen.blit(text, textrect)
#...

Here's the result:

macfij
  • 3,093
  • 1
  • 19
  • 24
  • I will try this and see if this makes a difference. – GhostFrag1 Dec 27 '13 at 18:32
  • Mind if I show you my current code to see if you could help me fit this in? – GhostFrag1 Dec 28 '13 at 12:15
  • Sure. I think I would handle it:) – macfij Dec 28 '13 at 12:22
  • The code: https://gist.github.com/GhostFrag1/8ea8e610e3af9e4aff9e as you can ee if you run this you can input your name then that is saved to a text file. When it is closed the reopened the name will be in the top left. – GhostFrag1 Dec 28 '13 at 13:24
  • check it: https://gist.github.com/macfij/8162008 if you have further questions, feel free to ask me. – macfij Dec 28 '13 at 17:45
  • Thank you going to try that out now :) – GhostFrag1 Dec 28 '13 at 20:43
  • Please let me know whether this was useful or not. – macfij Dec 29 '13 at 16:39
  • One more question. Is there any way you could help me add in ints to the text. As I wish to add this into a game I am working on and the player has a score. So for example it then looks like NAME 50. As when I tried a test so SCORE = 50 then did str(name) + SCORE + "\n" I got an error about mixing int and str – GhostFrag1 Dec 29 '13 at 19:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44116/discussion-between-macfij-and-ghostfrag1) – macfij Dec 29 '13 at 21:33
  • [`pygame.Font.render`](https://pyga.me/docs/ref/font.html#pygame.font.Font.render) in `pygame-ce` since `2.1.4` supports newlines, more info about the fork can be found [here](https://www.reddit.com/r/pygame/comments/1112q10/pygame_community_edition_announcement/) – Matiiss Mar 07 '23 at 21:44
1

As in many programming language, you can open the file in append mode, to write at the end of the current file. This can be achieve by adding a second parameter in open which is the mode:

f = open('names.txt', 'a')
f.write(str(name) + '\n')
f.close()
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97