Итак, я смог запустить свой код, однако у меня возникли проблемы с кодом рекордов. Я не могу использовать функции буферизованного чтения и печати, потому что по какой-то причине, которую я не понимаю, они не работают. Я хочу, чтобы программа сравнила счет с рекордом, и если счет больше, чем рекорд, рекорд будет обновлен в текстовом файле. причина, по которой файл txt необходим, связана с тем, что после закрытия программы мне нужен метод проверки предыдущего рекорда. Я действительно новичок в использовании обработки, записи и чтения файлов txt с помощью программ, и ни один из других сайтов и форумов, на которые я смотрел, не помог, потому что они не записывают переменную highscore в файл txt. Пожалуйста, помогите, я готов сломать свой компьютер. EM1.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
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();
}
}
}