2017-04-15 11 views
-3

私は現在、ゲームでどれくらいうまくやっているのかに基づいてユーザーがスコアを設定したコードを書いています。誰かが新しいハイスコアを設定して最高の3つを印刷し、他のものを削除してスペースを節約するために、ユーザーのコンピュータにローカルファイルを作成して取得する方法についていくつかの提案をしてもらえますか?ローカルハイスコアを作成するにはどうすればよいですか? - Python

これは現時点で私が持っているコードですが、それは改善の深刻な必要としている:あなたは、コードの用量はまだ何も格納しない見ることができるように

print("Yore score is", score, "/30") 

if sthighscore < score: 
    sthighscore = score 
    sthighscorename = name 
    print("NEW 1st HIGH SCORE: ") 
    print('1: ',sthighscorename, '-', sthighscore) 
    print("2: ", ndhighscorename, '-', ndhighscore) 
    print('3: ', rdhighscorename, '-', rdhighscore) 

elif sthighscore >= score > ndhighscore: 
    ndhighscore = score 
    ndhighscorename = name 
    print("NEW 2nd HIGH SCORE: ") 
    print('1: ', sthighscorename, '-', sthighscore) 
    print("2: ", ndhighscorename, '-', ndhighscore) 
    print('3: ', rdhighscorename, '-', rdhighscore) 

elif ndhighscore >= score > rdhighscore: 
    rdhighscore = score 
    rdhighscorename = name 
    print("NEW 3rd HIGH SCORE: ") 
    print('1: ',sthighscorename, '-', sthighscore) 
    print("2: ", ndhighscorename, '-', ndhighscore) 
    print('3: ', rdhighscorename, '-', rdhighscore) 

else: 
    print("NO NEW HIGH SCORES :(") 
    print('1: ',sthighscorename, '-', sthighscore) 
    print("2: ", ndhighscorename, '-', ndhighscore) 
    print('3: ', rdhighscorename, '-', rdhighscore) 

。助けてください。

+0

Pythonでファイル処理に関するいくつかの調査をしましたか? – jonrsharpe

+0

私はあなたが調査することができ、研究を試みたが、私の質問で何も見つけることができなかったかもしれないので、まだこれに新しいです、私は人々が私に何かを教えることができることを願ってここにそれを求めました。 –

+1

これはチュートリアルサイトではなく、あなたの質問はここで間違っています。 [公式チュートリアル](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)を含む、ファイル処理に関するオンラインチュートリアルがたくさんあります。また、[Python Crash Course](https://www.nostarch.com/pythoncrashcourse/)には、ゲームのハイスコアを保存するセクション(第14章)があります。 –

答えて

0

ローカルファイルを作成するには、open()関数を使用します。このようにスコアを格納できます。

print("Yore score is", score, "/30") 
if sthighscore < score: 
    sthighscore = score 
    sthighscorename = name 
    print("NEW 1st HIGH SCORE: ") 
    print('1: ',sthighscorename, '-', sthighscore) 
    print("2: ", ndhighscorename, '-', ndhighscore) 
    print('3: ', rdhighscorename, '-', rdhighscore) 
    f=open('score.txt','w') # This will create score.txt text file 
    f.write('%s\n%s\n%s'%('1: '+sthighscorename+ '-'+ str(sthighscore),"2: "+ ndhighscorename+ '-'+ str(ndhighscore), '3: '+ rdhighscorename+ '-'+ str(rdhighscore))) 
    f.close() 

他の値も同じ手順で保存できます。 検索するには、

f=open('score.txt','r') 
data=f.read().split('\n') 
print data 
sthighscore=int(data[0].split()[1].split('-')[1]) 
sthighscorename=data[0].split()[1].split('-')[0] 
ndhighscore=int(data[1].split()[1].split('-')[1]) 
ndhighscorename=data[1].split()[1].split('-')[0] 
rdhighscore=int(data[2].split()[1].split('-')[1]) 
rdhighscorename=data[2].split()[1].split('-')[0] 
+0

更新:このコードにはエラーがあります –

+0

f = open( 'score.txt'、 'r') data = f.read()。split( '\ n') print(data) sthighscorename = data [0] .split()[1] .split( ' - ')[0] sthighscorename = data [0] .split()[1] .split( ' - ')[0] ndhighscore = int(データ[1] .split()[1] .split( ' - ')[0] ndhighscorename = data [1] .split()[1] .split ] rdhighscorename = data [2] .split()[1] .split( ' - ')[1] .split( ' - ')[ 0] –

+0

エラーは次のとおりです。list index of range –

関連する問題