-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScorer.java
More file actions
53 lines (42 loc) · 954 Bytes
/
GameScorer.java
File metadata and controls
53 lines (42 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.*;
import java.awt.Graphics;
class GameScorer {
int score;
int highestScore;
//FileOutputStream fwriter;
FileReader freader;
public GameScorer() {
try {
freader = new FileReader("highscore.sg");
BufferedReader reader = new BufferedReader(freader);
highestScore = Integer.parseInt(reader.readLine());
} catch(FileNotFoundException ex) {
highestScore = 0;
} catch(IOException ex) {
highestScore = 0;
} finally {
score = 0;
}
}
private void score() {
score += 10;
if (score >= highestScore) {
highestScore = score;
}
}
public void saveScore() {
try {
FileWriter fout = new FileWriter("highscore.sg");
BufferedWriter writer = new BufferedWriter(fout);
writer.write(highestScore + "\n");
writer.close();
fout.close();
} catch(IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
score();
g.drawString("Score: " + score, 50, 30);
}
}