Pacman Game In C With Source Code

Pacman Game In C With Source Code

The Pacman Game In C With Source Code is developed using C programming language. In this game, your goal is to eat as many small dots as possible while navigating a maze. Pacman’s movement is managed using the key board’s navigation keys up, down, left, and right.

A pacman game console can be used as a semester project, to learn C programming and application, or simply as a fun game. It aims to give beginners a good understanding of programming small to large projects by imparting general workable and practical information about C.

It was created in the hopes of improving programming logic and serving as a replacement for the classic game Pacman. This pacman game in c language design is straightforward and clean, making it easy for users to learn, use, and navigate.

Pacman Game in C With Source Code 2...
Pacman Game in C With Source Code 2021 | C with Source Code

In this project is also includes a downloadable pacman game in c code for free, just find the downloadable source code below and click to start downloading.

Anyway if you want level up your knowledge in programming especially C/C++ Programming Language, try this new article I’ve made for you Best C Projects with Source Code for Beginners Free Download 2022.

To run this pacman game in c program make sure that you have a Code Blocks or any platform of C installed in your computer.

ABOUT PROJECTPROJECT DETAILS
Project Name : pacman game
Project Platform :C/C++
Programming Language Used:C Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Dev-C++/Codeblocks
Project Type :Desktop Application
Database:Stores data in .DAT file
Upload Date and Time:April 23, 2021-8:16 am
Pacman Game In C With Source Code Information

Pacman Game In C With Source Code Steps On How To Run The Project

Time needed: 5 minutes.

These are the steps on how to run Pacman Game In C With Source Code

  • Step 1: Download

    First, download the source code given below.

    Pacman Game In C Step 1

  • Step 2: Extract file.

    Second, after you finished download the source code, extract the zip file.
    Pacman Game In C Step 2

  • Step 3: Open CodeBlocks

    Third, open “CodeBlocks IDE”.

  • Step 4: Open Project.

    Fourth, open file tab and Open File after that open folder PacmanGame then click the “PacmanGame.c“.

  • Step 5: Run Project

    Fifth, run the project.

  • Step 6: The actual code.

    You are free to copy the given source code below or download the downloadable source code given.

The Code Given Below is the Actual Code of System

#include <stdio.h> // printf()
#include <conio.h> // getch()
#include <windows.h>
#include <stdbool.h>

#define H 30
#define W 60
#define NR_GHOSTS 5

struct coord
{
   int x;
   int y;
};

struct PacMan {
   struct coord position;
   int vx;
   int vy;
   int lives;
   bool chasing;
   int food_collected;
};


struct Ghost {
   struct coord position;
   int vx;
   int vy;
   bool chasing;
};


struct Ghost allGhosts[NR_GHOSTS];

struct PacMan myPacMan = {
                           {
                              .x = 1,
                              .y = 1,
                           },
                           .vx = 0,
                           .vy = 0,
                           .lives = 3,
                           .chasing = false,
                           .food_collected = 0
                         };

char playfield[H][W] =
{
   { "############################################################" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#        # # # # #          #             # # # # # #      #" },
   { "#        #       #         # #            #                #" },
   { "#        #       #        #   #           #                #" },
   { "#        # # # # #       #     #          #                #" },
   { "#        #              # # # # #         #                #" },
   { "#        #             #         #        #                #" },
   { "#        #            #           #       #                #" },
   { "#        #           #             #      #                #" },
   { "#        #          #               #     # # # # # #      #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#        ##        ##           #          ##        #     #" },
   { "#        # #      # #          # #         # #       #     #" },
   { "#        #  #    #  #         #   #        #  #      #     #" },
   { "#        #    # #   #        #     #       #   #     #     #" },
   { "#        #     #    #       # # # # #      #    #    #     #" },
   { "#        #          #      #         #     #     #   #     #" },
   { "#        #          #     #           #    #      #  #     #" },
   { "#        #          #    #             #   #       # #     #" },
   { "#        #          #   #               #  #        ##     #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "#                                                          #" },
   { "############################################################" }
}; // <-- CAUTION! Semicolon necessary!


void initialize()
{
   // 1. replace each empty field in the playfield
   //    with a food field
   for (int i = 0; i < H; i++)
   {
      for (int j = 0; j < W; j++)
      {
         if (playfield[i][j]==' ')
            playfield[i][j] = '.';
      }
   }

   // 2. initialize all ghosts
   for (int i = 0; i < NR_GHOSTS; i++)
   {
      allGhosts[i].vx = 0;
      allGhosts[i].vy = 0;
      allGhosts[i].chasing = true;

      // try to find a (x,y) coordinate randomly where a food piece is
      int x,y;
      do
      {
         x = 1 + rand() % (W-1);
         y = 1 + rand() % (H-1);

      } while (playfield[y][x] != '.');
      allGhosts[i].position.x = x;
      allGhosts[i].position.y = y;
      playfield[y][x] = 'G';

   }


} // initialize


void user_input()
{
   if (_kbhit())
   {
      char c1 = _getch();

      if (c1 == -32)
      {
         char c2 = _getch();

         myPacMan.vx = 0;
         myPacMan.vy = 0;

         switch (c2)
         {
            case 72: myPacMan.vy = -1; break; // cursor up
            case 80: myPacMan.vy = +1; break; // cursor down
            case 75: myPacMan.vx = -1; break; // cursor left
            case 77: myPacMan.vx = +1; break; // cursor right
         }
      }


      //printf("c1=%d c2=%d\n", c1, c2);

   }
}


void move_figures()
{
   // 1. delete PacMan from old position
   playfield[myPacMan.position.y][myPacMan.position.x] = ' ';

   // 2. compute new desired coordinate (nx,ny)
   int nx = myPacMan.vx + myPacMan.position.x;
   int ny = myPacMan.vy + myPacMan.position.y;

   // 3. test whether there is a wall at (nx,ny)
   if (playfield[ny][nx] == '#')
   {
      // Damn! There is a wall! Stop PacMan!
      myPacMan.vx = 0;
      myPacMan.vy = 0;
   }

   // 4. update PacMan's coordinate
   myPacMan.position.x += myPacMan.vx;
   myPacMan.position.y += myPacMan.vy;

   // 5. is there a food piece at the new location?
   if (playfield[ny][nx] == '.')
   {
      myPacMan.food_collected++;
   }

   // 6. put PacMan back again to playfield
   playfield[myPacMan.position.y][myPacMan.position.x] = 'P';


}


void check_for_collisions()
{

}


void show_playfield()
{
   for (int i = 0; i < H; i++)
   {
      for (int j = 0; j < W; j++)
      {
         printf("%c", playfield[i][j]);
      }
      printf("\n");
   }

   printf("Score: %d\n", myPacMan.food_collected);
}


// Set cursor position in console
// see http://www.dreamincode.net/forums/topic/153240-console-cursor-coordinates/
void set_cursor_position(int x, int y)
{
   //Initialize the coordinates
   COORD coord = { x, y };
   //Set the position
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

} // set_cursor_position


void hidecursor()
{
   HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_CURSOR_INFO info;
   info.dwSize = 100;
   info.bVisible = FALSE;
   SetConsoleCursorInfo(consoleHandle, &info);
}


int main()
{
   system("cls");



   hidecursor();
   initialize();

   while (1)
   {
      user_input();
      move_figures();
      check_for_collisions();
      show_playfield();

      Sleep( 1000 / 30 );
      set_cursor_position(0,0);
   }

} // main

Downloadable Source Code

Conclusion

This pacman game in c is only a project made for school requirement purposes only. You can download this program and modify it to suit your client requirements, since this is a student project it means you cannot really expect 100% functionality from this.

Inquiries

If you have any questions or suggestions about pacman game using c, please feel free to leave a comment below.

Leave a Comment