Skip to content

Instantly share code, notes, and snippets.

@rsmahmud
Created January 9, 2017 09:55
Show Gist options
  • Save rsmahmud/3fa0caa815b52f62d3a7fd08fd652653 to your computer and use it in GitHub Desktop.
Save rsmahmud/3fa0caa815b52f62d3a7fd08fd652653 to your computer and use it in GitHub Desktop.
Simple Pacman Like Game with ASCII characters [No Graphics]
/** Simple Pacman Like Game **/
#include<stdio.h>
#include<conio.h>
//#include<stdlib.h> //if compiler asks
#define ROW 20
#define COL 80 //keep it min 50,max 80 for best look
#define MAX_GHOST 20
#define DOTS_FOR_POINT 10
/***************************************************
** ROW * COL - MAX_GHOST = Total Number of dots **
***************************************************/
char dot = '.', ghost = '*', space = ' ', avatar=1;
char CUL[] = "\xC9",CUR[] = "\xBB",CDL[] = "\xC8",CDR[] = "\xBC";
char strdwn[] = "\xBA", strborder[] = "\xCD",Game_Area[ROW][COL];
int ghost_pos[2][MAX_GHOST];
int x, y, i, j,count, point;
int no_of_dots = ROW * COL - MAX_GHOST;
void U_Border(int limit){
printf("%s",CUL);
for(i=0; i <(COL>limit?COL:limit); i++)
printf("%s",strborder);
puts(CUR);
}
void D_Border(int limit){
printf("%s",CDL);
for(i=0; i <(COL>limit?COL:limit); i++)
printf("%s",strborder);
puts(CDR);
}
void Space_Print(int limit, char pos){
if(pos=='L'){
printf(strdwn);
for(i=0; i<(COL>limit?((COL-limit)/2):0); i++)
putchar(' ');
}
else if(pos=='R'){
for(i=0; i<(COL>limit?((COL-limit)/2)+(COL%2?0:1):0); i++)
putchar(' ');
puts(strdwn);
}
else if(pos=='M'){
printf(strdwn);
for(i=0; i<(COL>limit?COL:limit); i++)
putchar(' ');
puts(strdwn);
}
}
void INTRO(){
U_Border(49);Space_Print(49,'L');
printf(" >> Use Arrow Keys to Move the Game Character. ");
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> Moving the Character Outside of Game Area ");
Space_Print(49,'R');Space_Print(49,'L');
printf(" will Result in Force Termination. ");
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> You will gain 1 Point for Passing %2d Dots. ",DOTS_FOR_POINT);
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> There are %2d Ghosts '*' Hidden Behind Dots. ",MAX_GHOST-1);
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> Passing a '*' will Set Your Points to Zero. ");
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> Press 'X' Anytime to Terminate The Program. ");
Space_Print(49,'R');Space_Print(49,'L');
printf(" >> Press ENTER to Start the Game. ");
Space_Print(49,'R');D_Border(49);
getchar();
}
void Generate_Random_Star_Position(){
count =0;point =0; //Initializing important for playing multiple time in a single run
no_of_dots = ROW * COL - MAX_GHOST;
/** Fill Game Area with dots */
for(i=0; i<ROW; ++i)
for(j=0; j<COL; ++j)
Game_Area[i][j] = dot;
/** Generate Random Position for ghost */
srand(time(NULL));
for(j=0; j < MAX_GHOST; ++j)
ghost_pos[0][j] = rand()% COL;
for(j=0; j < MAX_GHOST; ++j)
ghost_pos[1][j] = rand()% ROW;
/** pick a random position for game character */
x = ghost_pos[0][0];
y = ghost_pos[1][0];
Game_Area[y][x]=space;
}
void Print_Game_Area(){
U_Border(COL);
for(i=0; i <ROW; i++){
printf(strdwn);
for(j=0; j<COL; j++){
if(i==y && j==x){
putchar(avatar);
continue;
}
putchar(Game_Area[i][j]);
}
puts(strdwn);
}
D_Border(COL);
}
int Get_Next_Pos(){
fflush(stdin);
switch(getch()){
case 72: --y; break; //UP arrow
case 80: ++y; break; //DOWN arrow
case 75: --x; break; //LEFT arrow
case 77: ++x; break; //RIGHT arrow
case 'x':case 'X':return 0; //Exit
}
return 1;
}
int Check_Star(){
int flag=0;
for(i=1;i<MAX_GHOST; ++i)
if(ghost_pos[0][i]==x){
flag = 1;
break;
}
if(flag == 1)
if(ghost_pos[1][i]==y)
return 1;
return flag = 0;
}
int Check_n_Set_Move(){
if(Game_Area[y][x]==dot){
if(Check_Star()){
//--no_of_dots; //uncomment if hidden ghosts are considered as dots
point = 0;
ghost_pos[0][i]= -1;
ghost_pos[1][i]= -1;
Game_Area[y][x]=ghost; //Star Revealed
//--count; //set behavior of count upon ghost encounter count = 0 or --count or continuous [commented]
return 1;
}
else{
++count;
--no_of_dots;
Game_Area[y][x]=space;
if(count >= DOTS_FOR_POINT){
++point;
count -= DOTS_FOR_POINT;
}
}
}
return 0;
}
void TERMINATED(){
U_Border(21);
Space_Print(21,'L');
printf(" Out of Game Area ");
Space_Print(21,'R');
Space_Print(21,'M');
Space_Print(21,'L');
printf(" Game Terminated ");
Space_Print(21,'R');
D_Border(21);
}
void FINISHED(){
U_Border(21);
Space_Print(21,'L');
printf(" Game Completed ");
Space_Print(21,'R');
Space_Print(21,'M');
Space_Print(21,'L');
printf(" Final Score :%3d ",point);
Space_Print(21,'R');
D_Border(21);
}
void GHOST(){
U_Border(21);
Space_Print(21,'L');
printf("Ghost '*' Encountered");
Space_Print(21,'R');
Space_Print(21,'M');
Space_Print(21,'L');
printf(" All Points Lost. ");
Space_Print(21,'R');
D_Border(21);
}
void Game_Stat(){
U_Border(21);
Space_Print(21,'L');
printf(" Dot Remains :%4d ",no_of_dots);
Space_Print(21,'R');
Space_Print(21,'L');
printf(" Dot Consumed :");
printf("%4d ",no_of_dots==ROW * COL - MAX_GHOST?0:count?count:DOTS_FOR_POINT);
Space_Print(21,'R');
Space_Print(21,'L');
printf(" Current Score :%4d ",point);
Space_Print(21,'R');
D_Border(21);
}
int END(char p){
system("cls");
Print_Game_Area();
U_Border(21);
Space_Print(21,'L');
printf(" Game Exit ");
Space_Print(21,'R');
Space_Print(21,'L');
printf(" Thank You...! ");
Space_Print(21,'R');
Space_Print(21,'L');
printf(" Play Again?(YES/NO) ");
Space_Print(21,'R');
D_Border(21);
switch(getchar()){
case 'N':case 'n':
fflush(stdin);
return 0;
}
fflush(stdin);
return 1;
}
int main(){
system("title Smiley Game");
system("mode 83,30");
while(1){
system("cls");
INTRO();
Generate_Random_Star_Position();
system("cls");
while(1){
Print_Game_Area();
if(Check_n_Set_Move())
GHOST();
else if(!no_of_dots){
FINISHED();
break;
}
else if( y<0 || y>ROW-1 || x<0 || x>COL-1){
TERMINATED();
break;
}
else
Game_Stat();
if(!Get_Next_Pos())
break;
system("cls");
}
if(!END(getch()))
return 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment