1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
void Food()
{
if(head.x==food.x&&head.y==food.y)
{
length++;
time_t a;
a=time(0);
srand(a);
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/
{
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
}
void Left()
{
int i;
for(i=0; i<=(bend[bend_no].x-head.x)&&len<length; i++)
{
GotoXY((head.x+i),head.y);
{
if(len==0)
printf("<");
else
printf("*");
}
body[len].x=head.x+i;
body[len].y=head.y;
len++;
}
Bend();
if(!kbhit())
head.x--;
}
void Right()
{
int i;
for(i=0; i<=(head.x-bend[bend_no].x)&&len<length; i++)
{
//GotoXY((head.x-i),head.y);
body[len].x=head.x-i;
body[len].y=head.y;
GotoXY(body[len].x,body[len].y);
{
if(len==0)
printf(">");
else
printf("*");
}
/*body[len].x=head.x-i;
body[len].y=head.y;*/
len++;
}
Bend();
if(!kbhit())
head.x++;
}
void Bend()
{
int i,j,diff;
for(i=bend_no; i>=0&&len<length; i--)
{
if(bend[i].x==bend[i-1].x)
{
diff=bend[i].y-bend[i-1].y;
if(diff<0)
for(j=1; j<=(-diff); j++)
{
body[len].x=bend[i].x;
body[len].y=bend[i].y+j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
else if(diff>0)
for(j=1; j<=diff; j++)
{
/*GotoXY(bend[i].x,(bend[i].y-j));
printf("*");*/
body[len].x=bend[i].x;
body[len].y=bend[i].y-j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
else if(bend[i].y==bend[i-1].y)
{
diff=bend[i].x-bend[i-1].x;
if(diff<0)
for(j=1; j<=(-diff)&&len<length; j++)
{
/*GotoXY((bend[i].x+j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x+j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
else if(diff>0)
for(j=1; j<=diff&&len<length; j++)
{
/*GotoXY((bend[i].x-j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x-j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
}
}
void Boarder()
{
system("cls");
int i;
GotoXY(food.x,food.y); /*displaying food*/
printf("F");
for(i=10; i<71; i++)
{
GotoXY(i,10);
printf("!");
GotoXY(i,30);
printf("!");
}
for(i=10; i<31; i++)
{
GotoXY(10,i);
printf("!");
GotoXY(70,i);
printf("!");
}
}
void Print()
{
//GotoXY(10,12);
printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
getch();
system("cls");
printf("\tGame instructions:\n");
printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n");
printf("\n\nPress any key to play game...");
if(getch()==27)
exit(0);
}
void record()
{
char plname[20],nplname[20],cha,c;
int i,j,px;
FILE *info;
info=fopen("record.txt","a+");
getch();
system("cls");
printf("Enter your name\n");
scanf("%[^\n]",plname);
//************************
for(j=0; plname[j]!='\0'; j++) //to convert the first letter after space to capital
{
nplname[0]=toupper(plname[0]);
if(plname[j-1]==' ')
{
nplname[j]=toupper(plname[j]);
nplname[j-1]=plname[j-1];
}
else nplname[j]=plname[j];
}
nplname[j]='\0';
//*****************************
//sdfprintf(info,"\t\t\tPlayers List\n");
fprintf(info,"Player Name :%s\n",nplname);
//for date and time
time_t mytime;
mytime = time(NULL);
fprintf(info,"Played Date:%s",ctime(&mytime));
//**************************
fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score
//fprintf(info,"\nLevel:%d\n",10);//call level to display level
for(i=0; i<=50; i++)
fprintf(info,"%c",'_');
fprintf(info,"\n");
fclose(info);
printf("Wanna see past records press 'y'\n");
cha=getch();
system("cls");
if(cha=='y')
{
info=fopen("record.txt","r");
do
{
putchar(c=getc(info));
}
while(c!=EOF);
}
fclose(info);
}
int Score()
{
int score;
GotoXY(20,8);
score=length-5;
printf("SCORE : %d",(length-5));
score=length-5;
GotoXY(50,8);
printf("Life : %d",life);
return score;
}
int Scoreonly()
{
int score=Score();
system("cls");
return score;
}
void Up()
{
int i;
for(i=0; i<=(bend[bend_no].y-head.y)&&len<length; i++)
{
GotoXY(head.x,head.y+i);
{
if(len==0)
printf("^");
else
printf("*");
}
body[len].x=head.x;
body[len].y=head.y+i;
len++;
}
Bend();
if(!kbhit())
head.y--;
}
|