Pacman Game In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Pacman Game In C++

Pacman Game In C++

BLUF: Mastering Pacman Game In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Pacman Game In C++

C++ is renowned for its efficiency. Learn how Pacman Game In C++ enables low-level control and high-performance computing in the tutorial below.

A basic console program or a brief arcade game named Pacman Game in C was developed for enjoyment. Comparable to the snake game, Pacman must be maneuvered so that it follows a predetermined blue path before it disappears or is consumed by Pacman. The gameplay is straightforward and grants points for clearing pathways as you progress. Access the source code and display screens for this endeavor here.

Program Breakdown

Example

# include < iostream >
# include < stdio.h >
# include < windows.h >
# include < string >
# include < vector >

We are integrating all the essential files needed in our codebase.

Example

char tmp_map [ 18 ] [ 32 ] ;
char map [ 18 ] [ 32 ] = {
    " +#############################+ " , 
    " |                             |",
    " |                             | " ,
    " | ## ########### ##   ######### | " ,
    " |   |                         | " ,
    " | | | ### |  |           |     | " ,
    "| |      |  | | ###  |   |  |  | " ,
    "| | #####|  | |      ## |     | " ,
    " | |           | ###  |      |  | " ,
    " | | ##### ###         ##       | " ,
    " |          ######  ####### ### | " ,
    " |                             | " ,
    " | # ### ####      ###   ####### | " ,
    " |                             | " ,
    " |                             | " ,
    " |                             | " ,
    " |                             | " ,
    " + ############################# + " } ;

We are defining the layout of our game using a map of type char.

Example

void ShowMap ( )
{
    for ( int i = 0 ;  i < 18 ; I + + )
    {
        Printf ( " % s \ n " , map [ i ] ) ;
    }
}

The code snippet above is utilized to display the map of our game.

Example

void gotoxy ( short x , short y )
{
    HANDLE hStdout = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
    COORD position = { x , y } ;
    SetConsoleCursorPosition ( hStdout , position ) ;
}

The code snippet above displays the initial position of the Pac-Man character.

Example

class entity
{
public:
    entity(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    void move_x(int p)
    {
        if (map[y][x + p] == ' ')
            x += p;
    }

    void move_y(int p)
    {
        if (map[y + p][x] == ' ')
            y += p;
    }

    void move(int p, int q)
    {
        x += p;
        y += q;
    }

    int get_x() { return x; }
    int get_y() { return y; }

    void draw(char p)
    {
        map[x][y] = p;
        gotoxy(x, y);
        printf("%c", p);
    }
private :
    int x ;
    int y ;
} ;
struct walk
{
    short walk_count ;
    short x ;
    short y ;
    short back ;
} ;
struct target
{
    short x ;
    short y ;
} ;

The class mentioned above is responsible for enabling the movement of our pacman character within the game map.

Example

void AddArray ( int x , int y , int wc , int back )
{
    if ( tmp_map [ y ] [ x ] == ' ' || tmp_map [ y ] [ x ] == ' . ' )
    {
        tmp_map [ y ] [ x ] = ' # ' ;
        walk tmp ;
        tmp.x = x ;
        tmp.y = y ;
        tmp.walk_count = wc ;
        tmp.back = back ;
        BFSArray.push_back ( tmp ) ;
    }
}

The provided code snippet will display the location of the Pacman character post its movement.

Example

void FindPath ( int sx , int sy , int x , int y )
{
    memcpy ( tmp_map , map , sizeof ( map ) ) ;
    BFSArray . clear ( ) ;
    walk tmp ;
    tmp.x = sx ;
    tmp.y = sy ;
    tmp.walk_count = 0 ;
    tmp.back = -1 ;
    BFSArray.push_back ( tmp ) ;

    int i = 0 ;
    while ( i < BFSArray.size ( ) )
    {
        if ( BFSArray [ i ].x = = x & & BFSArray [ i ]. y = = y )
        {
            walk_queue.clear ( ) ;
            target tmp2 ;
            while ( BFSArray [ i ] . walk_count ! = 0 )
            {
                tmp2 . x = BFSArray [ i ] . x ;
                tmp2 . y = BFSArray [ i ] . y ;
                walk_queue . push_back ( tmp2 ) ;

                i = BFSArray [ i ] . back ;
            }
            break ;
        }
        AddArray ( BFSArray [ i ] . x + 1 , BFSArray [ i ] . y , BFSArray [ i ] . walk_count + 1 , i ) ;
        AddArray ( BFSArray [ i ] . x - 1 , BFSArray [ i ] . y , BFSArray [ i ] . walk_count + 1 , i ) ;
        AddArray ( BFSArray [ i ] . x , BFSArray [ i ] . y + 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
        AddArray ( BFSArray [ i ] . x , BFSArray [ i ] . y - 1 , BFSArray [ i ] . walk_count + 1 , i ) ;

        /*
            AddArray ( BFSArray [ i ] . x + 1 , BFSArray [ i ] . y + 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray ( BFSArray [ i ] . x - 1 , BFSArray [ i ] . y + 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray ( BFSArray [ i ] . x + 1 , BFSArray [ i ] . y + 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray ( BFSArray [ i ] . x + 1 , BFSArray [ i ] . y - 1 , BFSArray [ i ] . walk_count + 1 , i ) ;

            AddArray( BFSArray [ i ] . x + 1 , BFSArray [ i ] . y - 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray( BFSArray [ i ] . x - 1 , BFSArray [ i ] . y - 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray( BFSArray [ i ] . x - 1 , BFSArray [ i ] . y + 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
            AddArray( BFSArray [ i ] . x - 1 , BFSArray [ i ] . y - 1 , BFSArray [ i ] . walk_count + 1 , i ) ;
        */
        i++ ;
    }

    BFSArray . clear ( ) ;
}

This function is employed by pacman to identify or locate the pathway.

Example

int main ( )
{
    bool running = true ;
    int x = 15 ; // hero x
    int y = 16 ; // hero y
    int old_x ;
    int old_y ; 
    int ex = 1 ;
    int ey = 1 ;

    int pts = 0 ;

    printf ( " Instruction : \ n1. Arrow Keys to move your hero \ n2. Eat the dots produced by the Eater to gain poins \ n3. Don't get caught by the Eater \ n \ n");
    printf ( " H -> Hard \ n N -> Normal \ n E -> Easy \ n \ n Input : " ) ;

In this section of the driver code, the user will input commands such as arrow keys to navigate the protagonist, consume the dots generated by the eater to accumulate points, and avoid being captured by the eater. Additionally, the user will be prompted to select a difficulty level from hard, normal, or easy.

Example

char diffi ;
    int speedmod = 3 ; 
    cin >> diffi ;

    if ( diffi = = ' N ' )
    {
        speedmod = 2 ;
    }
    else if ( diffi = = ' H ' )
    {
        speedmod = 1 ;
    }

    system ( " cls " ) ;
    ShowMap ( ) ;

    gotoxy ( x , y ) ;
    cout << " H " ;

    int frame = 0 ;

    FindPath ( ex , ey , x , y ) ;

    while ( running )
    {
        gotoxy ( x , y ) ;
        cout << " " ;

        old_x = x ;
        old_y = y ;

        if ( GetAsyncKeyState ( VK_UP ) )
        {
            if ( map [ y - 1 ] [ x ] = = ' . ' )
            {
                y - - ;
                pts + + ;
            }
            else if ( map [ y - 1 ] [ x ] = = ' ' )
                y - - ;
        }
        if ( GetAsyncKeyState ( VK_DOWN ) )
        {
            if ( map [ y + 1 ] [ x ] = = ' . ' )
            {
                y + + ;
                pts + + ;
            }
            else if ( map [ y + 1 ] [ x ] = = ' ' )
                y + + ;
        }

If the user opts for H, N, or E, the velocity will rise correspondingly.

Program for Pacman Game in C++

Example

#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
char tmp_map[18][32];
char map[18][32] = {
    "+#############################+",
    "|                             |",
    "|                             |",
    "|## ########### ##   #########|",
    "|   |                         |",
    "| | |### |  |           |     |",
    "| |      |  | |###  |   |  |  |",
    "| | #####|  | |      ## |     |",
    "| |           |###  |      |  |",
    "| |##### ###         ##       |",
    "|          ######  ####### ###|",
    "|                             |",
    "|# ### ####      ###   #######|",
    "|                             |",
    "|                             |",
    "|                             |",
    "|                             |",
    "+#############################+"};

void ShowMap()
{
    for (int i = 0; i < 18; i++)
    {
        printf("%s\n", map[i]);
    }
}

void gotoxy(short x, short y)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {x, y};

    SetConsoleCursorPosition(hStdout, position);
}

class entity
{
public:
    entity(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    void move_x(int p)
    {
        if (map[y][x + p] == ' ')
            x += p;
    }

    void move_y(int p)
    {
        if (map[y + p][x] == ' ')
            y += p;
    }

    void move(int p, int q)
    {
        x += p;
        y += q;
    }

    int get_x() { return x; }
    int get_y() { return y; }

    void draw(char p)
    {
        map[x][y] = p;
        gotoxy(x, y);
        printf("%c", p);
    }

private:
    int x;
    int y;
};

struct walk
{
    short walk_count;
    short x;
    short y;
    short back;
};

struct target
{
    short x;
    short y;
};

vector<target> walk_queue;

vector<walk> BFSArray;

void AddArray(int x, int y, int wc, int back)
{
    if (tmp_map[y][x] == ' ' || tmp_map[y][x] == '.')
    {
        tmp_map[y][x] = '#';
        walk tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.walk_count = wc;
        tmp.back = back;
        BFSArray.push_back(tmp);
    }
}

void FindPath(int sx, int sy, int x, int y)
{
    memcpy(tmp_map, map, sizeof(map));
    BFSArray.clear();
    walk tmp;
    tmp.x = sx;
    tmp.y = sy;
    tmp.walk_count = 0;
    tmp.back = -1;
    BFSArray.push_back(tmp);

    int i = 0;
    while (i < BFSArray.size())
    {
        if (BFSArray[i].x == x && BFSArray[i].y == y)
        {
            walk_queue.clear();
            target tmp2;
            while (BFSArray[i].walk_count != 0)
            {
                tmp2.x = BFSArray[i].x;
                tmp2.y = BFSArray[i].y;
                walk_queue.push_back(tmp2);

                i = BFSArray[i].back;
            }

            break;
        }

        AddArray(BFSArray[i].x + 1, BFSArray[i].y, BFSArray[i].walk_count + 1, i);
        AddArray(BFSArray[i].x - 1, BFSArray[i].y, BFSArray[i].walk_count + 1, i);
        AddArray(BFSArray[i].x, BFSArray[i].y + 1, BFSArray[i].walk_count + 1, i);
        AddArray(BFSArray[i].x, BFSArray[i].y - 1, BFSArray[i].walk_count + 1, i);

        /*
            AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

            AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
            AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
        */
        i++;
    }

    BFSArray.clear();
}

int main()
{
    bool running = true;
    int x = 15; // hero x
    int y = 16; // hero y
    int old_x;
    int old_y;

    int ex = 1;
    int ey = 1;

    int pts = 0;

    printf("Instruction:\n1. Arrow Keys to move your hero\n2. Eat the dots produced by the Eater to gain poins\n3. Don't get caught by the Eater\n\n");
    printf("H -> Hard\nN -> Normal\nE -> Easy\n\nInput : ");

    char diffi;
    int speedmod = 3;

    cin >> diffi;

    if (diffi == 'N')
    {
        speedmod = 2;
    }
    else if (diffi == 'H')
    {
        speedmod = 1;
    }

    system("cls");
    ShowMap();

    gotoxy(x, y);
    cout << "H";

    int frame = 0;

    FindPath(ex, ey, x, y);

    while (running)
    {
        gotoxy(x, y);
        cout << " ";

        old_x = x;
        old_y = y;

        if (GetAsyncKeyState(VK_UP))
        {
            if (map[y - 1][x] == '.')
            {
                y--;
                pts++;
            }
            else if (map[y - 1][x] == ' ')
                y--;
        }
        if (GetAsyncKeyState(VK_DOWN))
        {
            if (map[y + 1][x] == '.')
            {
                y++;
                pts++;
            }
            else if (map[y + 1][x] == ' ')
                y++;
        }
        if (GetAsyncKeyState(VK_LEFT))
        {
            if (map[y][x - 1] == '.')
            {
                x--;
                pts++;
            }
            else if (map[y][x - 1] == ' ')
                x--;
        }
        if (GetAsyncKeyState(VK_RIGHT))
        {
            if (map[y][x + 1] == '.')
            {
                x++;
                pts++;
            }
            else if (map[y][x + 1] == ' ')
                x++;
        }

        if (old_x != x || old_y != y)
        {
            FindPath(ex, ey, x, y);
        }

        gotoxy(x, y);
        cout << "H";

        map[ey][ex] = '.';
        gotoxy(ex, ey);
        cout << ".";

        if (frame % speedmod == 0 && walk_queue.size() != 0)
        {
            ex = walk_queue.back().x;
            ey = walk_queue.back().y;
            walk_queue.pop_back();
        }

        gotoxy(ex, ey);
        cout << "E";

        if (ex == x && ey == y)
        {
            break;
        }

        gotoxy(32, 18);
        gotoxy(32, 1);
        cout << pts;
        Sleep(100);
        frame++;
    }

    system("cls");
    printf("You Lose and your score is : %i", pts);
    cin.get();
    cin.get();
    cin.get();
    cin.get();
    cin.get();
    cin.get();
    cin.get();
    cin.get();

    return 0;
}

Output:

Output

Instruction:
1. Arrow Keys to move your hero
2. Eat the dots produced by the Eater to gain poins
3. Don't get caught by the Eater
H -> Hard
N -> Normal
E -> Easy

Input : N
+#############################+
|...                          | 0
|  .                          |
|##.########### ##   #########|
|  .|                         |
| |.|### |  |           |     |
| |.     |  | |###  |   |  |  |
| |.#####|  | |      ## |     |
| |......     |###  |      |  |
| |#####.###         ##       |
|       ..E ######  ####### ###|
|                             |
|# ### ####      ###   #######|
|                             |
|                             |
|                             |
|              H              |
+#############################+

You Lose and your score is : 0

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience