Pacman Game in C Last Updated : 25 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Making projects is one of the most interesting and fun ways to learn a programming language such as C. In this article, we will learn how to write code for the Pacman game in C. What is Pacman Game?Pacman Game is a classic arcade video game that was first released in 1980 by Namco (formerly known as Namco Bandai Games). It was created by Toru Iwatani and is one of the most iconic and influential video games of all time. Components of the GameThe game contains certain elements as mentioned below: Pacman or Cursor: The Cursor is the main element that is not static in the whole game Walls: Walls are the same as real walls we can't move surpassing them. Food or Points: These are the elements that need to be collected and added to the main score. Demons: These are the entities that need to be avoided in the game.Functions which are used in the game are: move(): It enables pacman to move in the game. Movement can be up, down, left and right.draw(): Prints the game board on the console.initialize(): It defines the board at the start of the game. Puts the walls around boundaries and inside the board, places demons at random places and places eatables at the rest of the places.C Program for Pacman GameBelow is the program for the Pacman game in C language: C // Pacman Game in C language #include <conio.h> #include <stdio.h> #include <stdlib.h> // All the elements to be used // Declared here #define WIDTH 40 #define HEIGHT 20 #define PACMAN 'C' #define WALL '#' #define FOOD '.' #define EMPTY ' ' #define DEMON 'X' // Global Variables are // Declared here int res = 0; int score = 0; int pacman_x, pacman_y; char board[HEIGHT][WIDTH]; int food = 0; int curr = 0; void initialize() { // Putting Walls as boundary in the Game for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (i == 0 || j == WIDTH - 1 || j == 0 || i == HEIGHT - 1) { board[i][j] = WALL; } else board[i][j] = EMPTY; } } // Putting Walls inside the Game int count = 50; while (count != 0) { int i = (rand() % (HEIGHT + 1)); int j = (rand() % (WIDTH + 1)); if (board[i][j] != WALL && board[i][j] != PACMAN) { board[i][j] = WALL; count--; } } int val = 5; while (val--) { int row = (rand() % (HEIGHT + 1)); for (int j = 3; j < WIDTH - 3; j++) { if (board[row][j] != WALL && board[row][j] != PACMAN) { board[row][j] = WALL; } } } // Putting Demons in the Game count = 10; while (count != 0) { int i = (rand() % (HEIGHT + 1)); int j = (rand() % (WIDTH + 1)); if (board[i][j] != WALL && board[i][j] != PACMAN) { board[i][j] = DEMON; count--; } } // Cursor at Center pacman_x = WIDTH / 2; pacman_y = HEIGHT / 2; board[pacman_y][pacman_x] = PACMAN; // Points Placed for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (i % 2 == 0 && j % 2 == 0 && board[i][j] != WALL && board[i][j] != DEMON && board[i][j] != PACMAN) { board[i][j] = FOOD; food++; } } } } void draw() { // Clear screen system("cls"); // Drawing All the elements in the screen for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { printf("%c", board[i][j]); } printf("\n"); } printf("Score: %d\n", score); } // Function enables to move the Cursor void move(int move_x, int move_y) { int x = pacman_x + move_x; int y = pacman_y + move_y; if (board[y][x] != WALL) { if (board[y][x] == FOOD) { score++; food--; curr++; if (food == 0) { res = 2; return; } } else if (board[y][x] == DEMON) { res = 1; } board[pacman_y][pacman_x] = EMPTY; pacman_x = x; pacman_y = y; board[pacman_y][pacman_x] = PACMAN; } } // Main Function int main() { initialize(); char ch; food -= 35; int totalFood = food; // Instructions to Play printf(" Use buttons for w(up), a(left) , d(right) and " "s(down)\nAlso, Press q for quit\n"); printf("Enter Y to continue: \n"); ch = getch(); if (ch != 'Y' && ch != 'y') { printf("Exit Game! "); return 1; } while (1) { draw(); printf("Total Food count: %d\n", totalFood); printf("Total Food eaten: %d\n", curr); if (res == 1) { // Clear screen system("cls"); printf("Game Over! Dead by Demon\n Your Score: " "%d\n", score); return 1; } if (res == 2) { // Clear screen system("cls"); printf("You Win! \n Your Score: %d\n", score); return 1; } // Taking the Input from the user ch = getch(); // Moving According to the // input character switch (ch) { case 'w': move(0, -1); break; case 's': move(0, 1); break; case 'a': move(-1, 0); break; case 'd': move(1, 0); break; case 'q': printf("Game Over! Your Score: %d\n", score); return 0; } } return 0; } Output: Comment More infoAdvertise with us S shubhamg0h7 Follow Improve Article Tags : C Language Geeks Premier League C Projects Geeks Premier League 2023 Similar Reads Top 25 C Projects with Source Codes for 2025 Ready to improve your C programming skills? youâre in the right place! This article is packed with C project ideas for all skill levels whether youâre a beginner, intermediate learner, or looking for advanced challenges. Working on these projects will not only boost your programming skills but also 12 min read Rock Paper Scissor in C Rock Paper Scissor (which is also called Stone Paper Scissor) is a hand game and played between two people, in which each player simultaneously forms one of three shapes. The winner of the game is decided as per the below rules: Rock vs Paper -> Paper wins.Rock vs Scissor -> Rock wins.Paper vs 4 min read Hangman Game in C Hangman game is a popular and simple game in which the player has to guess the word based on the given hint. In this article, we will write a program for the hangman game using C programming language. What is the Hangman Game? Hangman is a word puzzle game that involves: A secret word: A word is sel 6 min read C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition, 3 min read Snake and Ladder Game in C Snake and Ladder game is a traditional game that involves two or more players and the winner is the guy who reaches the final square on the game board at the earliest. Components of the GameA square board with a series of numbered squares arranged in m x m grid.A dice to be rolled usually uses a six 5 min read Bank account system in C using File handling This article focuses on how to create a bank account system using C language and File handling in C. Approach:Let's discuss the approach in detail, covering all the functions and their explanation in detail- Create a Menu in the main function and create different functions for the Menu, which will b 12 min read Student Information Management System Prerequisites: Switch Case in C/C++ Problem Statement: Write a program to build a simple Software for Student Information Management System which can perform the following operations: Store the First name of the student.Store the Last name of the student.Store the unique Roll number for every studen 14 min read E -Library Management System In this article, we will discuss the approach to creating an E-Library Management System where the user has the following options: Add book information.Display book information.To list all books of a given author.To list the count of books in the library. E -Library Management System Functionalities 10 min read Program for Employee Management System A Employeeâs Management System (EMS) is a software built to handle the primary housekeeping functions of a company. EMS help companies keep track of all the employees and their records. It is used to manage the company using computerized system. Aim of Employeeâs Management System: Built The Employe 14 min read Hospital Management System in C A Hospital Management System is software that facilitates its users some basic operations that take place in a typical hospital. This Hospital Management System in C language is a project for Hospitals having the following functionalities: Printing Hospital DataPrint Patients DataSort by Bed PriceSo 10 min read Like