C Projects Source code - Pacman Game Project

Published October 20, 2021

In this C Project source code series we are going to create a simple Pacman Game Project console game. This Pacman Game has a resemblance with the snake game as the Pacman is driven to move along the predefined path. The blue path is eaten or erased by the Pacman as it moves. You can score more if you eat more paths, this is a game application that has realistic nature and is simple to play. 

Pacman Game Project is a c console-based application, designed using code:: Blocks with GCC compiler for entertainment purposes. 

Here, the user-defined header file creates the source code, the behavior and moving direction of the Pacman are controlled by the external C files. You can download the source code from the download button below the image and execute it to get a better understanding

 

Pacman Game

 

This source code consit of all files and objects of the game

File structure

C Project - Packman game source code

 

main.c file

#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);

    }
}

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2308 Views