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
45 changes: 45 additions & 0 deletions pacman-java/PacMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void reset() {
int lives = 3;
boolean gameOver = false;
boolean showInstructions = true;
boolean paused = false;

PacMan() {
setPreferredSize(new Dimension(boardWidth, boardHeight));
Expand Down Expand Up @@ -276,6 +277,26 @@ public void draw(Graphics g) {
g.drawString(startMsg, (boardWidth - fm.stringWidth(startMsg)) / 2, boardHeight / 2 + 130);
}
}
if (paused) {
g.setColor(new Color(0, 0, 0, 180));
g.fillRect(0, 0, boardWidth, boardHeight);

g.setFont(new Font("Arial", Font.BOLD, 40));
FontMetrics fm = g.getFontMetrics();
String pauseTitle = "PAUSED";
g.setColor(Color.YELLOW);
g.drawString(pauseTitle, (boardWidth - fm.stringWidth(pauseTitle)) / 2, boardHeight / 2 - 40);

g.setFont(new Font("Arial", Font.PLAIN, 22));
fm = g.getFontMetrics();
String resumeMsg = "Press P to Resume";
g.setColor(Color.WHITE);
g.drawString(resumeMsg, (boardWidth - fm.stringWidth(resumeMsg)) / 2, boardHeight / 2 + 10);

String quitMsg = "Press R to Restart";
g.setColor(Color.CYAN);
g.drawString(quitMsg, (boardWidth - fm.stringWidth(quitMsg)) / 2, boardHeight / 2 + 50);
}
}

public void move() {
Expand Down Expand Up @@ -357,6 +378,10 @@ public void actionPerformed(ActionEvent e) {
repaint();
return;
}
if (paused) {
repaint();
return;
}
move();
repaint();
if (gameOver) {
Expand Down Expand Up @@ -384,6 +409,26 @@ public void keyReleased(KeyEvent e) {
gameOver = false;
gameLoop.start();
}

// Toggle pause with P key
if (e.getKeyCode() == KeyEvent.VK_P) {
paused = !paused;
return;
}

// Allow restart from pause menu with R key
if (paused && e.getKeyCode() == KeyEvent.VK_R) {
loadMap();
resetPositions();
lives = 3;
score = 0;
gameOver = false;
paused = false;
return;
}

// Guard arrow keys — don't move pacman while paused
if (paused) return;
// System.out.println("KeyEvent: " + e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_UP) {
pacman.updateDirection('U');
Expand Down