Snake game in c++
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
int i;
void snkshp();
void food(int,int,int);
void lv1(int);
void lv2(int);
void lv3(int);
void loss();
void win();
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
clock_t start,end,r;
//////////////////////////////////////////////////////////////////////////////
int x,y,j,x1,y1,tail,scr=0,xx=290,yy=160,fc=1,tt=0;
const int up=72,dw=80,ls=75,rs=77,ext=27;
char ch;
tail=5;
int a[1000],b[1000];
j=y=x=0;
/////////////////////////////////////////////////////////////////////////////
setcolor(WHITE);
rectangle(210,100,410,130);
rectangle(110,10,510,70);
settextstyle(4,0,8);
setcolor(RED);
outtextxy(100,200,"WELCOME");
settextstyle(4,0,4);
setcolor(3);
cleardevice();
///////////////////////////////////////////////////////////////////////////////////
char key;
do
{
snkshp();
settextstyle(0,0,0);
//////////////////////////////////////////////////////////////////////////////////
// if(kbhit());
start=clock();
key=getch();
if(key==up)
{ y-=10;}
if(key==dw)
{ y+=10;}
if(key==ls)
{ x-=10;}
if(key==rs)
{ x+=10;}
if(i>=12000)
{i=0;}
a[j]= x1=200+x;
b[j]= y1=200+y;
/////////////////////////////////////////////////////////////////////////////
if(x1==30||x1==460||y1==30||y1==390)
{sound(3);
loss();
delay(1);
break;}
//////////////////////////////////////////////////////////////////////////////'
if(x1==xx&&y1==yy)
{ scr+=2;
fc+=1;
xx-=100;
tail+=3;
if(fc>=15)
{fc=8;}
if(x1==390&&y1==260)
scr+=4;
for( i=10; i<=400; i+=10)
{
randomize();
random(xx);
random(yy);
if(xx<=25||yy<=35)
{xx=300+i;
yy=200+i; }
if(xx>=450||yy>=370)
{ xx+=i;
yy-=i;}
}
}
/////////////////////////////////////////////////////////////////////////////////
int level=0;
if(scr>=5)
{level+=1;
if(level==1)
{lv1(fc);tt+=1;}
if(x1==150&&y1>=80&&y1<=170)
{loss(); exit(0);}
}
//////////////////////////////////////////////////////////////////////////////////
if(scr>=15)
{
level+=1;
}
gotoxy(70,5);
cout<<scr;
gotoxy(70,10);
cout<<level;
food(fc,xx,yy);
if(level==2)
{ lv2(fc);tt+=1;
if(x1==220&&y1>=110&&y1<=200||y1==110&&x1>=230&&x1<=320||x1==320&&y1>=120&&y1<=200||x1==270&&y1>=130&&y1<=180||y1==200&&x1>=240&&x1<=320)
{loss();
break;}
}
if(scr>=25)
{level+=1;}
if(level==3)
{lv3(fc);tt+=1;
if(x1==320&&y1>=130&&y1<=220||y1==130&&x1>=330&&x1<=420||x1==420&&y1>=140&&y1<=220||x1==370&&y1>=150&&y1<=210||y1==220&&x1>=340&&x1<=420)
{loss();
break;}
}
end=clock();
r=end-start;
if(r>=30-tt)
{ loss();
break;}
gotoxy(70,25);
cout<<end;
if(scr>=40)
{ win();break;}
//////////////////////////////////////////////////////////////////////////////////////
nosound();
//////////////////////////////////////////////////////////////////////////////////
setcolor(14);
outtextxy(x1,y1,"X");
setcolor(0);
outtextxy(a[j-tail],b[j-tail],"X");
j++;
}
while (key !=ext);
/////////////////////////////////////////////////////////////////////////////
delay(100);
settextstyle(0,0,0);
setcolor(9);
outtextxy(510,390,"Best of Luck");
settextstyle(0,0,0);
outtextxy(180,10,"TRY AGAIN ? y/n");
ch=getche();
if(ch=='y'||ch=='Y')
{
return main();
}
else
exit(0);
closegraph();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
void food(int c,int x,int y)
{setcolor(c);
outtextxy(x,y,"0");
}
//////////////////////////////////////////////////////////////////////////////
void lv1(int c)
{
for(i=10;i<=100; i+=10)
{ setcolor(c);
settextstyle(0,0,0);
outtextxy(150,70+i,"#");}
}
///////////////////////////////////////////////////////////////////////////////
void lv2(int c)
{ setcolor(c);
for(i=10; i<=100; i+=10)
{ outtextxy(220,100+i,"*");
outtextxy(220+i,110,"*");}
for(i=10;i<=90; i++)
{ outtextxy(320,110+i,"*");
outtextxy(230+i,200,"*");}
for(i=10; i<=60; i++)
{outtextxy(270,120+i,"*");}
}
///////////////////////////////////////////////////////////////////////////////
void lv3(int c)
{ setcolor(c);
for(i=10; i<=100; i+=10)
{ outtextxy(320,120+i,"*");
outtextxy(320+i,130,"*");}
for(i=10;i<=90; i++)
{ outtextxy(420,130+i,"*");
outtextxy(330+i,220,"*");}
for(i=10; i<=60; i++)
{outtextxy(370,140+i,"*");}
}
/////////////////////////////////////////////////////////////////////////////////
void loss()
{
for(int cc=1; cc<8; cc++)
{
setcolor(cc);
settextstyle(3,0,8);
outtextxy(100,200,"You Loss");
sound(cc+2000);
sound(300);
delay(300);
nosound();
}
}
////////////////////////////////////////////////////////////////////////////////////
void win()
{
for(int cc=1; cc<8; cc++)
{
setcolor(cc);
settextstyle(3,0,8);
outtextxy(100,200,"You Win");
sound(cc+2000);
sound(300);
delay(300);
nosound();
}
}
////////////////////////////////////////////////////////////////////////////////////
void snkshp()
{
setbkcolor(0);
setcolor(0);
settextstyle(0,0,0);
setlinestyle(3,2,5);
setcolor(RED);
rectangle(20,21,470,400);
setcolor(6);
rectangle(30,31,460,390);
setcolor(RED);
rectangle(500,22,600,100);
rectangle(500,22,600,40);
rectangle(500,110,600,200);
rectangle(500,110,600,130);
rectangle(500,210,600,360);
rectangle(500,210,600,230);
rectangle(500,380,600,400);
rectangle(160,2,350,20);
setcolor(13);
outtextxy(525,27,"SCORE");
outtextxy(526,115,"LEVEL");
outtextxy(527,217,"KEYS");
setcolor(8);
outtextxy(505,240,"Right Arrow");
outtextxy(505,260,"Left Arrow");
outtextxy(505,280,"Up Arrow");
outtextxy(505,300,"Down Arrow");
outtextxy(505,320,"ESC to exit");
setcolor(4);
outtextxy(505,335," Time ");
setcolor(8);
outtextxy(505,345,"10sec+score");
outtextxy(505,387,"Time:");
setcolor(21);
settextstyle(4,0,8);
outtextxy(29,390,"SNAKE GAME");
}
Copy this code and save in notepad and save as .cpp extension in bin directory then run with turbo C
No comments:
Post a Comment