所以,我能够运行我的代码,但是我遇到了高分代码的问题。我无法使用bufferedreader和printwriter函数,因为由于某种原因,我不理解,他们没有运行。我希望程序将得分与高分进行比较,如果得分大于高分,则高分将在txt文件上更新。 txt文件是必要的原因是由于一旦程序关闭,我需要一个方法来检查以前的高分。我真的很擅长使用程序处理和编写和读取txt文件,而且我看过的其他网站和论坛都没有帮助,因为他们没有将高分数变量写入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();
}
}
}
答案 0 :(得分:0)
你几乎就在那里。我只看到一些稍微偏离的东西:
flush()
将剩余数据写入文件if (highscore < score)
中的updateHS()
是否阻止了写入:无论如何你都不需要这样做,因为scoring()
已经检查当前得分是否为高分并相应地更新。 / LI>
醇>
这里大部分代码已经调整为从磁盘写入/读取:
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();
}
}
}
这里有一个占位符EM1Class
,草图可以编译。
我不确定High_Score
班级应该直接了解EM1
。
您可能希望稍微重构一下代码,并确保它不像现在那样紧密耦合,因为您无论如何都要使用类。
同时查看Java style guide:它将有助于编写一致且整齐有序的代码。从长远来看,它肯定会有助于建立这样的规则,但也会立即得到回报,因为您的代码将更容易扫描/快速阅读并推断它的流程。
另一种选择是使用Processing内置XML和JSON函数,这将使解析更容易(并且还具有方便的加载/保存功能)。
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();
}