adventuretimeh

SNAKE GAME DEV

Sep 28th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<ctype.h>
  4. #include<stdlib.h>
  5. #include<windows.h>
  6. #include<time.h>
  7. #include<string.h>
  8. #define LEFT 1
  9. #define RIGHT 2
  10. #define UP 3
  11. #define DOWN 4
  12. void textcolor(int fc,int bc=-1){
  13.     if(fc<0 || fc>15)
  14.         return;
  15.     HANDLE h;
  16.     h = GetStdHandle(STD_OUTPUT_HANDLE);
  17.     if(bc>=0 && bc<16)
  18.         SetConsoleTextAttribute(h,fc|bc*16);
  19.     else
  20.         SetConsoleTextAttribute(h,fc);
  21. }
  22. void textcolor(char *fc,char *bc=""){
  23.     int x,y=16;
  24.     char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
  25.     "LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
  26.     for(x=0;x<16;x++)
  27.         if(strcmpi(colors[x],fc)==0)
  28.             break;
  29.     if(strlen(bc)>0)
  30.         for(y=0;y<16;y++)
  31.             if(strcmpi(colors[y],bc)==0)
  32.                 break;
  33.     textcolor(x,y);
  34. }
  35. void textcolor(char *fc,int bc){
  36.     int x;
  37.     char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
  38.     "LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
  39.     for(x=0;x<16;x++)
  40.         if(strcmpi(colors[x],fc)==0)
  41.             break;
  42.     textcolor(x,bc);
  43. }
  44. void textcolor(int fc,char *bc){
  45.     int y;
  46.     char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
  47.     "LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
  48.     if(strlen(bc)>0)
  49.         for(y=0;y<16;y++)
  50.             if(strcmpi(colors[y],bc)==0)
  51.                 break;
  52.     textcolor(fc,y);
  53. }
  54. void gotoxy(int x, int y)
  55. {
  56.  COORD coord;
  57.  coord.X = x;
  58.  coord.Y = y;
  59.  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  60. }
  61. void getup(){
  62.     HANDLE hout;
  63.     CONSOLE_CURSOR_INFO cursor;
  64.     hout = GetStdHandle(STD_OUTPUT_HANDLE);
  65.     cursor.dwSize=1;
  66.     cursor.bVisible=false;
  67.     SetConsoleCursorInfo(hout, &cursor);
  68.     system("mode con:cols=80 lines=25");
  69.     system("title Snake Game - www.youtube.com/projectcoding");
  70.     system("cls");
  71.     textcolor("LightPurple");
  72.     printf("\n  %c",218);
  73.     int x;
  74.     for(x=0;x<75;x++)
  75.         printf("%c",196);
  76.     printf("%c  ",191);
  77.     for(x=0;x<17;x++){
  78.         gotoxy(2,x+2);
  79.         printf("%c",179);
  80.         gotoxy(78,x+2);
  81.         printf("%c ",179);
  82.     }
  83.     printf("  %c",192);
  84.     for(x=0;x<75;x++)
  85.         printf("%c",196);
  86.     printf("%c  ",217);
  87.     printf(" %c",218);
  88.     for(x=0;x<21;x++)
  89.     printf("%c",196);
  90.     printf("%c\n",191);
  91.     printf("  %c S N A K E   G A M E %c\n",179,179);
  92.     printf("  %c",192);
  93.     for(x=0;x<21;x++)
  94.         printf("%c",196);
  95.     printf("%c",217);
  96.    
  97.     gotoxy(59,20);
  98.     printf("%c",218);
  99.     for(x=0;x<18;x++)
  100.         printf("%c",196);
  101.     printf("%c",191);
  102.     gotoxy(59,21);
  103.     printf("%c SCORE : 100      %c",179,179);
  104.     gotoxy(59,22);
  105.     printf("%c STATUS: Playing  %c",179,179);
  106.     gotoxy(59,23);
  107.     printf("%c",192);
  108.     for(x=0;x<18;x++)
  109.         printf("%c",196);
  110.     printf("%c",217);
  111.     gotoxy(28,20);
  112.     printf("Press 'x' to Exit");
  113.     gotoxy(28,21);
  114.     printf("Press Space to Pause and Play");
  115.     gotoxy(10,23);
  116.     textcolor("white","blue");
  117.     printf(" www.youtube.com/projectcoding ");
  118.     textcolor(7);
  119. }
  120. void score(int sc){
  121.     gotoxy(69,21);
  122.     printf("%6d",sc*10);
  123. }
  124. void status(char *s,int c=7){
  125.     gotoxy(69,22);
  126.     textcolor(c);
  127.     int x;
  128.     for(x=0;x<strlen(s);x++)
  129.         printf("%c",s[x]);
  130.     for(;x<8;x++)
  131.         printf(" ");
  132.     textcolor(7);
  133. }
  134. int main(){
  135.     getup();
  136.     register int flow,size,i,xb,yb;
  137.     int speed,restart=1,tmp,xpos[100],ypos[100],scr;
  138.     srand(time(NULL));
  139.     while(true){
  140.         if(restart){
  141.             status("Playing",10);
  142.             for(int k=1;k<75;k+=2)
  143.                 for(int j=0;j<17;j++){
  144.                     gotoxy(k+3,j+2);
  145.                     printf(" ");
  146.                 }
  147.             size=5;
  148.             speed=200;
  149.             scr=0;
  150.             score(scr);
  151.             flow=RIGHT;
  152.             xpos[0]=20;
  153.             for(i=0;i<size;i++){
  154.                 xpos[i]=xpos[0]-i*2;
  155.                 ypos[i]=10;
  156.             }
  157.             for(i=0;i<size;i++){
  158.                 gotoxy(xpos[i],ypos[i]);
  159.                 printf("o");
  160.             }
  161.             for(tmp=1;true;){
  162.                 do{
  163.                     xb=rand()%75+3;
  164.                 }while(xb%2!=0);
  165.                 yb=rand()%17+2;
  166.                 for(i=0;i<size;i++)
  167.                     if(xb==xpos[i] && yb==ypos[i]){
  168.                         tmp=0; break;
  169.                     }
  170.                 if(tmp)
  171.                     break;
  172.             }
  173.             gotoxy(xb,yb);
  174.             textcolor("lightgreen");
  175.             printf("@");
  176.             textcolor(7);
  177.             restart=0;
  178.         }
  179.         while(!kbhit() && !restart) {
  180.             if(xpos[0]==xb && ypos[0]==yb){
  181.                 for(tmp=1;true;){
  182.                     do{
  183.                         xb=rand()%75+3;
  184.                     }while(xb%2!=0);
  185.                     yb=rand()%17+2;
  186.                     for(i=0;i<size;i++)
  187.                         if(xb==xpos[i] && yb==ypos[i]){
  188.                             tmp=0; break;
  189.                         }
  190.                     if(tmp)
  191.                         break;
  192.                 }
  193.                 gotoxy(xb,yb);
  194.                 textcolor("lightgreen");
  195.                 printf("@");
  196.                 textcolor(7);
  197.                 size++;
  198.                 scr++;
  199.                 speed-=3;
  200.                 score(scr);
  201.             }
  202.             gotoxy(xpos[size-1],ypos[size-1]);
  203.             for(i=size-1;i>0;i--){             
  204.                 xpos[i]=xpos[i-1];
  205.                 ypos[i]=ypos[i-1];
  206.             }
  207.             switch(flow){
  208.                 case RIGHT :xpos[i]+=2; break;
  209.                 case LEFT : xpos[i]-=2; break;
  210.                 case UP :   ypos[i]-=1; break;
  211.                 case DOWN : ypos[i]+=1;
  212.             }
  213.             tmp=1;
  214.             for(i=1;i<size;i++)
  215.                 if(xpos[i]==xpos[0] && ypos[i]==ypos[0]){
  216.                     tmp=0;
  217.                     break;
  218.                 }
  219.             if(xpos[0]>76 || xpos[0]<4 || ypos[0]<2 ||ypos[0]>18)
  220.                 tmp=0;
  221.             if(tmp){
  222.                 printf(" ");
  223.                 gotoxy(xpos[0],ypos[0]);
  224.                 printf("O");   
  225.                 gotoxy(xpos[1],ypos[1]);
  226.                 printf("o");   
  227.             }
  228.             else{
  229.                 textcolor("LIGHTRED");
  230.                 printf("o");
  231.                 gotoxy(xpos[1],ypos[1]);
  232.                 printf("O");
  233.                 for(i=2;i<size;i++){
  234.                     gotoxy(xpos[i],ypos[i]);
  235.                     printf("o");
  236.                 }
  237.                 textcolor(7);
  238.                 status("GameOver",12);
  239.                 restart=1;
  240.                 getch();
  241.             }
  242.             //delay(speed);
  243.             Sleep(speed);
  244.         }
  245.         char ch=getch();
  246.         switch(tolower(ch)){
  247.             case 'x' :  system("cls");
  248.                         return 0;
  249.             case ' ' : status("Paused");
  250.                         while(true){
  251.                             char z=getch();
  252.                             if(z=='x')
  253.                                 return 0;
  254.                             if(z==' ')
  255.                                 break;
  256.                         }
  257.                         status("Playing",10);
  258.                         break;
  259.             case -32: {
  260.                 char chh=getch();
  261.                 if(chh==72 && flow!=DOWN)
  262.                     flow=UP;
  263.                 else if(chh==80 && flow!=UP)
  264.                     flow=DOWN;
  265.                 else if(chh==75 && flow!=RIGHT)
  266.                     flow=LEFT;
  267.                 else if(chh==77 && flow!=LEFT)
  268.                     flow=RIGHT;
  269.                 break;
  270.             }
  271.         }
  272.     }
  273. }
Add Comment
Please, Sign In to add comment