diff --git a/src/com/zetcode/Board.java b/src/com/zetcode/Board.java index 5c0bc8d..fa09c2d 100644 --- a/src/com/zetcode/Board.java +++ b/src/com/zetcode/Board.java @@ -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"; @@ -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); + } + } } } @@ -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); @@ -148,6 +171,7 @@ private void doDrawing(Graphics g) { drawPlayer(g); drawShot(g); drawBombing(g); + drawHUD(g); } else { @@ -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); @@ -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() { @@ -213,6 +240,7 @@ private void update() { alien.setImage(ii.getImage()); alien.setDying(true); deaths++; + score += 10; shot.die(); } }