2017-01-13 9 views
0

私はコードを実行することができましたが、最高得点のコードに問題があります。私は理解していない何らかの理由で、彼らが動作していないので、bufferedreaderとprintwriter関数を使用することができません。プログラムでスコアを最高スコアと比較し、スコアが最高スコアよりも大きい場合、最高スコアはtxtファイルで更新されます。 txtファイルが必要な理由は、いったんプログラムが終了すると、以前の最高得点をチェックする方法が必要になるからです。私は本当にプログラムを使って処理と書き込みと読み取りtxtファイルを使用することに新しいですし、他のサイトやフォーラムではハイスコア変数をtxtファイルに書き込まないために助けになったことはありません。私のコンピュータを壊す準備をしてください。 EM1.score =プログラムの過程で蓄積されたスコア。私の処理の難しさPrintWriterとBufferedReaderの動作

class High_Score { 
int highscore = 0; 
PrintWriter output; //imports the writer 
BufferedReader reader; //imports the reader 
int state = 0; //sets the varoable for the keyboard 

void scoring() { 
int score = EM1.score; 
if (score > highscore) { 
    highscore = score; 
} 
textSize(30); 
text("Your score is "+ score, 150, height/4); 
text("The current highscore is "+highscore+".", 75, height/2); 
text("Try to beat it.", 200, 450); 
textSize(12); 
text("Press esc to exit this page.", 225, 550); 
} 

void reader() { 
importHS(); 
updateHS(); 
} 

void updateHS() { 
int score = EM1.score; 
output = createWriter("highscore.txt"); //creates the file that will be 
if (highscore < score) { 
highscore = score; 
output.print(highscore); 
output.close(); 
} 
} 

void importHS() { 
reader = createReader("highscore.txt"); //reads the current highscore 
if (reader == null) { 
    highscore = 0; 
    return; 
} 
String line; 
try { 
    line = reader.readLine(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
    line = null; 
} 
if (line != null) { 
    highscore = int(line); 
    println(highscore); 
} 
try { 
    reader.close(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 
} 

void type() { //allows the screen to close is esc is pressed on the keyboard 
state = key; 
if (key == ESC) { 
    exit(); 
} 
} 
} 

答えて

0

あなたはほとんどそこにいます。私は少し離れて見るものの唯一のカップルがあります:

  1. あなたは
  2. updateHS()if (highscore < score)をチェックし、ファイルに残りのデータの書き込みを行うためにライターでflush()を呼び出していないことは、書き込みを妨げている:あなたはドン」 scoring()は、現在のスコアが最高得点であり、それに応じて更新されているかどうかを既にチェックしているので、とにかくこれが必要です。

はここにあなたのコードのほとんどは、ディスクから読み取る/書き込むために微調整です:ちょうどそのスケッチをコンパイル可能性があり

EM1Class EM1 = new EM1Class(); 
High_Score hs = new High_Score(); 

void setup(){ 
    size(800,600); 

    EM1.score = 500; 

    hs.reader(); 
} 

void draw(){ 
    background(0); 
    hs.scoring(); 
    hs.updateHS(); 
} 

void keyPressed(){ 
    if(keyCode == UP) EM1.score += 10; 
    if(keyCode == DOWN) EM1.score -= 10; 
} 

class EM1Class{ 
    int score; 
} 
class High_Score { 
    int highscore = 0; 
    PrintWriter output; //imports the writer 
    BufferedReader reader; //imports the reader 
    int state = 0; //sets the varoable for the keyboard 

    void scoring() { 
    int score = EM1.score; 
    if (score > highscore) { 
     highscore = score; 
    } 
    textSize(30); 
    text("Your score is "+ score, 150, height/4); 
    text("The current highscore is "+highscore+".", 75, height/2); 
    text("Try to beat it.", 200, 450); 
    textSize(12); 
    text("Press esc to exit this page.", 225, 550); 
    } 

    void reader() { 
    importHS(); 
    updateHS(); 
    } 

    void updateHS() { 
    int score = EM1.score; 
    output = createWriter("highscore.txt"); //creates the file that will be 
    output.print(highscore); 
    output.flush(); 
    output.close(); 
    } 

    void importHS() { 
    reader = createReader("highscore.txt"); //reads the current highscore 
    if (reader == null) { 
     highscore = 0; 
     return; 
    } 
    String line; 
    try { 
     line = reader.readLine(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
     line = null; 
    } 
    if (line != null) { 
     highscore = int(line); 
     println(highscore); 
    } 
    try { 
     reader.close(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    void type() { //allows the screen to close is esc is pressed on the keyboard 
    state = key; 
    if (key == ESC) { 
     exit(); 
    } 
    } 
} 

There'aプレースホルダEM1Class

High_Scoreクラスは、EM1を直接知っているはずです。 コードを少しリファクタリングして、クラスを使用しているので、現在のようにしっかりと結合されていないことを確認してください。

Java style guideもチェックしてください。一貫性のあるきれいに整理されたコードを書くのに役立ちます。それは、長期的にそうするために規律を築くことに確かに貢献するだけでなく、すぐにコードをスキャンしたり、すぐに読んで、その流れを推測したりすることができるようになります。

もう1つのオプションは、組み込み処理のXMLJSON関数を使用して、解析しやすくなります(便利なロード/保存機能もあります)。

int score = 0; 
int highScore; 

void setup(){ 
    loadHighScore(); 
} 
void draw(){ 
    background(0); 
    text("score:"+score+"\nhighScore:"+highScore+"\n\nuse UP/DOWN\narrows",10,15); 
} 
void keyPressed(){ 
    if(keyCode == UP) score += 10; 
    if(keyCode == DOWN) score -= 10; 
    if(score > highScore) highScore = score; 
} 

void loadHighScore(){ 
    try{ 
    JSONObject jsonScore = loadJSONObject("highScore.json"); 
    highScore = jsonScore.getInt("highScore"); 
    }catch(Exception e){ 
    println("error loading/parsing highScore.json"); 
    e.printStackTrace(); 
    } 
} 
void saveHighScore(){ 
    JSONObject jsonScore = new JSONObject(); 
    jsonScore.setInt("highScore",highScore); 
    saveJSONObject(jsonScore,"highScore.json"); 
} 

//save on close 
void exit(){ 
    saveHighScore(); 
    super.exit(); 
} 
関連する問題