Sunday 3 February 2013


My Snake Game:-

#include<graphics.h>
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<string.h>

failmsg1();
welcome();
show();
help();
starmsg1();
starmsg2();
starmsg3();
starmsg4();
star1(int,int);
star2(int,int);
star3(int,int);
star4(int,int);
rect_right(int,int);
rect_left(int,int);
rect_down(int,int);
rect_up(int,int);
boxbg(int,int,int,int,int,int,int,int,int);

int i,j,x,y,r=7,a,b,count,xd,m=300,p=100,key,gt=0,gm,score;
int round=0,mycolor=1,sec=0;
char msg[80],msga1[180],msga2[180],msga3[180],msga4[180],msga5[180],msg2[50],msgtime[55],n=0,af,name;

int main()
{

initgraph(&gt,&gm," ");//C:\\TurboC++\\Disk\\TurboC3\\BGI
x=getmaxx()/2;y=getmaxy()/2;
a=random(getmaxx());b=random(getmaxy());

sprintf(msgtime,"[%d]",sec);

sprintf(msg,"Score: %d ",count);
sprintf(msga1," F1 - HELP");
sprintf(msga2," F2 - SAVE");
sprintf(msga3," F3 - VIEW");
sprintf(msga4," ESC - QUIT");
sprintf(msga5," ENTER - RESUME");
sprintf(msg2," Do you want to Quit(y/n):___");

setbkcolor(15);
setcolor(1);
setfillstyle(1,4);

welcome();

/* settextstyle(3,0,2);
boxbg(15,15,80,50,14,9,3,1,1);
outtextxy(15,15,msgtime);
*/
boxbg(100,100,500,110,4,9,4,1,1);
boxbg(100,400,500,410,4,9,4,1,1);

boxbg(495,15,610,55,14,9,2,5,5);

settextstyle(0,0,1);
outtextxy(130,465,"|F1-HELP|F2-SAVE|F3-VIEW|ESC-QUIT|ENTER-RESUME|");
setfillstyle(1,4);
settextstyle(1,0,3);
/* rect_right(x+10,y);
rect_left(x,y);
rect_down(x,y+10);
rect_up(x,y);
*/
bar(x,y,x+10,y+10);
setcolor(4);
setfillstyle(1,random(14));
pieslice(random(getmaxx()),random(getmaxy()),0,360,r);
setfillstyle(1,4);
setcolor(1);
outtextxy(500,20,msg);
do
{
setfillstyle(1,4);
key=getch();
sprintf(msg,"Score: %d ",count);
/*
sprintf(msgtime,"[%d]",sec);
settextstyle(3,0,2);
outtextxy(15,15,msgtime);

*/
switch(key)
{
case 27:
cleardevice();
boxbg(225,350,578,390,14,9,2,5,5);
outtextxy(200,350,msg2);
n=getch();
if(n==89||n==121)
{
nosound();
exit(1);
}
else
{
nosound();
continue;
}

case 60:
cleardevice();
clrscr();
settextstyle(3,0,3);
outtextxy(200,400,"Do you want to save(y/n)...");
n=getch();
if(n==89||n==121)
{
FILE *fp;
fp=fopen("c:\\SmartGame.txt","a");
printf("Enter ur name :");
while((name=getchar())!='\n');
fprintf(fp,"%s %d",name,count);
fclose(fp);
}
nosound();
continue;

case 61:
cleardevice();
clrscr();
outtextxy(200,400,"Your score view :");
FILE *fp;
fp=fopen("c:\\SmartGame.txt","r");
printf("Name Score");
for(i=0;i<4;i++)
{
fprintf(stdout,"\n\n");
fscanf(fp,"%s %d",name,&count);
fprintf(stdout,"%s %d",name,count);
}
fclose(fp);
nosound();
continue;

case 59:
cleardevice();
do
{
sound(random(2000)+1000);
delay(200);
boxbg(5,80,280,350,14,9,2,5,5);
outtextxy(0,100,msga1);
outtextxy(0,150,msga2);
outtextxy(0,200,msga3);
outtextxy(0,250,msga4);
outtextxy(0,300,msga5);
boxbg(320,80,520,350,14,9,2,5,5);
help();
}while(!kbhit());
nosound();
continue;

case 75:
x-=10;
sound(x+1000);
setfillstyle(1,random(14));
rect_left(x,y);
setfillstyle(1,4);
nosound();
continue;

case 72:
y-=10;
sound(y+1000);
setfillstyle(1,random(14));
rect_up(x,y);
setfillstyle(1,4);
nosound();
continue;

case 77:
x+=10;
sound(x+1000);
setfillstyle(1,random(14));
rect_right(x+10,y);
setfillstyle(1,4);
nosound();
continue;

case 80:
y+=10;
sound(y+1000);
setfillstyle(1,random(14));
rect_down(x,y+10);
setfillstyle(1,4);
nosound();
continue;

default:
if(((x>=100 && x<=500) && (y>=90 && y<=110)) || ((x>=100 && x<=500) && (y>=390 && y<=410)))
{
failmsg1();
x=random(500);
y=random(500);
if(((x>=100 && x<=500) && (y>=90 && y<=110)) || ((x>=100 && x<=500) && (y>=390 && y<=410)))
{
x=random(100);
y=random(100);
}
}
if(((a>=100 && a<=500) && (b>=90 && b<=110)) || ((a>=100 && a<=500) && (b>=390 && b<=410)))
{
a=random(getmaxx()-10);
b=random(getmaxy()-10);
if(((a>=100 && a<=500) && (b>=90 && b<=110)) || ((a>=100 && a<=500) && (b>=390 && b<=410)))
{
a=random(getmaxx()-10);
b=random(getmaxy()-10);
}
}

if(y>=b-15 && y<=b+10)
{
if(x>=a-15 && x<=a+10)
{
for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(1);
}
a=random(getmaxx()-10);
b=random(getmaxy()-10);
++count;
}
}

show();
bar(x,y,x+10,y+10);
nosound();
}
}while(1);
return 0;

}

show()
{
cleardevice();
setcolor(4);

// boxbg(15,15,80,50,14,9,3,1,1);

boxbg(495,15,610,55,14,9,2,5,5);
boxbg(100,100,500,110,4,9,4,1,1);
boxbg(100,400,500,410,4,9,4,1,1);
round++;
settextstyle(0,0,1);
if(round<getmaxx())
{
setcolor(mycolor);
outtextxy(round++,465,"|F1-HELP|F2-SAVE|F3-VIEW|ESC-QUIT|ENTER-RESUME|");
}
else
{
round=0;
mycolor++;
if(mycolor==7)++mycolor;
if(mycolor==14)mycolor=1;
}
settextstyle(1,0,3);
setcolor(5);
rectangle(495,15,610,55);
setcolor(1);
outtextxy(500,20,msg);
setcolor(4);
setfillstyle(1,random(14));
pieslice(a,b,0,360,r);
setfillstyle(1,4);
setcolor(1);

if(count==10)
starmsg1();
if(count>=10)
star1(0,0);

if(count==20)
starmsg2();
if(count>=20)
star2(0,0);

if(count==30)
starmsg3();
if(count>=30)
star3(0,0);

if(count==40)
starmsg4();
if(count>=40)
star4(0,0);

if(count>=50)
{
cleardevice();
setcolor(4);
settextstyle(4,0,3);
boxbg(80,120,500,250,14,9,2,5,5);
outtextxy(190,150,"Congratulations !");
setcolor(1);
settextstyle(1,0,3);
outtextxy(100,200,"**** You have WON the game ****");
settextstyle(1,0,1);
boxbg(150,350,500,400,14,9,2,5,5);
outtextxy(160,350,"Now game will be initialize.....");
delay(5000);
cleardevice();
count=0;
}

if(x<0)
x=getmaxx()-1;
else
if(x>=getmaxx())
x=0;
else
if(y<0)
y=getmaxy()-1;

if(y>=getmaxy())
y=0;

}

rect_right(int x,int y)
{
int poly[]={x,y,x,y+10,5+x,y+5};
fillpoly(3,poly);
}

rect_left(int x,int y)
{
int poly[]={x,y,x,y+10,x-5,y+5};
fillpoly(3,poly);
}

rect_down(int x,int y)
{
int poly[]={x,y,10+x,y,5+x,10+y};
fillpoly(3,poly);
}

rect_up(int x,int y)
{
int poly[]={x,y,10+x,y,5+x,y-10};
fillpoly(3,poly);
}

welcome()
{ settextstyle(0,0,3);
boxbg(50,50,getmaxx()-50,getmaxy()-50,14,8,2,5,5);

setcolor(13);
outtextxy(160,150,"***");

setcolor(1);
outtextxy(220,150," WELCOME ");

setcolor(11);
outtextxy(420,150,"***");
delay(2000);

setcolor(1);
outtextxy(300,200,"IN");
delay(2000);

outtextxy(200,250,"SMART GAME");
delay(2000);

setcolor(4);
settextstyle(3,0,3);
outtextxy(340,310,"|Made_By:_Akhilesh|");
settextstyle(1,0,1);
outtextxy(336,340,"akhil.bca08@gmail.com");

setcolor(6);
settextstyle(4,0,3);
outtextxy(200,400,"Press any key to start...");

getch();
cleardevice();
}
help()
{
setcolor(4);
boxbg(322,82,518,348,14,9,2,5,5);
settextstyle(0,0,1);
outtextxy(340,100,"|_____WELCOME_____|");
outtextxy(340,130,"|________in_______|");
outtextxy(340,160,"|____Smart Game___|");
outtextxy(340,190,"|_________________|");
outtextxy(340,220,"|_________________|");
outtextxy(340,250,"|_________________|");
outtextxy(340,280,"|_________________|");
outtextxy(340,310,"|Made_By:_Akhilesh|");
setcolor(1);
settextstyle(1,0,3);

}

starmsg1()
{
for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(200);
settextstyle(4,0,3);
boxbg(120,130,450,300,14,9,2,5,5);
outtextxy(210,150,"Congratulations !");
settextstyle(3,0,1);
outtextxy(150,200,"**** You have won STAR ****");
star1(250,230);
settextstyle(3,0,3);
}
nosound();
// cleardevice();
count++;
}

star1(int x,int y)
{

int poly1[]={x+20,y+20,x+40,y+20,x+30,y+40};
int poly2[]={x+20,y+35,x+40,y+35,x+30,y+15};
setcolor(1);
setfillstyle(1,1);
fillpoly(3,poly1);
fillpoly(3,poly2);
setcolor(1);
setfillstyle(1,4);
}

starmsg2()
{
for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(200);
settextstyle(4,0,3);
boxbg(120,130,450,300,14,9,2,5,5);
outtextxy(210,150,"Congratulations !");
settextstyle(3,0,1);
outtextxy(150,200,"**** You have won STAR ****");
star2(220,230);
settextstyle(3,0,3);
}
// cleardevice();
count++;
}

star2(int x,int y)
{ int poly3[]={x+60,y+20,x+80,y+20,x+70,y+40};
int poly4[]={x+60,y+35,x+80,y+35,x+70,y+15};
setcolor(2);
setfillstyle(1,2);
fillpoly(3,poly3);
fillpoly(3,poly4);
setcolor(1);
setfillstyle(1,4);
}

starmsg3()
{
for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(200);
settextstyle(4,0,3);
boxbg(120,130,450,300,14,9,2,5,5);
outtextxy(210,150,"Congratulations !");
settextstyle(3,0,1);
outtextxy(150,200,"**** You have won STAR ****");
star3(160,230);
settextstyle(3,0,3);
}
// cleardevice();
count++;
}

star3(int x,int y)
{ int poly5[]={x+100,y+20,x+120,y+20,x+110,y+40};
int poly6[]={x+100,y+35,x+120,y+35,x+110,y+15};
setcolor(3);
setfillstyle(1,3);
fillpoly(3,poly5);
fillpoly(3,poly6);
setcolor(1);
setfillstyle(1,4);
}

starmsg4()
{

for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(200);
settextstyle(4,0,3);
boxbg(120,130,450,300,14,9,2,5,5);
outtextxy(210,150,"Congratulations !");
settextstyle(3,0,1);
outtextxy(150,200,"**** You have won STAR ****");
star4(140,230);
settextstyle(3,0,3);
}
// cleardevice();
count++;
}

star4(int x,int y)
{ int poly7[]={x+140,y+20,x+160,y+20,x+150,y+40};
int poly8[]={x+140,y+35,x+160,y+35,x+150,y+15};
setcolor(4);
setfillstyle(1,4);
fillpoly(3,poly7);
fillpoly(3,poly8);
setcolor(1);
setfillstyle(1,4);
}
failmsg1()
{
for(i=0;i<20;i++)
{
sound(random(2000)+1000);
delay(200);
setfillstyle(9,14);
bar3d(140,100, 440,250, 0, 0);
settextstyle(0,0,2);
outtextxy(230,150,"SORRY !");
settextstyle(0,0,1);
outtextxy(150,200,"**** You have Loose this game ****");
outtextxy(180,230," Now your SCORE will be ZERO ");
count=0;
}
// cleardevice();
}
boxbg(int left,int top, int right,int bottom, int color,int style,int bordercolor,int xa,int ya)
{
setfillstyle(style,color);
setcolor(bordercolor);
bar3d(left,top,right,bottom,xa,ya);
setcolor(5);
}

No comments:

Post a Comment