shohan11421

snake game project

Oct 18th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include<time.h>
  6. #include<ctype.h>
  7. #include <time.h>
  8. #include <windows.h>
  9. #include <process.h>
  10.  
  11. #define UP 72
  12. #define DOWN 80
  13. #define LEFT 75
  14. #define RIGHT 77
  15.  
  16. int length;
  17. int bend_no;
  18. int len;
  19. char key;
  20. void record();
  21. void load();
  22. int life;
  23. void Delay(long double);
  24. void Move();
  25. void Food();
  26. int Score();
  27. void Print();
  28. void gotoxy(int x, int y);
  29. void GotoXY(int x,int y);
  30. void Bend();
  31. void Boarder();
  32. void Down();
  33. void Left();
  34. void Up();
  35. void Right();
  36. void ExitGame();
  37. int Scoreonly();
  38.  
  39. struct coordinate{
  40.     int x;
  41.     int y;
  42.     int direction;
  43. };
  44.  
  45. typedef struct coordinate coordinate;
  46.  
  47. coordinate head, bend[500],food,body[30];
  48.  
  49. int main()
  50. {
  51.  
  52.     char key;
  53.  
  54.     Print();
  55.  
  56.     system("cls");
  57.  
  58.     load();
  59.  
  60.     length=5;
  61.  
  62.     head.x=25;
  63.  
  64.     head.y=20;
  65.  
  66.     head.direction=RIGHT;
  67.  
  68.     Boarder();
  69.  
  70.     Food(); //to generate food coordinates initially
  71.  
  72.     life=3; //number of extra lives
  73.  
  74.     bend[0]=head;
  75.  
  76.     Move();   //initialing initial bend coordinate
  77.  
  78.     return 0;
  79.  
  80. }
  81.  
  82. void Move()
  83. {
  84.     int a,i;
  85.  
  86.     do{
  87.  
  88.         Food();
  89.         fflush(stdin);
  90.  
  91.         len=0;
  92.  
  93.         for(i=0;i<30;i++)
  94.  
  95.         {
  96.  
  97.             body[i].x=0;
  98.  
  99.             body[i].y=0;
  100.  
  101.             if(i==length)
  102.  
  103.             break;
  104.  
  105.         }
  106.  
  107.         Delay(length);
  108.  
  109.         Boarder();
  110.  
  111.         if(head.direction==RIGHT)
  112.  
  113.             Right();
  114.  
  115.         else if(head.direction==LEFT)
  116.  
  117.             Left();
  118.  
  119.         else if(head.direction==DOWN)
  120.  
  121.             Down();
  122.  
  123.         else if(head.direction==UP)
  124.  
  125.             Up();
  126.  
  127.         ExitGame();
  128.  
  129.     }while(!kbhit());
  130.  
  131.     a=getch();
  132.  
  133.     if(a==27)
  134.  
  135.     {
  136.  
  137.         system("cls");
  138.  
  139.         exit(0);
  140.  
  141.     }
  142.     key=getch();
  143.  
  144.     if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))
  145.  
  146.     {
  147.  
  148.         bend_no++;
  149.  
  150.         bend[bend_no]=head;
  151.  
  152.         head.direction=key;
  153.  
  154.         if(key==UP)
  155.  
  156.             head.y--;
  157.  
  158.         if(key==DOWN)
  159.  
  160.             head.y++;
  161.  
  162.         if(key==RIGHT)
  163.  
  164.             head.x++;
  165.  
  166.         if(key==LEFT)
  167.  
  168.             head.x--;
  169.  
  170.         Move();
  171.  
  172.     }
  173.  
  174.     else if(key==27)
  175.  
  176.     {
  177.  
  178.         system("cls");
  179.  
  180.         exit(0);
  181.  
  182.     }
  183.  
  184.     else
  185.  
  186.     {
  187.  
  188.         printf("\a");
  189.  
  190.         Move();
  191.  
  192.     }
  193. }
  194.  
  195. void gotoxy(int x, int y)
  196. {
  197.  
  198.  COORD coord;
  199.  
  200.  coord.X = x;
  201.  
  202.  coord.Y = y;
  203.  
  204.  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  205.  
  206. }
  207. void GotoXY(int x, int y)
  208. {
  209.     HANDLE a;
  210.     COORD b;
  211.     fflush(stdout);
  212.     b.X = x;
  213.     b.Y = y;
  214.     a = GetStdHandle(STD_OUTPUT_HANDLE);
  215.     SetConsoleCursorPosition(a,b);
  216.  }
  217. void load(){
  218.     int row,col,r,c,q;
  219.     gotoxy(36,14);
  220.     printf("loading...");
  221.     gotoxy(30,15);
  222.     for(r=1;r<=20;r++){
  223.     for(q=0;q<=100000000;q++);//to display the character slowly
  224.     printf("%c",177);}
  225.     getch();
  226. }
  227. void Down()
  228. {
  229.     int i;
  230.     for(i=0;i<=(head.y-bend[bend_no].y)&&len<length;i++)
  231.     {
  232.         GotoXY(head.x,head.y-i);
  233.         {
  234.             if(len==0)
  235.                 printf("v");
  236.             else
  237.                 printf("*");
  238.         }
  239.         body[len].x=head.x;
  240.         body[len].y=head.y-i;
  241.         len++;
  242.     }
  243.     Bend();
  244.     if(!kbhit())
  245.         head.y++;
  246. }
  247. void Delay(long double k)
  248. {
  249.     Score();
  250.     long double i;
  251.     for(i=0;i<=(10000000);i++);
  252. }
  253. void ExitGame()
  254. {
  255.     int i,check=0;
  256.     for(i=4;i<length;i++)   //starts with 4 because it needs minimum 4 element to touch its own body
  257.     {
  258.         if(body[0].x==body[i].x&&body[0].y==body[i].y)
  259.         {
  260.             check++;    //check's value increases as the coordinates of head is equal to any other body coordinate
  261.         }
  262.         if(i==length||check!=0)
  263.             break;
  264.     }
  265.     if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0)
  266.     {
  267.         life--;
  268.         if(life>=0)
  269.         {
  270.             head.x=25;
  271.             head.y=20;
  272.             bend_no=0;
  273.             head.direction=RIGHT;
  274.             Move();
  275.         }
  276.         else
  277.         {
  278.             system("cls");
  279.             printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
  280.             record();
  281.             exit(0);
  282.         }
  283.     }
  284. }
  285. void Food()
  286. {
  287.     if(head.x==food.x&&head.y==food.y)
  288.     {
  289.         length++;
  290.         time_t a;
  291.         a=time(0);
  292.         srand(a);
  293.         food.x=rand()%70;
  294.         if(food.x<=10)
  295.             food.x+=11;
  296.         food.y=rand()%30;
  297.         if(food.y<=10)
  298.  
  299.             food.y+=11;
  300.     }
  301.     else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/
  302.     {
  303.         food.x=rand()%70;
  304.         if(food.x<=10)
  305.             food.x+=11;
  306.         food.y=rand()%30;
  307.         if(food.y<=10)
  308.             food.y+=11;
  309.     }
  310. }
  311. void Left()
  312. {
  313.     int i;
  314.     for(i=0;i<=(bend[bend_no].x-head.x)&&len<length;i++)
  315.     {
  316.         GotoXY((head.x+i),head.y);
  317.        {
  318.                 if(len==0)
  319.                     printf("<");
  320.                 else
  321.                     printf("*");
  322.         }
  323.         body[len].x=head.x+i;
  324.         body[len].y=head.y;
  325.         len++;
  326.     }
  327.     Bend();
  328.     if(!kbhit())
  329.         head.x--;
  330.  
  331. }
  332. void Right()
  333. {
  334.     int i;
  335.     for(i=0;i<=(head.x-bend[bend_no].x)&&len<length;i++)
  336.     {
  337.         //GotoXY((head.x-i),head.y);
  338.         body[len].x=head.x-i;
  339.         body[len].y=head.y;
  340.         GotoXY(body[len].x,body[len].y);
  341.         {
  342.             if(len==0)
  343.                 printf(">");
  344.             else
  345.                 printf("*");
  346.         }
  347.         /*body[len].x=head.x-i;
  348.         body[len].y=head.y;*/
  349.         len++;
  350.     }
  351.     Bend();
  352.     if(!kbhit())
  353.         head.x++;
  354. }
  355. void Bend()
  356. {
  357.     int i,j,diff;
  358.     for(i=bend_no;i>=0&&len<length;i--)
  359.     {
  360.             if(bend[i].x==bend[i-1].x)
  361.             {
  362.                 diff=bend[i].y-bend[i-1].y;
  363.                 if(diff<0)
  364.                     for(j=1;j<=(-diff);j++)
  365.                     {
  366.                         body[len].x=bend[i].x;
  367.                         body[len].y=bend[i].y+j;
  368.                         GotoXY(body[len].x,body[len].y);
  369.                         printf("*");
  370.                         len++;
  371.                         if(len==length)
  372.                             break;
  373.                     }
  374.                 else if(diff>0)
  375.                     for(j=1;j<=diff;j++)
  376.                     {
  377.                         /*GotoXY(bend[i].x,(bend[i].y-j));
  378.                         printf("*");*/
  379.                         body[len].x=bend[i].x;
  380.                         body[len].y=bend[i].y-j;
  381.                         GotoXY(body[len].x,body[len].y);
  382.                         printf("*");
  383.                         len++;
  384.                         if(len==length)
  385.                             break;
  386.                     }
  387.             }
  388.         else if(bend[i].y==bend[i-1].y)
  389.         {
  390.             diff=bend[i].x-bend[i-1].x;
  391.             if(diff<0)
  392.                 for(j=1;j<=(-diff)&&len<length;j++)
  393.                 {
  394.                     /*GotoXY((bend[i].x+j),bend[i].y);
  395.                     printf("*");*/
  396.                     body[len].x=bend[i].x+j;
  397.                     body[len].y=bend[i].y;
  398.                     GotoXY(body[len].x,body[len].y);
  399.                         printf("*");
  400.                    len++;
  401.                    if(len==length)
  402.                            break;
  403.                }
  404.            else if(diff>0)
  405.                for(j=1;j<=diff&&len<length;j++)
  406.                {
  407.                    /*GotoXY((bend[i].x-j),bend[i].y);
  408.                    printf("*");*/
  409.                    body[len].x=bend[i].x-j;
  410.                    body[len].y=bend[i].y;
  411.                    GotoXY(body[len].x,body[len].y);
  412.                        printf("*");
  413.                    len++;
  414.                    if(len==length)
  415.                        break;
  416.                }
  417.        }
  418.    }
  419. }
  420. void Boarder()
  421. {
  422.    system("cls");
  423.    int i;
  424.    GotoXY(food.x,food.y);   /*displaying food*/
  425.        printf("F");
  426.    for(i=10;i<71;i++)
  427.    {
  428.        GotoXY(i,10);
  429.            printf("!");
  430.        GotoXY(i,30);
  431.            printf("!");
  432.    }
  433.    for(i=10;i<31;i++)
  434.    {
  435.        GotoXY(10,i);
  436.            printf("!");
  437.        GotoXY(70,i);
  438.        printf("!");
  439.    }
  440. }
  441. void Print()
  442. {
  443.    //GotoXY(10,12);
  444.    printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
  445.   getch();
  446.    system("cls");
  447.    printf("\tGame instructions:\n");
  448.    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");
  449.    printf("\n\nPress any key to play game...");
  450.    if(getch()==27)
  451.    exit(0);
  452. }
  453. void record(){
  454.    char plname[20],nplname[20],cha,c;
  455.    int i,j,px;
  456.    FILE *info;
  457.    info=fopen("record.txt","a+");
  458.    getch();
  459.    system("cls");
  460.    printf("Enter your name\n");
  461.    scanf("%[^\n]",plname);
  462.    //************************
  463.    for(j=0;plname[j]!='\0';j++){ //to convert the first letter after space to capital
  464.    nplname[0]=toupper(plname[0]);
  465.    if(plname[j-1]==' '){
  466.    nplname[j]=toupper(plname[j]);
  467.    nplname[j-1]=plname[j-1];}
  468.    else nplname[j]=plname[j];
  469.    }
  470.    nplname[j]='\0';
  471.    //*****************************
  472.    //sdfprintf(info,"\t\t\tPlayers List\n");
  473.    fprintf(info,"Player Name :%s\n",nplname);
  474.     //for date and time
  475.  
  476.    time_t mytime;
  477.   mytime = time(NULL);
  478.   fprintf(info,"Played Date:%s",ctime(&mytime));
  479.      //**************************
  480.      fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score
  481.      //fprintf(info,"\nLevel:%d\n",10);//call level to display level
  482.    for(i=0;i<=50;i++)
  483.    fprintf(info,"%c",'_');
  484.    fprintf(info,"\n");
  485.    fclose(info);
  486.    printf("wanna see past records press 'y'\n");
  487.    cha=getch();
  488.    system("cls");
  489.    if(cha=='y'){
  490.    info=fopen("record.txt","r");
  491.    do{
  492.        putchar(c=getc(info));
  493.        }while(c!=EOF);}
  494.      fclose(info);
  495. }
  496. int Score()
  497. {
  498.    int score;
  499.    GotoXY(20,8);
  500.    score=length-5;
  501.    printf("SCORE : %d",(length-5));
  502.    score=length-5;
  503.    GotoXY(50,8);
  504.    printf("Life : %d",life);
  505.    return score;
  506. }
  507. int Scoreonly()
  508. {
  509. int score=Score();
  510. system("cls");
  511. return score;
  512. }
  513. void Up()
  514. {
  515.    int i;
  516.    for(i=0;i<=(bend[bend_no].y-head.y)&&len<length;i++)
  517.    {
  518.        GotoXY(head.x,head.y+i);
  519.        {
  520.            if(len==0)
  521.                printf("^");
  522.            else
  523.                printf("*");
  524.        }
  525.        body[len].x=head.x;
  526.        body[len].y=head.y+i;
  527.        len++;
  528.    }
  529.    Bend();
  530.    if(!kbhit())
  531.        head.y--;
  532. }
Add Comment
Please, Sign In to add comment