-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreboard.java
More file actions
49 lines (43 loc) · 1.27 KB
/
Copy pathScoreboard.java
File metadata and controls
49 lines (43 loc) · 1.27 KB
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
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.Font;
import greenfoot.Color;
/**
* This class represents the scoreboard that tracks the score and lives of a player.
*
@authors (Rupert & Raymond Van Niekerk)
* @student Numbers (222894237 & 221154469)
* @version (1)
*/
public class Scoreboard extends Actor {
private int score;
private int lives;
public Scoreboard() {
// Constructor initializes score and lives, then updates the display image
score = 0;
lives = 3;
updateImage();
}
public void addScore(int points) {
// Add points to the score and update the display image
score += points;
updateImage();
}
public void loseLife() {
// Decrease lives by 1 and update the display image
lives--;
updateImage();
}
public int getLives() {
// Return the number of lives left
return lives;
}
public boolean isOutOfLives() {
// Check if the player is out of lives
return lives <= 0;
}
private void updateImage() {
// Update the display image with the current score and lives
setImage(new GreenfootImage("Score: " + score + " Lives: " + lives, 24, Color.WHITE, Color.BLACK));
}
}