我正在pygame中为我的妹妹制作游戏,以便在pygame中练习,我正在尝试从文本文件中添加一个随机行到窗口名称:
导入pygame,random,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:{}然后在大括号中添加一个随机行。
答案 0 :(得分: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))