2017-04-02 7 views
1

私はパイゲームで練習するためにパイゲームでゲームを作っていますが、テキストファイルのランダムな行をウィンドウ名に追加しようとしています:テキストファイルのランダム行からテキストを追加しようとしています

私はケースの誰にコードのすべての残りの部分を持っている

インポート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 

は、私は窓か何かを作るために他のコードを実行していないことを言います。 私はそれを言いたい:ドリームランド:{}その後、中括弧でファイルからランダムな行を追加します。

答えて

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)) 
関連する問題