@Maxime Lorant 的答案就是你想要的。假设您有一个包含您的姓名的列表。
names = ["Name1", "Name2", "Name3"]
f = open("names.txt", "a")
for i in names:
f.write(i + "\n")
f.close()
如果 names.txt 是一个空白文件,现在它的内容应该是:
Name1
Name2
Name3
编辑
现在我看到了你想要达到的目标。请看这个链接:
http://sivasantosh.wordpress.com/2012/07/18/displaying-text-in-pygame/
基本上,换行符在 pygame 中不起作用 - 您必须更改文本矩形的坐标。我对 pygame 有点陌生,但我设法做你想做的事,这是我天真的方法(我从上面的链接编辑了代码):
#...
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)
#...
结果如下: