Pacman Game in C
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 Game
The 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 Game
Below 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; } |
Contact Us