@Maxime Lorant的回答就是你想要的。假设你有一张名单,上面写着你的名字。在names = ["Name1", "Name2", "Name3"]
f = open("names.txt", "a")
for i in names:
f.write(i + "\n")
f.close()
如果名称.txt是一个空白文件,现在它的内容应该是:
^{pr2}$
基本上,换行符在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)
#...
结果如下: