//Smitten Ediot.
//sweetfriends@gmail.com
//Programmed Date - 13/10/2007 - Happy Ramzan.
//Snake.C
//Umaaga,
//ascii 72 for up arrow
//ascii 80 for down arrow
//ascii 75 for left arrow
//ascii 77 for Right arrow
#include[stdio.h]
#include[conio.h]
#include[graphics.h]
#include[dos.h]
#include[stdlib.h]
void wrhed(int**,int**);
void mov_Left(int*,int*);
void mov_Rit(int*,int*);
void mov_Top(int*,int*);
void mov_Dwn(int*,int*);
void print(int,int);
void taill(int,int);
void showoff_Screen();
long int keystrokes=0;
long int ks=0;
int tail[11700][2];
int x=20,y=10;
int foodx=25,foody=35;
int key=80,pinkey=80;
int points=0;
int dlay=70;
int just_Found=0;
int foood=0;
int just_Done=0;
int tc=0;
int toss=0;
//main function
void main()
{
textmode(C80);
textcolor(2);
showoff_Screen();
clrscr();
textcolor(4);
textmode(C4350);
gotoxy(foodx,foody);
cprintf("0");
gotoxy(x,y);
while(!kbhit() (key=getch())!=27)
{
_setcursortype(_NOCURSOR);
if(pinkey==80 && key==72)key=pinkey;
if(pinkey==72 && key==80)key=pinkey;
if(pinkey==77 && key==75)key=pinkey;
if(pinkey==75 && key==77)key=pinkey;
switch(key)
{
case 72:mov_Top(&x,&y);pinkey=key;break;
case 75:mov_Left(&x,&y);pinkey=key;break;
case 77:mov_Rit(&x,&y);pinkey=key;break;
case 80:mov_Dwn(&x,&y);pinkey=key;break;
}
delay(dlay);
gotoxy(3,50);
textcolor(4);
cprintf("Snake Movement Count: %5ld",ks++);
gotoxy(x,y);
if(pinkey!=key)
{
gotoxy(32,50);
textcolor(1);
cprintf("Key Strokes: %5ld",keystrokes++);
gotoxy(x,y);
}
textcolor(random(15));
}
}//main end
//----------------------------
void taill(int q,int w)
{
tc++;
tail[tc][0]=q;
tail[tc][1]=w;
if(foood)foood=0;
}
void wrhed(int **xx,int **yy)
{
**xx=wherex();
**yy=wherey();
if((**xx==80 && **yy==49) && key==77) { **xx=1;}
if((**xx==80 && **yy==49) && key==80) { **yy=1;}
if(**xx==1 && key==75)**xx=80;
else if(**xx==80 && key==77)**xx=1;
if(**yy==49 && key==80) **yy=1;
else if(**yy==1 && key==72)**yy=50;
//food part
if(foodx==**xx && foody==**yy)
{
// tail_Count++;
foodx=random(70);
foody=random(40);
int aagain=1;
while(foodx<5 foody<3 aagain)
{
foodx=random(70);
foody=random(40);
aagain=0;
for(int yh=toss;yh<=tc;yh++)
{
if(tail[yh][0]==foodx && tail[yh][1]==foody)
{
aagain=1;
}
}
}
gotoxy(foodx,foody);
textcolor(2);
cprintf("0");
gotoxy(60,50);
points++;
cprintf("Points : %5d",points*10);
gotoxy(**xx,**yy);
foood=1;
}
}
void mov_Left(int *a,int *b)
{
wrhed(&a,&b);
x--;
gotoxy(x,y);
print(x,y);
gotoxy(x,y);
taill(x,y);
}
void mov_Rit(int *a,int *b)
{
wrhed(&a,&b);
x++;
gotoxy(x,y);
print(x,y);
gotoxy(x,y);
taill(x,y);
}
void mov_Top(int *a,int *b)
{
wrhed(&a,&b);
y--;
gotoxy(x,y);
print(x,y);
gotoxy(x,y);
taill(x,y);
}
void mov_Dwn(int *a,int *b)
{
wrhed(&a,&b);
y++;
gotoxy(x,y);
print(x,y);
gotoxy(x,y);
taill(x,y);
}
void showoff_Screen()
{
clrscr();
char name[122]="Progammed by Murali.M";
_setcursortype(_NOCURSOR);
while(!kbhit())
{
gotoxy(22,21);
cprintf(" Hello, Press any key to Start Game..... . . . .");
gotoxy(29,12);
for(int as=0;as<=20;as++)
{
cprintf("%c",name[as]);
delay(100);
}
delay(300);
clrscr();
}
gotoxy(12,12);
int level;
cprintf("Enter the speed (any from 1 to 5):");
scanf("%d",&level);
switch(level)
{
case 1:dlay=90;break;
case 2:dlay=80;break;
case 3:dlay=70;break;
case 4:dlay=66;break;
case 5:dlay=60;break;
default: dlay=65;
}
cprintf("Press Esc key to exit the game while playin....");
}
void print(int i,int j)
{
for(int yh=toss;yh<=tc;yh++)
{
if(tail[yh][0]==x && tail[yh][1]==y)
{
clrscr();
textmode(C80);
gotoxy(1,4);
printf("\n Your Wonderful Points : %5d",points*10);
printf("\n\n Total Movement of the snake : %5ld",ks);
printf("\n\n Total keystrokes : %5ld",keystrokes);
printf("\n\n Average keystrokes per Point is : %5d",keystrokes/points);
getch();
delay(100);
clrscr();
exit(0);
}
}
if(foood==0)
{
gotoxy(tail[toss][0],tail[toss][1]);
cprintf(" ");
toss++;
}
gotoxy(i,j);
cprintf("0");
}
Hello friends, plz replace a angle bracket instead of square bracket at the header file section of the program and when u try to execute tis program u wil get errors at two lines,
one at --- while(!kbhit() OR (key=getch())!=27) --- tis line and another at -- while(foodx =5 OR foody OR again).... line. cause OR should be replaced by the symbol near shift key which represent or in c program . Plz make tis corrections b4 executin to avoid errors. Thank You!! span>