0% found this document useful (0 votes)
7 views16 pages

Rubric of Pacman Game

The document describes a console-based Pacman game project developed by Muhammad Sami Mughal at the University of Engineering and Technology Lahore. Players control Pacman to collect feeds while avoiding ghosts, with scores saved to a file and high scores displayed. The project includes functional requirements, source code, wireframes, data structures, and future improvement plans.

Uploaded by

sheikhwahhaj458
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Rubric of Pacman Game

The document describes a console-based Pacman game project developed by Muhammad Sami Mughal at the University of Engineering and Technology Lahore. Players control Pacman to collect feeds while avoiding ghosts, with scores saved to a file and high scores displayed. The project includes functional requirements, source code, wireframes, data structures, and future improvement plans.

Uploaded by

sheikhwahhaj458
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Pacman Game

Pacman

Session: 2024-2028

Submitted by:
Muhammad Sami Mughal 2024-CS-831

Supervised by:
Mr. Talha

Computer Science
University of Engineering and Technology Lahore
Pakistan

Muhammad Sami Mughal 2024-CS-831


Pacman Game

Major parts of my Project


Description:
This program is a console-based Pacman game where the player controls
Pacman (P) in a maze to collect feeds (.) and avoid ghosts (G). Pacman moves using arrow
keys, and each feed collected increases the score. Ghosts move randomly, and colliding with
a ghost reduces the player's lives. The game ends when all three lives are lost, displaying the
final score. Scores are saved to a file, and high scores are displayed from the saved data.

Users of Application:
 The user can control Pacman using arrow keys to navigate the
maze, collect feeds, and avoid ghosts.
 The user can view their final score and high scores saved from
previous gameplay.

Functional Requirements:
There are many functions in this program and some are written
below;

1. The movePacman function updates Pacman's position based on user input and handles
score increment when collecting feeds.
2. The moveGhost function controls the movement of ghosts randomly within the maze
and updates their positions accordingly.

Muhammad Sami Mughal 2024-CS-831


Pacman Game

Source code of Pacman


#include <iostream>

#include <fstream>

#include <windows.h>

#include <conio.h>

#include <cstdlib>

#include <ctime>

using namespace std;

void maze();

void xycord(int x, int y);

void movePacman(int &x, int &y, int dx, int dy, int &score);

void moveGhost(int &x, int &y, int dx, int dy);

void displayStatus(int lives, int score);

void saveScore(const string &playerName, int score);

void showHighScores();

const int mazeWidth = 79, mazeHeight = 20;

char gameMaze[mazeHeight][mazeWidth];

int main()

Muhammad Sami Mughal 2024-CS-831


Pacman Game

string playerName;

cout << "Enter your name: ";

cin >> playerName;

int px = 40, py = 8; // Pacman coordinates

int gx[6] = {18, 18, 59, 18, 74, 19}; // Ghost coordinates

int gy[6] = {2, 11, 3, 20, 2, 2};

int lives = 3, score = 0;

system("color 04");

srand(time(0)); // Seed for random ghost movement

maze();

displayStatus(lives, score);

while (lives > 0)

// Pacman movement

if (GetAsyncKeyState(VK_DOWN))

movePacman(px, py, 0, 1, score);

if (GetAsyncKeyState(VK_UP))

Muhammad Sami Mughal 2024-CS-831


Pacman Game

movePacman(px, py, 0, -1, score);

if (GetAsyncKeyState(VK_RIGHT))

movePacman(px, py, 1, 0, score);

if (GetAsyncKeyState(VK_LEFT))

movePacman(px, py, -1, 0, score);

// Ghosts movement

for (int i = 0; i < 6; i++)

int direction = rand() % 4;

int dx = 0, dy = 0;

if (direction == 0)

dx = 1;

else if(direction == 1)

Muhammad Sami Mughal 2024-CS-831


Pacman Game

dx = -1;

else if (direction == 2)

dy = 1;

else if (direction == 3)

dy = -1;

moveGhost(gx[i], gy[i], dx, dy);

if (gx[i] == px && gy[i] == py)

lives--;

displayStatus(lives, score);

xycord(px, py);

cout << " ";

px = 40; py = 8; // Reset Pacman position

Sleep(500);

Muhammad Sami Mughal 2024-CS-831


Pacman Game

if (lives <= 0)

xycord(0, mazeHeight + 2);

cout << "Game Over! Final Score: " << score << endl;

saveScore(playerName, score);

showHighScores();

break;

Sleep(100);

return 0;

// Function to render the maze

void maze() {

system("cls");

string mazeData[mazeHeight] = {

"###########################################################################
####",

"### . . . . . . . . . . . . . . . . . . . . . . |%| . ###",

"### . . . . . . . |%| . . . . . . . .###",

Muhammad Sami Mughal 2024-CS-831


Pacman Game

"### . |%| . .|%| .|%| |%| .|%| . . |%| . .###",

"### . |%| . .|%| .|%| |%| .|%| . |%| . |%| . |%| .###",

"### . |%| . .|%| .|%|%|%|%||%| .|%| . |%| . . |%| .###",

"### . |%| . .|%| .|%|. . . |%| .|%| . |%| . . . . |%| .###",

"### . |%| . .|%| .|%|. . |%| .|%| . |%| . |%|. |%| .###",

"### . |%| . . ..|%| .|%|. . |%| .|%| . .... .###",

"### . |%|%|%|%|%|%| .|%|. . |%| .|%| . |%| . |%|%|%||%|%|.###",

"### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .|%| .###",

"### . . . |%| . |%| .###",

"### . |%| .. .. |%| . |%| .###",

"### . |%| . . |%| .|%|. . . . . .###",

"### . |%| |%|%|%|%|%|%|%|%| . |%| .|%| . . ###",

"### . . . . . . . . . |%|. . . . . . . . . . . . |%| . . .###",

"### . |%| |%|%|%|%|%|%|%|%|%| . .|%| . .###",

"### . |%| |%| . .|%| . .###",

"### . |%| |%| . . . . . . . . . . . . . ..###",

"###########################################################################
####"

};

for (int i = 0; i < mazeHeight; i++)

for (int j = 0; j < mazeWidth; j++)

Muhammad Sami Mughal 2024-CS-831


Pacman Game

gameMaze[i][j] = mazeData[i][j];

cout << mazeData[i][j];

cout << endl;

// Function to move Pacman

void movePacman(int &x, int &y, int dx, int dy, int &score)

int newX = x + dx;

int newY = y + dy;

// Check maze boundaries

if (newX >= 0 && newX < mazeWidth && newY >= 0 && newY < mazeHeight)

char nextPos = gameMaze[newY][newX];

if (nextPos == ' ' || nextPos == '.')

if (nextPos == '.') {

score++;

Muhammad Sami Mughal 2024-CS-831


Pacman Game

displayStatus(0, score); // Update score display

xycord(x, y);

cout << " ";

x = newX;

y = newY;

xycord(x, y);

cout << 'P';

// Function to move Ghost

void moveGhost(int &x, int &y, int dx, int dy)

int newX = x + dx;

int newY = y + dy;

// Check maze boundaries

if (newX >= 0 && newX < mazeWidth && newY >= 0 && newY < mazeHeight)

char nextPos = gameMaze[newY][newX];

if (nextPos == ' ' || nextPos == 'P')

Muhammad Sami Mughal 2024-CS-831


Pacman Game

xycord(x, y);

cout << " ";

x = newX;

y = newY;

xycord(x, y);

cout << 'G';

// Function to set cursor position

void xycord(int x, int y)

COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

// Function to display lives and score

void displayStatus(int lives, int score)

Muhammad Sami Mughal 2024-CS-831


Pacman Game

xycord(0, mazeHeight + 1);

cout << "Lives: " << lives << " Score: " << score << " ";

// Function to save score to file

void saveScore(const string &playerName, int score)

ofstream file("scores.txt", ios::app);

if (file.is_open()) {

file << playerName << " " << score << endl;

file.close();

} else {

cout << "Error: Unable to save score." << endl;

// Function to display high scores from file

void showHighScores()

ifstream file("scores.txt");

string playerName;

int score;

Muhammad Sami Mughal 2024-CS-831


Pacman Game

cout << "\nHigh Scores:\n";

if (file.is_open())

while (file >> playerName >> score)

cout << playerName << ": " << score << endl;

file.close();

else

cout << "Error: Unable to read high scores." << endl;

Wireframes:

Fig 1: Player Name

Muhammad Sami Mughal 2024-CS-831


Pacman Game

Fig 2: Actual game, Score and Lives

Fig 3: Scores store in separate file

Data Structure:
Arrays are used to store the coordinates of ghosts (gx[], gy[]) to track their
positions in the maze. They also store the maze data (gameMaze[]) to render the maze layout
and update the game state.

Muhammad Sami Mughal 2024-CS-831


Pacman Game

Function Prototypes:

Function prototypes in this program are used to declare the functions


before their definition, ensuring the main function can call them correctly. They specify the
function’s name, return type, and parameters, allowing the compiler to know about the
functions before their actual implementation.

Examples:

1. void movePacman(int &x, int &y, int dx, int dy, int &score);
This prototype declares the movePacman function, which moves Pacman and updates
the score.
2. void maze();
This prototype declares the maze function, which generates and displays the game’s
maze layout.

Functions Working Flow:

Start

Enter Player Maze


name

Scores Lives

Scores in High Scores


separate file
Muhammad Sami Mughal 2024-CS-831
Pacman Game

Weakness in Program:
My program is not very efficient. I should add a proper interface,
instructions and some other functions.

Future Directions:
I will add more functions in my program to make it better and more
efficient. I will also add colors and headers to make attractive this program.

… …

Muhammad Sami Mughal 2024-CS-831

You might also like