2017-04-02 76 views
1

我正在在pygame的游戏我的姐姐在pygame的练习,我想从一个文本文件中的窗口名称添加一个随机行:试图从一个随机行从一个文本文件中添加文本

进口pygame的,随机的,SYS

RandomMess = open('GameText.txt') 
pygame.display.set_caption('Dream Land: {}'). 
#The .txt file currently has 4 lines but will be a lot more in the future... 
#These are the test lines: This is in beta, hi :), hello, hi 

我在任何情况下的代码的所有的休息说,我没有做过任何其他代码,以使窗口或任何东西。 我想说:Dream Land:{}然后在大括号中从文件中添加一个随机行。

回答

1

您正在寻找readlines()和random.choice()。

import random,sys 

RandomMess = open('GameText.txt') 

# .readlines() will create a list of the lines in the file. 
# So in our case ['This is in beta', 'hi :)', 'hello', 'hi'] 
# random.choice() then picks one item from this list. 
line = random.choice(RandomMess.readlines()) 

# Then use .format() to add the line into the caption 
pygame.display.set_caption('Dream Land: {}'.format(line)) 
相关问题