Lives, Score

This commit is contained in:
Urban Modig
2025-08-21 20:14:18 +02:00
parent 712d58e8e3
commit 9bb76ce682
4 changed files with 39 additions and 5 deletions

View File

@ -129,4 +129,8 @@ public class PacMan {
public void resetPosition() {
position = startPosition;
}
public Image getLifeIcon() {
return movmentImages[0][1];
}
}

View File

@ -50,8 +50,7 @@ public class GhostManager {
ghosts.add(blinky);
ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(),new ScatterToTopLeft(), image[2]));
ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), image[1]));
Ghost clyde = new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), image[3]);
ghosts.add(clyde);
ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), image[3]));
setMode(GhostMode.CHASE);
}
@ -68,7 +67,7 @@ public class GhostManager {
public void setMode(GhostMode mode) {
this.globalMode = mode;
log.debug("Mode changed to {}", globalMode);
log.info("Mode changed to {}", globalMode);
for (Ghost g : ghosts) {
g.setMode(mode);
}

View File

@ -12,15 +12,18 @@ import se.urmo.game.util.Direction;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.InputStream;
public class PlayingState implements GameState {
private final Game game;
private final GameStateManager gameStateManager;
private final GhostManager ghostManager;
private final Font arcadeFont;
private PacMan pacman;
@Getter
private GameMap map;
private int score;
private int lives = 3;
public PlayingState(Game game, GameStateManager gameStateManager) {
this.game = game;
@ -28,6 +31,7 @@ public class PlayingState implements GameState {
this.map = new GameMap();
this.pacman = new PacMan(game, new CollisionChecker(map));
this.ghostManager = new GhostManager(new GhostCollisionChecker(map));
this.arcadeFont = loadArcadeFont();
}
@Override
@ -42,6 +46,24 @@ public class PlayingState implements GameState {
map.draw(g);
pacman.draw(g);
ghostManager.draw(g);
drawUI(g);
}
private void drawUI(Graphics2D g) {
g.setColor(Color.WHITE);
g.setFont(arcadeFont);
// Score (above map, left)
g.drawString("Your Score", 48, 48);
g.drawString("" + score, 48, 72);
// Lives (below map, left)
for (int i = 1; i < lives; i++) {
g.drawImage(pacman.getLifeIcon(),
6 * GameMap.OFFSET_X - i * 24,
map.rows() * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y,
null);
}
}
@Override
@ -75,10 +97,19 @@ public class PlayingState implements GameState {
ghost.resetPosition();
} else {
// Pac-Man loses a life
pacman.loseLife();
pacman.resetPosition();
lives--;
if(lives == 0)System.exit(1);
else pacman.resetPosition();
}
}
}
}
private Font loadArcadeFont() {
try (InputStream is = getClass().getResourceAsStream("/fonts/PressStart2P-Regular.ttf")) {
return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(16f);
} catch (Exception e) {
return new Font("Monospaced", Font.BOLD, 16);
}
}
}

Binary file not shown.