SabirSazzad

Snake Game

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