#include #include #include #include #include int gd=DETECT,gm,ctrl=-30,x_location,y_location,rear=-1,maxx,maxy; int in_key,ctr[1500],nc,xr=0,yr=0,sp=15,err=0,scr=0,i=0,front=0; int size,x=0,y=0,dx_location,xctr[1500],yctr[1500]; char pattern={1}; main() { /*function to initialise the graphics mode*/ grapini(); /*function for intro graphics*/ intro(); /*function to draw boundaries*/ snakeboundary(); /*function to initialise game and generate first dot*/ intgame(); /*condition to check if key pressed is 'q'*/ nc=in_key=bioskey(0); while(in_key!=4209) { /*condition to check if any key is pressed*/ while(!kbhit()) { /*case to increment front end of snake*/ switch(in_key) { case 0x4800:y_location-=1;break; case 0x5000:y_location+=1;break; case 0x4b00:x_location-=1;break; case 0x4d00:x_location+=1;break; } putpixel(x_location,y_location,15); /*condition to check if the snake touches the boundary*/ if(err) break; if(((y_location==dy_location)&&(x_location==dx_location))||\ (x_location>=maxx-13)||(y_location<=13)||(y_location>=maxy-63)) { err=1; break; } /*delay to vary speed*/ delay(sp); /*ctrl variable to allow initial length of snake*/ if(ctrl>0) { if((dx_location==xctr[front]&&(dy_location==yctr[front])) nc=ctr[front++]; if(((dx_location==xr-1)||(dx_location==xr)||(dx_location==xr+1))&&\ ((dy_location==yr-1)||(dy_location==yr)||(dy_location==yr+1))) /*function for random dot generation*/ randdot(); /*case to increment back end of snake*/ switch(nc) { case 0x4800:dy_location-=1;break; case 0x5000:dy_location+=1;break; case 0x4b00:dx_location-=1;break; case 0x4d00:dx_location+=1;break; } putpixel(dx_location,dy_location,0); } ctrl++; } /*stats for storing the new direction and position of snake*/ in_key=bioskey(0); rear+=1; ctr[rear]=in_key; xctr[rear]=x_location; yctr[rear]=y_location; } /*condition to display score after end of game*/ if(err) { settextstyle(7,0,5); setcolor(150; printf("your score is %d",scr); outtextxy(200,100,"your game is over"); getch(); } closegraph(); } /*end of main*/ /*function to initialise graphics mode*/ grapini() { intgraph(&gd,&gm,"c:\\turboc3\\turboc3"); setgraphmode(VGAHI); maxx=getmaxx(); maxy=getmaxy(); moveto(x_location=maxx/2,y_location=maxy/2); } /*function for intro graphics*/ intro() { int locy=0,locx=0,locy1=0,locy2=0; settextstyle(7,0,10); locx=maxx/4-15; locy1=locy2=locy=maxy/2; /*condition to display demo till key pressed*/ while(!kbhit()) { for(i=1;i<=50;i++) { setcolor(14); outtextxy(locx,locy2-maxy/2,"g"); outtextxy(locx+65,locy1-maxy/2,"a"); outtextxy(locx+150,locy2-maxy/2,"m"); outtextxy(locx+295,locy1-maxy/2,"e"); outtextxy(locx,locy1,"s"); outtextxy(locx+60,locy2,"n"); outtextxy(locx+150,locy1,"a"); outtextxy(locx+225,locy2,"k"); outtextxy(locx+300,locy1,"e"); delay(50); locy1++; locy2--; setcolor(0); outtextxy(locx,locy2-maxy/2+1,"j"); outtextxy(locx+65,locy1-maxy/2-1,"p"); outtextxy(locx+150,locy2-maxy/2+1,"'"); outtextxy(locx+295,locy-maxy/2-1,"s"); outtextxy(locx,locy1-1,"s"); outtextxy(locx+60,locy2+1,"n"); outtextxy(locx+150,locy1-1,"a"); outtextxy(locx+225,locy2+1,"k"); outtextxy(locx+300,locy1-1,"e"); } } getch(); } /*function to draw boundaries*/ snake boundary() { setcolor(10); line(10,10,10,maxy-60);line(13,13,13,maxy-63); line(10,10,maxx-10,10);line(13,13,maxx-13,13); line(maxx-10,10,maxx-10,maxy-60);line(maxx-13,13,maxx-13,maxy-63); line(10,maxy-60,maxx-10,maxy-60);line(13,maxy-63,maxx-13,maxy-63); } /*function to initialise game and generate first dot*/ intgame() { randomize(); xr=random(maxx-13); if(xr<=13) xr+=13; yr=random(maxy-63); if(yr<=13) yr+=13; bar(xr-1,yr+1,xr+1,yr-1); setfillstyle(pattern,15); dx_location=maxx/2; dy_location=maxy/2; } /*function for random dot generation*/ randdot() { scr++; setfillstyle(pattern,0); bar(xr-2,yr+2,xr+2,yr-2); sp=sp-1; xr=random(maxx-13); if(xr<=13) xr+=13; yr=random(maxy-63); if(yr<=13) yr+=13; bar(xr-1,yr+1,xr+1,yr-1); ctrl=-30; setfillstyle(pattern,15); bar(xr-1,yr+1,xr+1,yr-1); }