Snake Game Using C

/* forward your comments to bestengineeringprojects.com */

To setup the graphics library:
copy graphics.h to include folder and lbgi.a to library folder.

Click Here To Download graphic.h and lbgi.a
for linker parameters add these in project:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

then start coding

The Library files used is provided by
Computer Science Depart. of Colorado University
( http://www.cs.colorado.edu/~main/bgi/ )

snake game using C

The Code:

Now you can simply paste following code in the source file and compile , execute it. ( If you would like to know more about the code or would like to read the description of the code then scroll down.):

/*
Name: Snake Game
Copyright: bestengineeringprojects.com
Date: 03/10/12 00:44
Description: Snake Game using C
*/

#include <graphics.h>
#include <stdio.h>
#include <windows.h>

#define lft 1
#define rgt 2
#define up 3
#define dwn 4

int score; //to store score
int gamedelay = 25;//to store game speed
int foodcount=0; //To count the numbers of foods
int foodgx,foodgy;

int hscore; // Highest Score

// Opening the file for finding out previous highest scorer and score
void hscore_read()
{
char hscoresst[5];
FILE * scr;
scr = fopen(“score.txt”,”r”);
fscanf(scr, “%s”,hscoresst);
hscore= atoi(hscoresst);
fclose(scr);

}

void hscore_write()
{
FILE * scr;
scr = fopen(“score.txt”,”w”);
fprintf(scr, “%d”,score);
fclose(scr);

}

 

 

struct Snake_Data {
int length;
int head_x; // to store x axes of head
int head_y; // to store y axes of head
int head_dir; // to store direction of head
int tail_x; // to store x axes of tail
int tail_y; // to store x axes of tail
int tail_dir; // to store direction of tail
int bend_x [500]; //to stroe x co-ordinate of bends
int bend_y [500]; //to stroe y co-ordinate of bends
int bend_dir [500]; // to store the bend direction to bend the tail when it reaches that point
} Snake; // snake variable

void initialdatas() //Initializes the datas to be used in the game
{
int i;
Snake.length = 30;
Snake.head_x = 180+50;
Snake.head_y = 80+50;
Snake.head_dir = rgt;
Snake.tail_x = Snake.head_x- Snake.length;
Snake.tail_y = Snake.head_y;
Snake.tail_dir = Snake.head_dir;
for (int i = 0; i <500;i++)
{
Snake.bend_x[i] = 0;
Snake.bend_dir[i] = 0;
}
score = 0;

}

void initialscreen( ) //Draws Intial Screen.
{
//”poly” is for the polygon to draw button for highest score and “i” , “j” is for loop variable.
int i,j, poly[8];
//Scorestring is for saving the text “score” an intiger “score” into one string variable.
char scorestring [100];
char info [100];
//Draw Boundary
setcolor (3);
line (180,80,680,80);
line (680,80,680,480);
line (680,480,180,480);
line (180,480,180,80);

//Draw Menus
setfillstyle(SOLID_FILL, 9);

//Draw Press P to Pause and R to resume
fillellipse(20, 70, 5, 10);
outtextxy (35,60, “Press P to pause & resume “);

//Draw Current Score
fillellipse(20, 125, 5, 10);
sprintf (scorestring, “Score : %d”, score);
outtextxy (35,115, scorestring);

// Draw Button for highest score
poly[0]=20;
poly[1]=185;
poly[2]=20;
poly[3]=255;
poly[4]=175;
poly[5]=255;
poly[6]=175;
poly[7]=185;
setfillstyle(SOLID_FILL, 5);
fillpoly(4, poly);
setcolor (3);
outtextxy (20,160, ” Click for Highest Score:”);

// Draw text area for highest score
poly[0]=20;
poly[1]=285;
poly[2]=20;
poly[3]=355;
poly[4]=175;
poly[5]=355;
poly[6]=175;
poly[7]=285;
setfillstyle(SOLID_FILL, 4);
fillpoly(4, poly);
outtextxy (20,265, “Highest Score:”);

//Leval Information
outtextxy (20,365, ” Curren Leval: 1″);
outtextxy (20,395, ” Press 1 – 9″);
outtextxy (20,410, ” to change leval”);

// Draw Header texts
outtextxy (455,20, “Welcome to the Snake game.”);
outtextxy (400,40, “Developed for final year project in C.”);
outtextxy (400,60, ” copyrights @ bestengineeringprojects.com”);

//draw starting snake
for (int i = Snake.length; i>0;i–)
{
putpixel (Snake.head_x-i,Snake.head_y,3);

}
}

void movesnake ()
{
//Move the Head
if (Snake.head_dir == lft)
{
Snake.head_x –;
}
if (Snake.head_dir == rgt)
{
Snake.head_x ++;
}
if (Snake.head_dir == up)
{
Snake.head_y –;
}
if (Snake.head_dir == dwn)
{
Snake.head_y ++;

}
putpixel (Snake.head_x, Snake.head_y,3);

//Move the Tail
putpixel (Snake.tail_x, Snake.tail_y,0);
if (Snake.tail_dir == lft)
{
Snake.tail_x –;
}
if (Snake.tail_dir == rgt)
{
Snake.tail_x ++;
}
if (Snake.tail_dir == up)
{
Snake.tail_y –;
}
if (Snake.tail_dir == dwn)
{
Snake.tail_y ++;

}

}

void userinput ()
{
int mousexa,mouseya; //To store the position off mouse for mouse events handling functions.
static int i = 0;
if ( i > 1000) i = 0;
static int j = 0;
if ( j > 1000) j = 0;
char input;

//Processor For Keyboard commands
if (kbhit ())
{
input = getch ();

//Change Respective Return value to our MACRO Direction Code Value

if (input == 80) input = dwn;

if (input == 72) input = up;

if (input == 75) input = lft;

if (input == 77) input = rgt;
//Processor For keyboard number 1-10 hit and leval change
if (input == 49)
{
gamedelay = 25;
outtextxy (20,365, ” Curren Leval: 1″);
}
if (input == 50)
{
gamedelay = 22;
outtextxy (20,365, ” Curren Leval: 2″);
}
if (input == 51)
{
gamedelay =19;
outtextxy (20,365, ” Curren Leval: 3″);
}
if (input == 52)
{
gamedelay = 16;
outtextxy (20,365, ” Curren Leval: 4″);
}
if (input == 53)
{
gamedelay = 13;
outtextxy (20,365, ” Curren Leval: 5″);
}
if (input == 54)
{
gamedelay = 10;
outtextxy (20,365, ” Curren Leval: 6″);
}
if (input == 55)
{
gamedelay = 7;
outtextxy (20,365, ” Curren Leval: 7″);
}
if (input == 56)
{
gamedelay = 4;
outtextxy (20,365, ” Curren Leval: 8″);
}
if (input == 57)
{
gamedelay = 1;
outtextxy (20,365, ” Curren Leval: 9″);
}
//Processor for Pause command
if (input == 112)
{
recheck:
if (kbhit ())
{
goto resume;
}
else
{
delay(50);

//Processor For mouse commands for displaying highest score.
if(ismouseclick(WM_LBUTTONDOWN )) // detects the event of mouse left button click down
{
mousexa = mousex() ; mouseya = mousey(); // detects the position of mouse
if(mousexa < 175 && mousexa > 20 && mouseya < 255 && mouseya > 185) // finds if the mouse is inside the button or not
{
char scorestring[20];
sprintf (scorestring, “%d”, hscore);
outtextxy (30,290,scorestring );
}
clearmouseclick(WM_LBUTTONDOWN);
}

goto recheck;
}

}
resume:

//Change head direction based on logic

if (input == lft && Snake.head_dir != rgt && Snake.head_dir != lft)
{
Snake.head_dir = lft;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = lft;
i++;
}
if (input == rgt && Snake.head_dir != lft && Snake.head_dir != rgt)
{
Snake.head_dir = rgt;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = rgt;
i++;
}
if (input == up && Snake.head_dir != dwn && Snake.head_dir != up)
{
Snake.head_dir = up;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = up;
i++;
}
if (input == dwn && Snake.head_dir != up && Snake.head_dir != dwn)
{
Snake.head_dir = dwn;
Snake.bend_x [i] = Snake.head_x;
Snake.bend_y [i] = Snake.head_y;
Snake.bend_dir [i] = dwn;
i++;
}

}

//Code to change the y direction at respective time
if (Snake.tail_x == Snake.bend_x [j] && Snake.tail_y == Snake.bend_y [j])
{
Snake.tail_dir = Snake.bend_dir [j];
j++;

}

}

int randomvalue (int starting, int ending) // to find a random value between staring and ending

{
int difference=ending-starting;
return (rand() % difference);

}

void gamephysics ()
{
int futurex, futurey, futurepixel;
int i;
char scorestring [100];
char info [100];
if (foodcount < 1 ) //always keeps atleast one food
{
int valid = 0;
int foodx;
int foody;
while (!valid)
{
foodx = 180+ randomvalue (0,494);
foody = 80 + randomvalue (0,394);
//Saved value of foodx an foody in global variable for using data to remove food in another function
foodgx=foodx;
foodgy=foody;
// Prints Food
putpixel (foodx,foody,7);
putpixel (foodx,foody+1,7);
putpixel (foodx,foody+2,7);
putpixel (foodx,foody+3,7);
putpixel (foodx,foody+4,7);
putpixel (foodx+1,foody,7);
putpixel (foodx+1,foody+1,7);
putpixel (foodx+1,foody+2,7);
putpixel (foodx+1,foody+3,7);
putpixel (foodx+1,foody+4,7);
putpixel (foodx+2,foody,7);
putpixel (foodx+2,foody+1,7);
putpixel (foodx+2,foody+2,7);
putpixel (foodx+2,foody+3,7);
putpixel (foodx+2,foody+4,7);
putpixel (foodx+3,foody,7);
putpixel (foodx+3,foody+1,7);
putpixel (foodx+3,foody+2,7);
putpixel (foodx+3,foody+3,7);
putpixel (foodx+3,foody+4,7);
putpixel (foodx+4,foody,7);
putpixel (foodx+4,foody+1,7);
putpixel (foodx+4,foody+2,7);
putpixel (foodx+4,foody+3,7);
putpixel (foodx+4,foody+4,7);

foodcount++;
valid = 1;

}
}

//Get future value of head in int variable futurex and futurey and calculate the logic

futurex = Snake.head_x;
futurey = Snake.head_y;

if (Snake.head_dir == lft)
{
futurex –;
}
if (Snake.head_dir == rgt)
{
futurex ++;
}
if (Snake.head_dir == up)
{
futurey –;
}
if (Snake.head_dir == dwn)
{
futurey ++;
}

futurepixel = getpixel(futurex,futurey);

// Food colision check and logic
if (futurepixel == 7)
{
foodcount –;
score++;
//Draw Current Score
fillellipse(20, 125, 5, 10);
sprintf (scorestring, “Score : %d”, score);
outtextxy (35,115, scorestring);
// Removes curren food
putpixel (foodgx,foodgy,0);
putpixel (foodgx,foodgy+1,0);
putpixel (foodgx,foodgy+2,0);
putpixel (foodgx,foodgy+3,0);
putpixel (foodgx,foodgy+4,0);
putpixel (foodgx+1,foodgy,0);
putpixel (foodgx+1,foodgy+1,0);
putpixel (foodgx+1,foodgy+2,0);
putpixel (foodgx+1,foodgy+3,0);
putpixel (foodgx+1,foodgy+4,0);
putpixel (foodgx+2,foodgy,0);
putpixel (foodgx+2,foodgy+1,0);
putpixel (foodgx+2,foodgy+2,0);
putpixel (foodgx+2,foodgy+3,0);
putpixel (foodgx+2,foodgy+4,0);
putpixel (foodgx+3,foodgy,0);
putpixel (foodgx+3,foodgy+1,0);
putpixel (foodgx+3,foodgy+2,00);
putpixel (foodgx+3,foodgy+3,0);
putpixel (foodgx+3,foodgy+4,0);
putpixel (foodgx+4,foodgy,0);
putpixel (foodgx+4,foodgy+1,0);
putpixel (foodgx+4,foodgy+2,0);
putpixel (foodgx+4,foodgy+3,0);
putpixel (foodgx+4,foodgy+4,0);

//food bonus
if (Snake.tail_dir == up)
{
for (i = 0; i<11;i++) putpixel (Snake.tail_x,Snake.tail_y+i,3);
Snake.tail_y +=10;

}
if (Snake.tail_dir == dwn)
{
for (i = 0; i<11;i++) putpixel (Snake.tail_x,Snake.tail_y-i,3);
Snake.tail_y -=10;

}
if (Snake.tail_dir == lft)
{
for (i = 0; i<11;i++)
putpixel (Snake.tail_x+i,Snake.tail_y,3);
Snake.tail_x +=10;

}
if (Snake.tail_dir == rgt)
{
for (i = 0; i<11;i++)
putpixel (Snake.tail_x-i,Snake.tail_y,3);
Snake.tail_x -=10;

}

}

// Boundry colosion and self colosion check
if (futurepixel == 3)
{

//Checks current score and saves if it is highest

if(score>hscore)
{
hscore_write();
}

setcolor(9);
outtextxy (280,170, “Game Over!”);
outtextxy (280,200,”Press any key to end!”);
if(score>hscore)
{
outtextxy (280,230,”And Congratulations! You are highest scorer!”);
}

getch();
exit(1);
}

}

void gameloop ()//main game
{
int delays;
while (1)
{
movesnake();
userinput();
gamephysics ();
delay(gamedelay);
}

}

int main()
{
hscore_read();
initwindow(700, 500,”Snake Game :) Best Engineering projects (bep)”,10);
initialdatas();
initialscreen();
gameloop();

}

One Thought to “Snake Game Using C”

  1. pooja adhikari

    Hello sir i am student of engineering and i want to submit snake game using c in my semester project. Is is code is running?

Leave a Comment