Snake Game by C program by R4R Team

program-

#include< iostream.h>
#include< math.h>
#include< stdio.h>
#include< stdlib.h>
#include< conio.h>
#include< graphics.h>
#include< dos.h>
inline void snake(int);
inline void pause(),start(),start1(),outer(),score(int);
long int x[100],y[100],i,j=0,z,f=6,x1=300,y1=300,scor=0;
void main()
{
clrscr();
x[0]=150,y[0]=200;
for(z=1;z< 6;z++) {
x[z]=x[z-1]-4;
y[z]=200;
}
char ch='s';
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
start();
abc:
cleardevice();
start1();
setbkcolor(GREEN);
while(1)
{
outer();
setcolor(WHITE);
for(j=0;j< f;j++) {
snake(j);
}
circle(x1,y1,2);
delay(20);
if(kbhit())
ch=getch();
if(ch=='a'){
x[0]--;
for(i=1;i< f;i++){
if(y[i]==y[i-1])
x[i]--;
else
if(y[i]< y[i-1])
y[i]++;
else
y[i]--;
}
}
if(ch=='i')
goto abc;
if(ch=='z'){
y[0]++;
for(i=1;i< f;i++) {
if(x[i]==x[i-1])
y[i]++;
else
if(x[i]< x[i-1])
x[i]++;
else
x[i]--;
}
}
if(ch=='s')
continue;
if(ch=='d'){
x[0]++;
for(i=1;i< f;i++){
if(y[i]==y[i-1])
x[i]++;
else
if(y[i]< y[i-1])
y[i]++;
else
y[i]--;
}
}
if(ch=='w') {
y[0]--;
for(i=1;i< f;i++){
if(x[i]==x[i-1])
y[i]--;
else
if(x[i]< x[i-1])
x[i]++;
else
x[i]--;
}
}
if(ch=='l')
break;
if(ch=='p')
pause();
if(pow((x1-x[0])*(x1-x[0])+(y1-y[0])*(y1-y[0]),0.5)< =4){
scor++;
again:
x1=rand()%385+1;
y1=rand()%444+1;
if((x1>100 && x1< 350)&&(y1>100 && y1< 350)){}
else
goto again;
f++;
if(x[j-1]!=x[j-2]){
y[j]=y[j-1];
x[j]=x[j-1]-4;}
else{
y[j]=y[j-1]-4;
x[j]=x[j-1];
}
}
if(x[0]==102 || x[0]==398 || y[0]==102 || y[0]==348){
delay(1000);
break;
}
cleardevice();
}
cleardevice();
score(scor);
getch();
closegraph();
}
void snake(int j) {
circle(x[j],y[j],2);
}
void pause(){}
void start()
{
int p=0;
settextstyle(0,HORIZ_DIR,5);
outtextxy(80,170,"Snake n GAME");
settextstyle(3,HORIZ_DIR,3);
outtextxy(250,350,"Loading...");
for(p=0;p< 440;p++){
circle(70+p,400,4);
delay(10);
}
delay(1000);
}
void start1(){
char c;
while(1){
outtextxy(100,200,"-> Press < s> for start");
outtextxy(100,250,"-> Press < p> for pause");
outtextxy(100,350,"-> Press < esc> key to quit the gamen");
outtextxy(100,300,"-> Use arroy key to move left right up down");
settextstyle(1,HORIZ_DIR,3);
outtextxy(180,100,"....INSTRUCTIONS....");
c=getch();
delay(500);
if(c=='s')
break;
}
}
void outer()
{
setcolor(RED);
line(100,100,400,100);
line(100,100,100,350);
line(100,350,400,350);
line(400,100,400,350);
}
void score(int n){
settextstyle(1,HORIZ_DIR,8);
outtextxy(120,100,"GAME OVER");
settextstyle(1,HORIZ_DIR,3);
outtextxy(200,230,"Your score is ");
char mgs[128];
sprintf(mgs,"%d",n);
outtextxy(200,270,mgs);
}

output:








Leave a Comment: