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.
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 PROJECT | PROJECT 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 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.
- Step 2: Extract file.
Second, after you finished download the source code, extract the zip file.
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
#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.
Related Articles
- Pacman Game In Java With Source Code
- Pacman In Python Code
- JavaScript Snake Game with Source Code
- Snake Game In Java With Source Code
- Snakes and Ladders Game in Python with Source Code
- Code For Game in Python: Python Game Projects With Source Code
Other Articles you might read also:
- Best Final Year Project for Computer Science 2021
- Best Thesis Title For IT/CS Student [Updated 2021]
- How to Make An Effective Thesis or Capstone Document
- Writing A Good Research Title For Thesis or Capstone Project
- ICHS Library Management System
- College Management System Project in Django with Source Code
- Search Filter In Django With Source Code
- Best Python Course Online | Python Course Free 2021
- ECOMMERCE IN JAVASCRIPT FRAMEWORK WITH SOURCE CODE
- To Do List Project in Python with Source Code | Video | 2020
Inquiries
If you have any questions or suggestions about pacman game using c, please feel free to leave a comment below.