Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/com/zetcode/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Board extends JPanel {

private int direction = -1;
private int deaths = 0;
private int lives = 3;
private int score = 0;

private boolean inGame = true;
private String explImg = "src/images/explosion.png";
Expand Down Expand Up @@ -101,7 +103,16 @@ private void drawPlayer(Graphics g) {
if (player.isDying()) {

player.die();
inGame = false;
lives--;

if (lives <= 0) {
inGame = false;
} else {
player = new Player();
for (Alien a : aliens) {
a.getBomb().setDestroyed(true);
}
}
}
}

Expand All @@ -126,6 +137,18 @@ private void drawBombing(Graphics g) {
}
}

private void drawHUD(Graphics g) {

var small = new Font("Helvetica", Font.BOLD, 12);
g.setFont(small);
g.setColor(Color.white);
g.drawString("Score: " + score, 5, Commons.BOARD_HEIGHT - 5);
String livesStr = "Lives: " + lives;
var fm = this.getFontMetrics(small);
g.drawString(livesStr, Commons.BOARD_WIDTH - fm.stringWidth(livesStr) - 5,
Commons.BOARD_HEIGHT - 5);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Expand All @@ -148,6 +171,7 @@ private void doDrawing(Graphics g) {
drawPlayer(g);
drawShot(g);
drawBombing(g);
drawHUD(g);

} else {

Expand All @@ -167,9 +191,9 @@ private void gameOver(Graphics g) {
g.fillRect(0, 0, Commons.BOARD_WIDTH, Commons.BOARD_HEIGHT);

g.setColor(new Color(0, 32, 48));
g.fillRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 50);
g.fillRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 70);
g.setColor(Color.white);
g.drawRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 50);
g.drawRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 70);

var small = new Font("Helvetica", Font.BOLD, 14);
var fontMetrics = this.getFontMetrics(small);
Expand All @@ -178,6 +202,9 @@ private void gameOver(Graphics g) {
g.setFont(small);
g.drawString(message, (Commons.BOARD_WIDTH - fontMetrics.stringWidth(message)) / 2,
Commons.BOARD_WIDTH / 2);
String scoreStr = "Score: " + score;
g.drawString(scoreStr, (Commons.BOARD_WIDTH - fontMetrics.stringWidth(scoreStr)) / 2,
Commons.BOARD_WIDTH / 2 + 22);
}

private void update() {
Expand Down Expand Up @@ -213,6 +240,7 @@ private void update() {
alien.setImage(ii.getImage());
alien.setDying(true);
deaths++;
score += 10;
shot.die();
}
}
Expand Down