Skip to content

michaalj/pacman-java-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Pacman Java Game

A multithreaded Pacman clone built with Java Swing. The game features a procedurally generated maze rendered via a custom JTable model, four independently threaded ghosts, keyboard-driven player movement, a persistent high score system, and a fully interactive GUI with no CLI interaction required.

What It Does

The player controls Pacman on a dynamically generated board (configurable from 10×10 to 100×100 cells). Pacman collects dots while avoiding four ghosts that move independently on their own threads. The game tracks score, lives, and provides a persistent high score ranking saved to file.

Tech Stack

  • Language: Java (standard library only)
  • GUI: Java Swing (JFrame, JTable, JList, JPanel, JOptionPane)
  • Table model: Custom AbstractTableModel for the game board
  • List model: Custom AbstractListModel for the high score list
  • Rendering: Custom DefaultTableCellRenderer with sprite-based cell rendering
  • Concurrency: java.lang.Thread, synchronized blocks
  • Persistence: File I/O with Scanner / PrintWriter for high score storage

Project Structure

src/
├── Main.java                              # Entry point
├── UI/
│   ├── Menu.java                          # Main menu window (New Game, High Score, Exit)
│   └── Game.java                          # Game window, thread management, input handling
├── Pacman/
│   └── Pacman.java                        # Player entity (Runnable), movement logic
├── Ghost/
│   └── Ghost.java                         # Ghost entity (Runnable), random AI movement
├── Board/
│   ├── GameBoard.java                     # AbstractTableModel, procedural maze generation
│   └── GameBoardColorRenderer.java        # Cell renderer with sprite support
├── Ranking/
│   ├── HighScoreModel.java                # AbstractListModel, file-based persistence
│   └── Player.java                        # Player name + score data object
├── Exceptions/
│   └── InvalidBoardSizeException.java     # Thrown for board sizes outside 10-100 range
└── Images/                                # Sprite assets (Pacman, ghosts, dots)

Setup and Run

Prerequisites: JDK 11 or later.

# Compile
find src -name "*.java" | xargs javac -d out

# Run
java -cp out Main

Important: The game loads sprite images from src/Images/ using relative paths. Run the application from the project root directory to ensure images load correctly.

How to Play

  1. Launch the application — the main menu appears.
  2. Click NEW GAME and enter board dimensions (10–100 rows and columns).
  3. Use arrow keys to move Pacman.
  4. Collect dots (score +1 per dot) while avoiding ghosts.
  5. Pacman starts with 3 lives. Contact with a ghost costs one life.
  6. Press Ctrl+Shift+Q at any time to quit and return to menu.
  7. After game over, enter your name for the high score ranking.
  8. Click HIGH SCORE from the menu to view saved rankings.

Key Design Decisions

  • Board as JTable: The game board is backed by AbstractTableModel. Each cell holds an integer code (0 = empty, 1 = wall, 3 = Pacman, 4–7 = ghosts, 8 = dot). A custom DefaultTableCellRenderer maps these codes to colors or sprite images.
  • One thread per entity: Pacman and each ghost run on independent threads. A separate verification thread checks for collisions and manages lives.
  • Ghost AI: Ghosts move randomly, re-randomizing direction at intersections (when more than 2 paths are available). They preserve the previous cell value to restore dots after passing over them.
  • Maze generation: The board is procedurally generated using a symmetric pattern based on row/column indices, producing a maze with corridors and walls.

Limitations

  • Ghost movement is purely random with no pathfinding toward the player.
  • Image paths are hardcoded as relative paths from the working directory.
  • No power-ups or ghost vulnerability mode is implemented.

Documentation

Additional documentation is available in the docs/ directory:

About

Multithreaded Pacman clone in Java Swing. Procedurally generated maze, sprite-based rendering via custom JTable model, four independently threaded ghosts, and persistent high score system.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages