-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.java
99 lines (91 loc) · 2.99 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Score
*
* @author Florian Kirchner, Florian Nitschmann
* @version 1.0
*/
public class Score extends BattleArea
{
//counter for the current score
private int counter = 0;
//score the reach the next level
private int maxScore = 1;
//the number of the current background image (world)
private int currentBg;
//variable for Level.class
private Level levelCounter;
//current level
public int level = 1;
//act method
public void act () {
if(counter < 0) {
counter = 0;
}
checkWin();
}
//score constructor (opens a new score)
public Score(int number, Level level) {
levelCounter = level;
String score = "Punkte: ";
score += number;
GreenfootImage img = new GreenfootImage(score.length() * 20, 30);
java.awt.Color color = new java.awt.Color(255, 255, 255);
img.setColor(color);
img.drawString(score, 2, 20);
setImage(img);
}
//update method (update the score count)
public void update(int num) {
counter += num;
setImage(counter);
}
//setImage method (sets a new 'image' [label on world] for the score)
public void setImage(int number) {
String score = "Punkte: ";
score += number;
GreenfootImage img = getImage();
img.clear();
img.drawString(score, 2, 20);
}
//method checkWin (called in act() an checks if maxScore is reached)
public void checkWin() {
if(counter == maxScore) {
//create a new maxScore
int newMaxScore = maxScore+5; //can be changed
maxScore = newMaxScore;
//get a number for a random background image in another level
int newBg = Greenfoot.getRandomNumber(4) + 1;
if(newBg == currentBg) {
newBg = Greenfoot.getRandomNumber(4) + 1;
}
else if(newBg != currentBg){
if(newBg == 1) {
getWorld().setBackground("xp.jpg");
currentBg = 1;
}
else if(newBg == 2) {
getWorld().setBackground("vista.jpg");
currentBg = 2;
}
else if(newBg == 3) {
getWorld().setBackground("win_7.jpg");
currentBg = 3;
}
else if(newBg == 4) {
getWorld().setBackground("mac.jpg");
currentBg = 4;
}
}
//set a new level
int newLevel = level+1;
level = newLevel;
levelCounter.setLevel(newLevel);
}
//called a the begin of the game
else if(counter != maxScore && level == 1) {
currentBg = 1;
levelCounter.setLevel(1);
}
}
}