write pac-man game as standalone java application
- User
write pac-man game as standalone java application
Assistant
I'll create a complete Pac-Man game in Java using Swing. Here's the implementation:
PacManGame.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; public class PacManGame extends JPanel implements ActionListener, KeyListener { private static final int BOARD_WIDTH = 600; private static final int BOARD_HEIGHT = 600; private static final int UNIT_SIZE = 25; private static final int GAME_UNITS = (BOARD_WIDTH * BOARD_HEIGHT) / (UNIT_SIZE * UNIT_SIZE); private static final int DELAY = 100; private int pacmanX = 25; private int pacmanY = 25; private int pacmanDirection = 0; // 0=right, 1=down, 2=left, 3=up private int score = 0; private boolean running = false; private Timer timer; private Random random; private List<Point> dots; private List<Ghost> ghosts; private boolean[][] walls; public PacManGame() { random = new Random(); this.setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT)); this.setBackground(Color.BLACK); this.setFocusable(true); this.addKeyListener(this); initializeGame(); startGame(); } private void initializeGame() { walls = new boolean[BOARD_WIDTH / UNIT_SIZE][BOARD_HEIGHT / UNIT_SIZE]; dots = new ArrayList<>(); ghosts = new ArrayList<>(); // Create simple maze walls createMaze(); // Create dots createDots(); // Create ghosts ghosts.add(new Ghost(300, 300, Color.RED)); ghosts.add(new Ghost(325, 300, Color.PINK)); ghosts.add(new Ghost(275, 300, Color.CYAN)); } private void createMaze() { // Create border walls for (int i = 0; i < BOARD_WIDTH / UNIT_SIZE; i++) { walls[i][0] = true; // top wall walls[i][BOARD_HEIGHT / UNIT_SIZE - 1] = true; // bottom wall } for (int i = 0; i < BOARD_HEIGHT / UNIT_SIZE; i++) { walls[0][i] = true; // left wall walls[BOARD_WIDTH / UNIT_SIZE - 1][i] = true; // right wall } // Add some internal walls for (int i = 3; i < 8; i++) { walls[i][3] = true; walls[i][18] = true; } for (int i = 15; i < 20; i++) { walls[i][8] = true; walls[i][15] = true; } } private void createDots() { for (int x = 1; x < BOARD_WIDTH / UNIT_SIZE - 1; x++) { for (int y = 1; y < BOARD_HEIGHT / UNIT_SIZE - 1; y++) { if (!walls[x][y] && !(x == 1 && y == 1)) { // Don't place dot at starting position dots.add(new Point(x * UNIT_SIZE, y * UNIT_SIZE)); } } } } public void startGame() { running = true; timer = new Timer(DELAY, this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); draw(g); } public void draw(Graphics g) { if (running) { // Draw walls g.setColor(Color.BLUE); for (int x = 0; x < BOARD_WIDTH / UNIT_SIZE; x++) { for (int y = 0; y < BOARD_HEIGHT / UNIT_SIZE; y++) { if (walls[x][y]) { g.fillRect(x * UNIT_SIZE, y * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE); } } } // Draw dots g.setColor(Color.YELLOW); for (Point dot : dots) { g.fillOval(dot.x + 10, dot.y + 10, 5, 5); } // Draw Pac-Man g.setColor(Color.YELLOW); g.fillOval(pacmanX, pacmanY, UNIT_SIZE, UNIT_SIZE); // Draw Pac-Man mouth g.setColor(Color.BLACK); int mouthAngle = pacmanDirection * 90; g.fillArc(pacmanX, pacmanY, UNIT_SIZE, UNIT_SIZE, mouthAngle + 30, 60); // Draw ghosts for (Ghost ghost : ghosts) { g.setColor(ghost.color); g.fillOval(ghost.x, ghost.y, UNIT_SIZE, UNIT_SIZE); // Draw ghost eyes g.setColor(Color.WHITE); g.fillOval(ghost.x + 5, ghost.y + 5, 5, 5); g.fillOval(ghost.x + 15, ghost.y + 5, 5, 5); g.setColor(Color.BLACK); g.fillOval(ghost.x + 6, ghost.y + 6, 3, 3); g.fillOval(ghost.x + 16, ghost.y + 6, 3, 3); } // Draw score g.setColor(Color.WHITE); g.setFont(new Font("Arial", Font.BOLD, 20)); FontMetrics metrics = getFontMetrics(g.getFont()); g.drawString("Score: " + score, (BOARD_WIDTH - metrics.stringWidth("Score: " + score)) / 2, g.getFont().getSize()); } else { gameOver(g); } } public void move() { int newX = pacmanX; int newY = pacmanY; switch (pacmanDirection) { case 0: newX += UNIT_SIZE; break; // right case 1: newY += UNIT_SIZE; break; // down case 2: newX -= UNIT_SIZE; break; // left case 3: newY -= UNIT_SIZE; break; // up } // Check wall collision if (!walls[newX / UNIT_SIZE][newY / UNIT_SIZE]) { pacmanX = newX; pacmanY = newY; } } public void checkDot() { Point pacmanPos = new Point(pacmanX, pacmanY); for (int i = 0; i < dots.size(); i++) { if (dots.get(i).equals(pacmanPos)) { dots.remove(i); score += 10; break; } } // Check win condition if (dots.isEmpty()) { running = false; } } public void moveGhosts() { for (Ghost ghost : ghosts) { // Simple AI: move randomly but avoid walls int direction = random.nextInt(4); int newX = ghost.x; int newY = ghost.y; switch (direction) { case 0: newX += UNIT_SIZE; break; case 1: newY += UNIT_SIZE; break; case 2: newX -= UNIT_SIZE; break; case 3: newY -= UNIT_SIZE; break; } if (newX >= 0 && newX < BOARD_WIDTH && newY >= 0 && newY < BOARD_HEIGHT && !walls[newX / UNIT_SIZE][newY / UNIT_SIZE]) { ghost.x = newX; ghost.y = newY; } } } public void checkGhost() { for (Ghost ghost : ghosts) { if (pacmanX == ghost.x && pacmanY == ghost.y) { running = false; } } } public void gameOver(Graphics g) { // Game Over text g.setColor(Color.RED); g.setFont(new Font("Arial", Font.BOLD, 50)); FontMetrics metrics1 = getFontMetrics(g.getFont()); g.drawString("Game Over", (BOARD_WIDTH - metrics1.stringWidth("Game Over")) / 2, g.getFont().getSize()); // Score text g.setColor(Color.WHITE); g.setFont(new Font("Arial", Font.BOLD, 20)); FontMetrics metrics2 = getFontMetrics(g.getFont()); g.drawString("Score: " + score, (BOARD_WIDTH - metrics2.stringWidth("Score: " + score)) / 2, g.getFont().getSize() + 70); // Win message if (dots.isEmpty()) { g.setColor(Color.GREEN); g.setFont(new Font("Arial", Font.BOLD, 30)); FontMetrics metrics3 = getFontMetrics(g.getFont()); g.drawString("You Win!", (BOARD_WIDTH - metrics3.stringWidth("You Win!")) / 2, g.getFont().getSize() + 120); } } @Override public void actionPerformed(ActionEvent e) { if (running) { move(); checkDot(); moveGhosts(); checkGhost(); } repaint(); } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: pacmanDirection = 2; break; case KeyEvent.VK_RIGHT: pacmanDirection = 0; break; case KeyEvent.VK_UP: pacmanDirection = 3; break; case KeyEvent.VK_DOWN: pacmanDirection = 1; break; } } @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} // Ghost class class Ghost { int x, y; Color color; Ghost(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; } } public static void main(String[] args) { JFrame frame = new JFrame("Pac-Man Game"); PacManGame game = new PacManGame(); frame.add(game); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); } }You'll also need to add this import at the top:
import java.util.Random;This Pac-Man game includes:
Core Features:
- Pac-Man character that moves with arrow keys
- Maze with walls (blue blocks)
- Collectible dots (yellow circles)
- Three ghosts with simple AI movement
- Score tracking
- Win/lose conditions
Game Mechanics:
- Pac-Man can't move through walls
- Collecting dots increases score by 10 points
- Game ends when touching a ghost
- Win by collecting all dots
- Ghosts move randomly but avoid walls
Controls:
- Arrow keys to move Pac-Man
- Game starts automatically
To run the game:
- Save the code as
PacManGame.java - Compile:
javac PacManGame.java - Run:
java PacManGame
The game creates a simple but functional Pac-Man experience with a basic maze layout. You can extend it by adding more sophisticated ghost AI, power pellets, multiple levels, or sound effects.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.