Welcome to the Pac-Man Game! This classic arcade-style game is implemented in C, featuring ghosts, pellets, and walls for an engaging gameplay experience. You control Pac-Man to collect all the pellets while avoiding ghosts. Enjoy the nostalgia!
This Pac-Man game was developed as part of a first semester project for the course "Programming Fundamentals" at FAST NUCES Karachi. The project aimed to apply fundamental programming concepts and techniques learned during the course.
- Classic Gameplay: Move Pac-Man using
W
,A
,S
, andD
keys. - Ghosts: Randomly moving ghosts add challenge.
- Pellets: Collect all pellets to win.
- Scoring: Keep track of your score and lives.
- Saving/Loading: Save your game and load it later.
-
Controls:
W
: Move UpA
: Move LeftS
: Move DownD
: Move RightQ
: Quit Game
-
Objective: Collect all pellets without getting caught by ghosts to win the game.
-
Game Over: The game ends when you run out of lives or collect all pellets.
-
Saving: Press
Q
and choose to save the game before quitting.
To compile and run the game, follow these steps:
-
Clone the repository:
git clone https://github.com/SHaiderM16/Pac-Man-Game.git
-
Navigate to the project directory:
cd Pac-Man-Game
-
Compile the code:
gcc -o pacman src/pacman.c
-
Run the game:
./pacman
Here's a brief overview of the global variables used in the game:
-
pacmanX & pacmanY: Pac-Man's coordinates.
-
score: Player's current score.
-
lives: Player's remaining lives.
-
pacmanFlag: Track Pac-Man's visited positions.
-
ghostsLoaded: Indicates if ghosts were loaded from a saved game.
The game utilizes a simple structure to represent ghosts:
struct Ghost
{
int x;
int y;
} ghosts[TOTAL_GHOSTS];
Here's a high-level overview of the key functions used in the game:
-
incrementScore(int *score)
: Increases score by 1. -
decrementLive(int*lives)
: Decreases lives by 1. -
setTextColor(int color)
: Sets text color using Windows API. -
displayMap(char map[ROWS][COLUMNS], int *pacmanX, int*pacmanY)
: Displays the game map. -
initialisePellets(char map[ROWS][COLUMNS])
: Initializes pellets on the map. -
movePacman(char map[ROWS][COLUMNS], int *pacmanX, int*pacmanY, char direction)
: Handles Pac-Man's movement and collision. -
initialiseGhosts(char map[ROWS][COLUMNS], int pacmanX, int pacmanY)
: Initializes ghosts. -
ghostsMovement(char map[ROWS][COLUMNS], int *pacmanX, int*pacmanY)
: Handles ghosts' movement. -
saveGame(char map[ROWS][COLUMNS])
: Saves game state to a file. -
loadGame(char map[ROWS][COLUMNS])
: Loads a saved game from a file.
Here's a quick look at some of the core game functions. For full code, please check the source file pacman.c.
void incrementScore(int *score) {
*score = *score + 1;
}
void movePacman(char map[ROWS][COLUMNS], int *pacmanX, int *pacmanY, char direction) {
int x = *pacmanX;
int y = *pacmanY;
map[x][y] = ' '; // Clear the current position of Pacman
switch (direction) {
case 'w':
case 'W':
if (map[x - 1][y] != '#')
*pacmanX = x - 1;
if (map[*pacmanX][*pacmanY] == 'G') {
decrementLive(&lives);
*pacmanX = ROWS / 2;
*pacmanY = COLUMNS / 2;
} else if (map[*pacmanX][*pacmanY] == '.')
incrementScore(&score);
pacmanFlag[*pacmanX][*pacmanY] = 1;
map[*pacmanX][*pacmanY] = 'C';
break;
// Handle other cases...
}
}
This project is licensed under the MIT License - see the LICENSE file for details.
Special thanks to the creators of the original Pac-Man game for the inspiration.
For any questions or feedback, please contact:
- Syed Haider Murtaza at [email protected]
- Muhammad Rayyan at [email protected]
- Mujtaba Kamran at [email protected]