Return to site

Snake Game In C Language

broken image


Report on snake game 1. Introduction The following is an example game written in C based on the game called 'snake' which has been around since the earliest days of home computing and has re-emerged in recent years on mobile phones. It isn't the world's greatest game, but it does give you an idea of what you can achieve with a relatively simple C program, and perhaps the basis by which to.

In this page, we are sharing the source code of a 'simple snake game' developed on C# programming language as a windows form application. Here are the most relevant keywords about this game: Graphics, Timer, Double Buffering, First Game, Game Loop.

You can download the source code of the snake game from here. Unarc dll error code 14 free download. Please download the project, and build it with Visual Studio to run the application.

Note that, the application is written in C# programming language, using old style Windows Form Applications as UI components. Try to use WPF (windows presentation foundation) for better development and maintainability experiences.

How to improve the game?

1. Level: Update timer1.Interval to be 20 smaller when the size of the snake reaches multiplicatives of 10.

2. Walls: Free neve 1073 vst download. Add 3-5 blue points that represent walls. When snake hit them, it is gameover.

3. Score: Eating a food should have an award. Actually, reaching the food faster should be rewarded. When a food appears, you can start countnig from 20 to 1 in each step and add the value to the score.

4. Bonus food: Sometimes you may create other colors of foods that have bonus snake size and points.

5. Scores: Save the best scores and create a Top 10 list.

User InterfaceJust like KeroVee, you get an analyzer on the left side of the user interface of GSnap.This analyzer helps you to keep eye on every correction it does on the incoming vocal. You can control the GSnap's effect on the sound by Gate knob. This style analyzer is very useful when you correct the note via MIDI.This keyboard also helps you to select and de-select the notes on the scale for fine-tuning and adjusting the autotune.A scale more select knob is given just above the analyzer where you can select the right scale for your song.There are three other sections are given for better adjustment.Detection section: This section helps you to adjust the input frequencies, Gate and speed of the plugin. Gate is the best feature I like in this plugin. T pain plugin free. As well as the speed also helps you to make your vocal more natural.Correction section: In this section, you find 4 knobs (Threshold, amount, attack and ) and the choice to select between MIDI and Manual selection.MIDI section: This section is used to adjust the incoming MIDI signals.


added 5 years 8 months ago

Snake game in C language .I have simplified the program which you can easily understand(see the explanation part). The program for snake game is of 235 lines.In this game you have to direct snake towards its food which randomly appears on the screen more it will eat more it will grow.Using arrow keys you can direct it. Before reading following explanation please read the explanation of Cricket game, Puzzle Game and stop watch because many of the concepts such as scan codes ,kbhit() function ,how to generate random numbers , I have explained their.

SOURCE CODE

Snake Game In C Language
#include
#include
int m,n,a;
void boundary();
int snake(int x[1500],int y[1500],char d[1500])
{
int i;
long score=(a-5)*100;
if(x[0]m&&y[0]n)
{
take:
gotoxy(m,n);
printf(' ');
m=rand()%71+3;
n=rand()%17+4;
for(i=0;i if(x[i]m&&y[i]n)
goto take;
a++;
}
if(a>=1406)
{
gotoxy(30,12);
printf('Congratulation You Won The Game');
gotoxy(32,12);
printf('Your Score : %ld',score);
getch();
return 1;
}
gotoxy(m,n);
printf('%c',5);
for(i=0;i {
gotoxy(x[i],y[i]);
printf('%c',d[i]);
}
gotoxy(x[i],y[i]);
printf(' ');
gotoxy(3,23);
printf(' Your Score : %ld',score);
for(i=1;i if(x[0]x[i]&&y[0]y[i])
goto out;
if(x[0]<3||x[0]>75||y[0]<4||y[0]>21)
{
out:
gotoxy(34,10);
printf('GAME OVER');
gotoxy(32,12);
printf('Your Score : %ld',score);
delay(1000);
getch();
return 1;
}
return 0;
}
void boundary()
{
int i,ab=3,de=22,cd=2,dc=1;
gotoxy(35,2);
printf('SNAKE GAME');
gotoxy(2,1);
printf('%c',201);
gotoxy(76,1);
printf('%c',187);
gotoxy(2,25);
printf('%c',200);
gotoxy(76,25);
printf('%c',188);
for(i=0;i<=72;i++,ab++)
{
gotoxy(ab,dc);
printf('%c',205);
gotoxy(ab,dc+2);
printf('%c',205);
gotoxy(ab,dc+21);
printf('%c',205);
gotoxy(ab,dc+24);
printf('%c',205);
}
de=76,ab=2;
gotoxy(33,23);
printf('BkSp Pausett Esc Quit') ;
printf('n made by:mohammed ujjain wala');
for(i=0;i<23;i++,cd++)
{
gotoxy(ab,cd);
printf('%c',186);
gotoxy(de,cd);
printf('%c',186);
}
gotoxy(2,3);
printf('%c',204);
gotoxy(2,22);
printf('%c',204);
gotoxy(76,3);
printf('%c',185);
gotoxy(76,22);
printf('%c',185);
}
void main()
{
int i,key,set,max=1406,x[1500],y[1500],speed=200,select;
char d[1500];
randomize();
m=rand()%71+2;
n=rand()%17+2;
clrscr();
printf('nnnnnttt MOHAMMED UJJAIN WALAnntttt PRESENTS');
printf('nnnttttSNAKE GAME');
printf('nnnnpress any key to continue');
getch();
while(1)
{
phir:
clrscr();
printf('nntttSNAKE GAME');
printf('nnnt1.) STARTtttpress 1nnt2.) INSTRUCTIONSttpress 2nnt3.) SETTINGStttpress 3nnt4.) EXITtttpress 4 ');
scanf('%d',&select);
clrscr();
boundary();
switch(select)
{
case 1:
for(i=0;i d[i]='*',x[i]=i+40,y[i]=10;
d[0]=2,key=75,a=5;
while(1)
{
if(kbhit())
{
key=getch();
if(!key)
key=getch();
}
switch(key)
{
case 75:
if(x[0]-1x[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
x[0]-=1,set=1;
if(snake(x,y,d))
goto phir;
break;
case 77:
if(x[0]+1x[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
x[0]++,set=2;
if(snake(x,y,d))
goto phir;
break;
case 72:
if(y[0]-1y[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
y[0]--,set=3;
if(snake(x,y,d))
goto phir;
break;
case 80:
if(y[0]+1y[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
y[0]++,set=4;
if(snake(x,y,d))
goto phir;
break;
case 27:
goto phir;
case 32:
snake(x,y,d);
gotoxy(30,12);
printf('press any key to continue..');
getch();
clrscr();
boundary();
default:
same:
printf('a');
if(set1)
key=75;
else if(set2)
key=77;
else if(set3)
key=72;
else
key=80;
break;
}
if(kbhit())
delay(40);
else
delay(speed);
}
case 2:
clrscr();
printf('ttttHOW TO PLAYnnOur snake is hungry and you have to direct it toward its food ,more the snake will eat more it will grow .Remember snake should not collide with walls or itself or game will be over.nUse arrow key to move the snake');
printf('nnnttt ****ENJOY THE GAME****nnn press any key to continue.......');
getch();
goto phir;
case 3:
clrscr();
printf('ttttSETTINGSnEnter the speed level (1-5): ');
spee:
scanf('%d',&speed);
if(speed5)
speed=100;
else if(speed4)
speed=200;
else if(speed2)
speed=400;
else if(speed1)
speed=500;
else if(speed3)
speed=300;
else
{
clrscr();
printf('ttttSETTINGSnPlease enter the no.between 1-5 :');
goto spee;
}
printf('nYour speed level has been setnpress any key to continue');
getch();
goto phir;
case 4:
exit(0);
}
}
}
Game
#include
#include
int m,n,a;
void boundary();
int snake(int x[1500],int y[1500],char d[1500])
{
int i;
long score=(a-5)*100;
if(x[0]m&&y[0]n)
{
take:
gotoxy(m,n);
printf(' ');
m=rand()%71+3;
n=rand()%17+4;
for(i=0;i if(x[i]m&&y[i]n)
goto take;
a++;
}
if(a>=1406)
{
gotoxy(30,12);
printf('Congratulation You Won The Game');
gotoxy(32,12);
printf('Your Score : %ld',score);
getch();
return 1;
}
gotoxy(m,n);
printf('%c',5);
for(i=0;i {
gotoxy(x[i],y[i]);
printf('%c',d[i]);
}
gotoxy(x[i],y[i]);
printf(' ');
gotoxy(3,23);
printf(' Your Score : %ld',score);
for(i=1;i if(x[0]x[i]&&y[0]y[i])
goto out;
if(x[0]<3||x[0]>75||y[0]<4||y[0]>21)
{
out:
gotoxy(34,10);
printf('GAME OVER');
gotoxy(32,12);
printf('Your Score : %ld',score);
delay(1000);
getch();
return 1;
}
return 0;
}
void boundary()
{
int i,ab=3,de=22,cd=2,dc=1;
gotoxy(35,2);
printf('SNAKE GAME');
gotoxy(2,1);
printf('%c',201);
gotoxy(76,1);
printf('%c',187);
gotoxy(2,25);
printf('%c',200);
gotoxy(76,25);
printf('%c',188);
for(i=0;i<=72;i++,ab++)
{
gotoxy(ab,dc);
printf('%c',205);
gotoxy(ab,dc+2);
printf('%c',205);
gotoxy(ab,dc+21);
printf('%c',205);
gotoxy(ab,dc+24);
printf('%c',205);
}
de=76,ab=2;
gotoxy(33,23);
printf('BkSp Pausett Esc Quit') ;
printf('n made by:mohammed ujjain wala');
for(i=0;i<23;i++,cd++)
{
gotoxy(ab,cd);
printf('%c',186);
gotoxy(de,cd);
printf('%c',186);
}
gotoxy(2,3);
printf('%c',204);
gotoxy(2,22);
printf('%c',204);
gotoxy(76,3);
printf('%c',185);
gotoxy(76,22);
printf('%c',185);
}
void main()
{
int i,key,set,max=1406,x[1500],y[1500],speed=200,select;
char d[1500];
randomize();
m=rand()%71+2;
n=rand()%17+2;
clrscr();
printf('nnnnnttt MOHAMMED UJJAIN WALAnntttt PRESENTS');
printf('nnnttttSNAKE GAME');
printf('nnnnpress any key to continue');
getch();
while(1)
{
phir:
clrscr();
printf('nntttSNAKE GAME');
printf('nnnt1.) STARTtttpress 1nnt2.) INSTRUCTIONSttpress 2nnt3.) SETTINGStttpress 3nnt4.) EXITtttpress 4 ');
scanf('%d',&select);
clrscr();
boundary();
switch(select)
{
case 1:
for(i=0;i d[i]='*',x[i]=i+40,y[i]=10;
d[0]=2,key=75,a=5;
while(1)
{
if(kbhit())
{
key=getch();
if(!key)
key=getch();
}
switch(key)
{
case 75:
if(x[0]-1x[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
x[0]-=1,set=1;
if(snake(x,y,d))
goto phir;
break;
case 77:
if(x[0]+1x[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
x[0]++,set=2;
if(snake(x,y,d))
goto phir;
break;
case 72:
if(y[0]-1y[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
y[0]--,set=3;
if(snake(x,y,d))
goto phir;
break;
case 80:
if(y[0]+1y[1])
goto same;
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
y[0]++,set=4;
if(snake(x,y,d))
goto phir;
break;
case 27:
goto phir;
case 32:
snake(x,y,d);
gotoxy(30,12);
printf('press any key to continue..');
getch();
clrscr();
boundary();
default:
same:
printf('a');
if(set1)
key=75;
else if(set2)
key=77;
else if(set3)
key=72;
else
key=80;
break;
}
if(kbhit())
delay(40);
else
delay(speed);
}
case 2:
clrscr();
printf('ttttHOW TO PLAYnnOur snake is hungry and you have to direct it toward its food ,more the snake will eat more it will grow .Remember snake should not collide with walls or itself or game will be over.nUse arrow key to move the snake');
printf('nnnttt ****ENJOY THE GAME****nnn press any key to continue.......');
getch();
goto phir;
case 3:
clrscr();
printf('ttttSETTINGSnEnter the speed level (1-5): ');
spee:
scanf('%d',&speed);
if(speed5)
speed=100;
else if(speed4)
speed=200;
else if(speed2)
speed=400;
else if(speed1)
speed=500;
else if(speed3)
speed=300;
else
{
clrscr();
printf('ttttSETTINGSnPlease enter the no.between 1-5 :');
goto spee;
}
printf('nYour speed level has been setnpress any key to continue');
getch();
goto phir;
case 4:
exit(0);
}
}
}

OUTPUT


EXPLANATION

After removing all the exception handling cases and unnecessary things, simplified program is given below
#include
#include
int m,n,a=5;
void snake(int x[1500],int y[1500],char d[1500])
{
int i;
if(x[0]m&&y[0]n)
{
m=rand()%70+1;
n=rand()%20+1;
a++;
}
gotoxy(m,n);
printf('%c',5);
for(i=0;i {
gotoxy(x[i],y[i]);
printf('%c',d[i]);
}
}
void main()
{
int i,key=75,x[1500],y[1500];
char d[1500];
randomize();
m=rand()%70+1;
n=rand()%20+1;
for(i=0;i<1400;i++)
d[i]='*',x[i]=i+40,y[i]=10;
d[0]=2;
while(1)
{
clrscr();
if(kbhit())
{
key=getch();
if(!key)
key=getch();
}
for(i=a;i>0;i--)
y[i]=y[i-1],x[i]=x[i-1];
if(key75)
x[0]-=1;
else if(key77)
x[0]++;
else if(key72)
y[0]--;
else if(key80)
y[0]++;
else
exit(0);
snake(x,y,d);
delay(200);
}
}
Is it simple, yes ?Ok than let us understand the program.
I have already told you how random numbers are generated .Now this randomly generated numbers will be stored in m and n, this will decide the position of our food on the screen .Thus m and n simply holds the position of food. We will draw snake body using character type array d[1500] and to print whole body of snake at proper position we will use two integer type arrays x[1500] and y[1500] i.e. x[],y[] both holds the coordinates of whole body of snake. For example x[0] and y[0] holds the coordinates of face of snake similarly x[1] and y[1] hold the coordinate of next body part of snake.
Now we have given 1 in while loop condition so it will run infinite times .Now to move the snake left right, up or down, what we actually do is first copy the coordinates of previous body part to next and second we will increment or decrement the value x[0] or y[0] .Thus the whole body will go to the path through which its face was passed .This was the main logic of the program
Initially we have set key=75 now when no key is hit than if(kbhit()) becomes false and if(key75) becomes true so x[0] is decremented i.e. snake will move to left.Now snake() function is called. Base address of the three arrays x[],y[] and d[] is passed. Then this function checks whether the snake has eaten the food or not by simply comparing the coordinates of food and snake's face ,if yes it simply prints the food in other position and increment the value of a i.e. snake becomes bigger in size. Now using gotoxy() function we will print the food and snake on the output screen.
After that delay() functions is called which suspends the program execution for 100 milliseconds. Thus after a delay of every 100 milliseconds the snake position is updated ,which seems that snake is moving.
Now when you press any arrow key if(kbhit()) becomes true and the key which is been hit is retrieved by getch() function here we will use scan codes of the arrow keys. Now suppose you press UP arrow key than 72 will get stored in key and the program will run for that value of key again and again until another key is pressed i.e. our snake will move in upward direction until you give him another direction.

Snake Game Code In C Language

So in this way program execute now you can add many others things to it such as by drawing walls, giving conditions in which the game is over and also giving option to set the speed of snake, to pause the game and many other things to make it more attractive.
Any questions regarding to program please write in comments.

Snake Game In C Language Mini Project For Engineers


RELATED POST:-



broken image