<分区>
我正在修改我在 Internet 上找到的一个 2048 游戏,以便它会在玩家输掉或重置游戏时打印出他最后给定的分数。但是,我在保存所有乐谱时遇到了问题。它确实有效,但程序会在每次播放器重置或丢失时覆盖文件。
这是我的作家课。
class writer {
public void writing(int desiredText) {
try {
//Whatever the file path is.
File highScores = new File("C:/Users/Anthony/Desktop/Java/highScores.txt");
FileOutputStream is = new FileOutputStream(highScores);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
String desiredText_string = Integer.toString(desiredText);
w.write(desiredText_string);
w.close();
} catch (IOException e) {
System.err.println("Problem writing to the file highScores.txt");
}
}
}
以防万一,这正是需要作者的时刻。
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
write.writing(myScore);
resetGame();
}
if (!canMove()) {
write.writing(myScore);
myLose = true;
}
... (code is continued on...)