This project has been created as part of the 42 curriculum by jtruckse, wcheung.
A 2D Pac-Man game built with Python and Pygame as part of the 42 curriculum. Features dynamic mazes loaded from an external generator wheel, ghost AI, multi-level progression, persistent highscores, and an arcade-style menu.
- Python 3.12+
uvpackage manager
- Install dependencies and wheel package:
make install
- Run the game:
Or directly:
make run
uv run python3 pac-man.py config.json
- Menu:
W/SorUp/Down: Navigate buttonsSpaceorEnter: Select option
- In-Game:
W/A/S/DorArrow Keys: Move Pac-ManSpace: Start / Pause gameEscape: Return to menu
- Cheat Mode:
C: Toggle cheat modeT: Freeze timerL: Reset lives to 3N: Skip level / WinX: Exit cheat mode
- Highscores Screen:
- Type name (up to 10 characters, alphanumeric and spaces)
Backspace: Delete characterEnter: Submit scoreEscapeorSpace: Return to menu from leaderboard
The game is configured via a JSON file (e.g. config.json):
| item | description | data type | default value |
|---|---|---|---|
highscore_filename |
The file name (or path) where top 10 highscores are stored | str | "highscores.json" |
seed |
Seed used for random maze generation and reproducibility | int | 42 |
lives |
Number of chances before game over | int | 3 |
level_max_time |
Maximum time allowed per level in seconds | int | 90 |
pacgum |
Number of pacgums spawned in each level | int | 42 |
points_per_pacgum |
Score points per eaten pacgum | int | 10 |
points_per_super_pacgum |
Score points per eaten super pacgum | int | 50 |
points_per_ghost |
Score points per eaten ghost | int | 200 |
levels |
List of level config with level, width, and height |
list[dict] | [{"level": 1, "width": 20, "height": 20}] |
A demo of config.json:
{
"highscore_filename": "highscores.json",
"seed": 42,
"lives": 3,
"level_max_time": 90,
"pacgum": 42,
"points_per_pacgum": 10,
"points_per_super_pacgum": 50,
"points_per_ghost": 200,
"levels": [
{
"level": 1,
"width": 15,
"height": 15
},
{
"level": 2,
"width": 15,
"height": 15
},
{
"level": 3,
"width": 15,
"height": 15
},
{
"level": 4,
"width": 15,
"height": 15
},
{
"level": 5,
"width": 15,
"height": 15
},
{
"level": 6,
"width": 15,
"height": 15
},
{
"level": 7,
"width": 15,
"height": 15
},
{
"level": 8,
"width": 15,
"height": 15
},
{
"level": 9,
"width": 15,
"height": 15
},
{
"level": 10,
"width": 15,
"height": 15
}
]
}
The highscore system persists player scores across game sessions (src/highscores.py).
- Storage Format: Stored as a simple JSON list at the path specified by
"highscore_filename"inconfig.json(defaults tohighscores.json) - Data Validation & Constraints (
HighscoreRecord):- Player names: 1 to 10 characters, restricted to alphanumeric characters and spaces
- Scores: Non-negative integers (
score >= 0) - Top 10 Highscores: Maintained in descending order of score, keeping at most the top 10 entries
- Robustness & Error Handling:
- Missing, empty, or corrupted files gracefully fall back to an empty list without crashing
- Malformed or invalid entries are discarded during loading
- Interactive Pygame Screens (
Leaderboard&NameInput):Leaderboard: Displays the top 10 ranked players and scores directly in the Pygame window, accessible from the main menu or after submitting a scoreNameInput: Interactive post-game screen where players type their name with live character/length validation, on-screen error feedback, and a confirmation screen (score_saved) before transitioning to the leaderboard
Mazes are generated dynamically via MazeLoader (src/maze_loader.py) using the external mazegenerator package (A-Maze-ing wheel):
-
Generator Configuration:
-
MazeLoader.load(width, height, seed)instantiatesMazeGenerator(size=(width, height), entry_cell=(0, 0), exit_cell=(0, 0), perfect=False, seed=seed) - Setting
perfect=Falsecreates loops and multiple paths rather than dead-end-heavy perfect mazes, which is essential for Pac-Man gameplay so the player can evade ghosts - Dimensions (
width,height) are read per level fromconfig.json(enforced as square grids up to25x25), andseedensures reproducible layouts
-
-
Wall Bitmask Encoding:
- Each cell in
MazeData.grid(list[list[int]]) stores an integer bitmask (0–15) representing which walls surround that cell:-
1(UP/ Top wall) -
2(RIGHT/ Right wall) -
4(DOWN/ Bottom wall) -
8(LEFT/ Left wall)
-
-
DrawMaze(src/drawmaze.py) andPacMan(src/pacman.py) use bitwise AND (cell & direction) to render walls and check valid movement directions
- Each cell in
-
Robust Error Handling & Subject V.4 Compliance:
- In strict compliance with 42 Subject Section V.4 ("You must not write your own generator... Your loader must adapt to their interface, not the opposite. If the generator fails, you must handle the error cleanly."), the project does not generate custom mazes.
- If the external
mazegeneratorwheel is missing, crashes, or returns an invalid grid,MazeLoaderraises a customMazeLoaderError. -
GameManagercatches this error cleanly, presents an on-screen/console warning, and returns safely to the main menu without crashing.
-
Dynamic Spawn Placement (
_find_spawn_cell):- Mazes with dimensions
$\ge 14 \times 10$ generated by the assigned wheel embed a solid$5 \times 7$ "42" pattern (cells set to15/ all four walls closed) at the center. -
DrawMazedynamically checks the spawn cell: if solid (15), it steps left until an open, walkable cell is found, ensuring Pac-Man never spawns trapped inside obstacle walls.
- Mazes with dimensions
- Architecture: Centralized finite state machine (
GameManager) controlling transitions betweenMENU,PLAYING,NAME_INPUT,HIGHSCORES, andQUIT - Maze Integration: Interfaces with external
mazegeneratorwheel, parses wall bitmasks (1: UP, 2: RIGHT, 4: DOWN, 8: LEFT), and cleanly handles wheel errors viaMazeLoaderError - Player & Collision: Grid-aligned movement with bitwise wall checks preventing movement into closed cells
- Ghost AI & States: Ghosts spawn at corners, chase Pac-Man, and switch to frightened/flee behavior when super-pacgums are eaten
- Scoring & Lives: Real-time scoring from pacgums, super-pacgums, and eaten ghosts; player lives tracking with countdown timer per level
- Leaderboard: Persistent JSON storage preserving top 10 scores with in-game name entry and input sanitization
pacman/
├── pac-man.py # Entry point: CLI validation, ConfigParser -> GameManager
├── config.json # Game & level configuration
├── highscores.json # Persistent Top-10 leaderboard storage
├── assets/ # Directional sprite sheets (Pac-Man, Ghosts)
├── project_management/ # Team roles, phases & timeline tracking
└── src/
├── parser.py # ConfigParser, GameConfig, LevelsConfig
├── game_manager.py # GameManager & GameState state machine
├── menu.py # GameMenu (pygame_menu main menu UI)
├── highscores.py # HighscoreRecord, NameInput, Leaderboard
├── maze_loader.py # MazeLoader, MazeData (A-Maze-ing wheel integration)
├── drawmaze.py # DrawMaze (maze wall rendering & game loop)
├── player_mechanics.py # Game (start/pause state controller)
├── pacman.py # PacMan (grid movement, wall checks, animation)
├── pacgum.py # Pacgum (distribution & collision detection)
├── ghosts.py # Ghosts (Blinky, Pinky, Inky, Clyde targeting)
└── asset_helper.py # Sprite loader helper
The application is driven by a central state machine in GameManager (src/game_manager.py) using GameState:
pac-man.py— Entry point: validates CLI arguments, parsesconfig.jsonviaConfigParser, and launchesGameManagersrc/game_manager.py— Central controller managing the Pygame window, clock, and transitions betweenGameState.MENU,GameState.PLAYING,GameState.NAME_INPUT,GameState.HIGHSCORES, andGameState.QUITsrc/menu.py—GameMenu: main menu UI returning"PLAY","HIGHSCORES", or"QUIT"src/highscores.py—HighscoreRecord(JSON persistence & validation),NameInput(post-game name entry UI), andLeaderboard(top-10 display UI)src/maze_loader.py—MazeLoader&MazeData: wrapper aroundMazeGeneratorwith validation and clean error handlingsrc/drawmaze.py,src/pacman.py,src/pacgum.py,src/ghosts.py,src/player_mechanics.py— Maze rendering, Pac-Man movement/animation, pacgum distribution/collision, and ghost mechanics
┌──────────────────────┐
│ pac-man.py │
└──────────┬───────────┘
│ parses config.json
▼
┌──────────────────────┐
│ ConfigParser │
└──────────┬───────────┘
│ GameConfig
▼
┌──────────────────────┐
│ GameManager │
└──────────┬───────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ GameState │ Play │ GameState │ Game End │ GameState │
│ MENU ├─────────►│ PLAYING ├─────────►│ NAME_INPUT │
│ (GameMenu) │ │ (DrawMaze) │ │ (NameInput) │
└───────┬───────┘ └───────┬───────┘ └───────┬───────┘
│ │ │
│ Highscores ├─► MazeLoader │ Submit Score
│ ├─► PacMan ▼
│ ├─► Pacgum ┌───────────────┐
│ └─► Ghosts │ GameState │
└────────────────────────────────────────────►│ HIGHSCORES │
│ (Leaderboard) │
└───────────────┘
We track progress, weekly sprints, and task division in the project_management/ directory:
- project_management/README.md: Team responsibilities and tools
- project_management/timeline.md: Weekly tasks, development phases, and planned vs. actual tracking
- project_management/risks.md: Risk analysis and mitigations
- project_management/bugs.md: Bugs found/fixed and acceptance tests
- Trello Board: Sprint to-dos and task cards
- Python whl
- pygame-menu guide
- pygame-menu create menu
- pygame screen blit vs flip
- pygame documentation
- pygame tutorial
- we used AI to explore python and pygame functions
- to explain errors and debug
- to explain when stuck at some points
- to structure sentences better for README