Signup/Sign In
PUBLISHED ON: APRIL 8, 2022

Pacman Game Project Using C Language

Pacman Game Project Using C Language

Introduction

Pacman Game in C is a basic console program or a little clip game created for amusement purposes. It is similar to the snake game in that Pacman must be directed in such a manner that he advances down a predetermined blue route before it is deleted or devoured by Pacman. The more you clear the way, the more points you get; the game is simple to play. This project's source code and output screens are shared here.

Source Code

#include "header.h"



SDL_Rect cor={24,25};
int quit=no;

SDL_Surface *load_image( char filename[] )
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    //Load the image
    char file[]="sprites/";
    strcat(file,filename);

    loadedImage = IMG_Load( file);
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
     //Return the optimized image
    return optimizedImage;
}



int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* screen;
    screen=SDL_SetVideoMode(500,350,32,SDL_SWSURFACE);
    SDL_Surface* bg=load_image("dark.jpg");
    int key_press;
    SDL_WM_SetIcon(IMG_Load("pacman_16X16.png"), NULL);
    SDL_WM_SetCaption("Pacman","pacman_16X16.png");

    SDL_Event event;
    extern int quit;

    SDL_BlitSurface(bg,NULL,screen,NULL);
  // build_map(screen);
    gameplay(screen);



    return 0;
}






void pacman_sprites(SDL_Rect location,int direction,SDL_Surface* screen,char comp[20][12])
{



    SDL_Rect char_up,char_down,char_left,char_right,char_neutral,char_dead;
//definition of sprites
    char_up.x=0; char_up.y=20;  char_up.w=20;  char_up.h=20;
    char_down.x=20; char_down.y=20;  char_down.w=20;  char_down.h=20;
    char_left.x=20; char_left.y=0;  char_left.w=20;  char_left.h=20;
    char_right.x=40; char_right.y=0;  char_right.w=20;  char_right.h=20;
    char_neutral.x=0; char_neutral.y=0;  char_neutral.w=20;  char_neutral.h=20;
    char_dead.x=40; char_dead.y=20;  char_dead.w=20;  char_dead.h=20;
// end def of sprites
    SDL_Surface *one,*two;
    one=load_image("pacman.gif");
    two=load_image("pacman_follow.gif");

    if  (legibility(comp,&direction)==yes)
    {
        moveit(direction); // it just changes  the co-ordinates.....doesnt animate sprites
    switch(direction)
    {
        case SDLK_UP:
        SDL_BlitSurface(one,&char_up,screen,&location);
        SDL_Flip(screen);
        break;
        case SDLK_DOWN:
        SDL_BlitSurface(one,&char_down,screen,&location);
        SDL_Flip(screen);
         break;
        case SDLK_RIGHT:
        SDL_BlitSurface(one,&char_right,screen,&location);
        SDL_Flip(screen);
        break;
        case SDLK_LEFT:
        SDL_BlitSurface(one,&char_left,screen,&location);
        SDL_Flip(screen);
        break;
    }


    SDL_Delay(75);
    SDL_BlitSurface(one,&char_neutral,screen,&location);
    SDL_Flip(screen);


    SDL_Delay(75);
    SDL_BlitSurface(two,NULL,screen,&location);
    SDL_Flip(screen);
    }
    else
    {
        //SDL_Delay(1000);
        SDL_BlitSurface(one,&char_neutral,screen,&location);
        SDL_Flip(screen);
    }



    SDL_FreeSurface(one);
    SDL_FreeSurface(two);

}



void gameplay(SDL_Surface* screen)
{

    extern int quit;
    char comp[20][12];
    extern SDL_Rect cor;
    SDL_Event event;
    int key_press;



    createmap(screen,comp);

    while (quit==no)
    {
           while(SDL_PollEvent(&event))
           {

                if(event.type==SDL_QUIT) quit=yes;
                if (event.type==SDL_KEYDOWN)   key_press=event.key.keysym.sym;
            }




            pacman_sprites(cor,key_press,screen,comp);

    }
}

Explanation

The .c files of Pacman Game have been listed as follows:

  • main.c
  • map.c
  • move_guide.c
  • accessories.h

header.h is a user-defined header file produced in the game's source code. The game's C files are used to control Pacman's velocity and direction, as well as to generate the route. The Pacman Game in C demonstrates the usage of file handling, the use of user-defined header files, and the combining of many C files in a single project, among other things.

The Pacman Game in C is quite easy to create, play, and end. To play the game, just double-click the game's application file (.exe file). The game window will then appear, in which you must guide Pacman along the thick blue lines in a rectangle style.

Pacman's movement is controlled by the key board's navigation keys (up, down, left, and right). Pacman's movement is paused by using the space bar. You will be the winner if you can drive the Pacman along all of the lines such that he consumes everything.

Output

Pacman

Final Words

This Pacman Game in C may be used as a semester project, to learn C programming and application, or just as a fun game. If you're planning to submit the project as a college or school project, try making some changes.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.