-
Notifications
You must be signed in to change notification settings - Fork 1
/
Score.java
43 lines (34 loc) · 933 Bytes
/
Score.java
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
import greenfoot.*;
import java.awt.Graphics;
public class Score extends Actor {
private static final Color textColor = new Color(1, 1, 1);
private int value = 0;
private int target = 0;
private String text;
private int stringLength;
public Score(String prefix) {
text = prefix;
stringLength = (text.length() + 2) * 20;
setImage(new GreenfootImage(stringLength, 16));
GreenfootImage image = getImage();
image.setColor(textColor);
reload();
}
public void act() {
if (value < target) {
value++;
reload();
} else if (value > target) {
value--;
reload();
}
}
public void add(int score) {
target += score;
}
private void reload() {
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 12);
}
}