Advertisement
Firefoxes

snake try

Dec 17th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <time.h>
  7.  
  8. #define CAMP "##############################################", \
  9.              "#............................................#", \
  10.              "#............................................#", \
  11.              "#............................................#", \
  12.              "#............................................#", \
  13.              "#.......ooo..................................#", \
  14.              "#............................................#", \
  15.              "#............................................#", \
  16.              "#............................................#", \
  17.              "#............................................#", \
  18.              "#............................................#", \
  19.              "#............................................#", \
  20.              "#............................................#", \
  21.              "#............................................#", \
  22.              "##############################################"
  23.  
  24. #define TAMV 15
  25. #define TAMH 46
  26. #define MAX 1000000
  27.  
  28. #define U 'w'
  29. #define D 's'
  30. #define R 'd'
  31. #define L 'a'
  32.  
  33. #define SI 1
  34. #define NO 0
  35.  
  36. #define LIB 0
  37. #define OBS 1
  38. #define BIN 2
  39.  
  40. void imprimeCampo(char tab[][TAMH]);
  41. int moveCabeza(char tab[][TAMH], int *y, int *x, char dir);
  42. void borraCola (char tab[][TAMH], int *b, int *a, char dir[MAX], int *i);
  43. int comp (char dirt[MAX], int i, char dir);
  44. void fruta(char tab[][TAMH]);
  45. int comeFruta(char tab[][TAMH], int y, int x, char dir);
  46.  
  47. int main()
  48. {
  49.     char camp[TAMV][TAMH] = {CAMP};
  50.     char direction[MAX];
  51.     char dir=R;
  52.  
  53.     int cont = 0;
  54.     int i=1;
  55.  
  56.     int x = 10;
  57.     int y = 5;
  58.     int a = 8;
  59.     int b = 5;
  60.  
  61.     int control;
  62.     int come=NO;
  63.  
  64.     imprimeCampo(camp);
  65.     direction[0]=R;
  66.     direction[1]=R;
  67.  
  68.     printf(" \nPress any key to start and '0' to stop.\n");
  69.     getch();
  70.     fruta(camp);
  71.  
  72.   do
  73.   {
  74.         Sleep(200);
  75.         if(kbhit()==1)
  76.         {
  77.             dir=getch();
  78.             if(comp(direction, i, dir)==BIN)
  79.                     dir=direction[i];
  80.         }
  81.  
  82.  
  83.         ++i;
  84.         direction[i]=dir;
  85.         come = comeFruta(camp, y, x, dir);
  86.         control=moveCabeza(camp, &y, &x, dir);
  87.  
  88.         if(come==0)
  89.         {
  90.             borraCola(camp, &b, &a, direction, &cont);
  91.             ++cont;
  92.         }
  93.         else
  94.             fruta(camp);
  95.  
  96.  
  97.         imprimeCampo(camp);
  98.         printf("Score: %d\n", come);
  99.     }
  100.     while(dir!='0' && control!=OBS);
  101.  
  102.     printf("\nGame over.\n");
  103.  
  104.     return 0;
  105. }
  106.  
  107. void imprimeCampo(char tab[][TAMH])
  108. {
  109.   int i;
  110.   int j;
  111.  
  112.   system("cls");
  113.  
  114.   for(j=0;j<TAMV;j++)
  115.     {
  116.       for(i=0;i<TAMH;i++)
  117.             printf("%c", tab[j][i]);
  118.       printf("\n");
  119.     }
  120. }
  121.  
  122. int moveCabeza(char tab[][TAMH], int *y, int *x, char dir)
  123. {
  124.     int pos=OBS;
  125.  
  126.     if(tab[(*y)-1][(*x)]!='o' && dir==U && tab[(*y)-1][(*x)]!='#')
  127.     {
  128.       tab[--(*y)][(*x)]='o';
  129.       pos=LIB;
  130.     }
  131.  
  132.     if(tab[(*y)+1][(*x)]!='o' && dir==D && tab[(*y)+1][(*x)]!='#')
  133.     {
  134.        tab[++(*y)][(*x)]='o';
  135.        pos=LIB;
  136.     }
  137.  
  138.     if(tab[(*y)][(*x)+1]!='o' && dir==R && tab[(*y)][(*x)+1]!='#')
  139.     {
  140.         tab[(*y)][++(*x)]='o';
  141.         pos=LIB;
  142.     }
  143.  
  144.     if(tab[(*y)][(*x)-1]!='o' && dir==L && tab[(*y)][(*x)-1]!='#')
  145.     {
  146.       tab[(*y)][--(*x)]='o';
  147.       pos=LIB;
  148.     }
  149.  
  150.     return pos;
  151. }
  152.  
  153. void borraCola(char tab[][TAMH], int *b, int *a, char dir[MAX], int *i)
  154. {
  155.     tab[*b][*a] = '.';
  156.  
  157.         if(dir[*i]==U)
  158.             --(*b);
  159.  
  160.         if(dir[*i]==D)
  161.             ++(*b);
  162.  
  163.         if(dir[*i]==R)
  164.             ++(*a);
  165.  
  166.         if(dir[*i]==L)
  167.             --(*a);
  168.  
  169. }
  170.  
  171. int comp (char dirt[MAX], int i, char dir)
  172. {
  173.     int control=LIB;
  174.  
  175.     if((dir==U && dirt[i]==D) || (dir==D && dirt[i]==U) || (dir==L && dirt[i]==R) || (dir==R && dirt[i]==L))
  176.         control = BIN;
  177.     return control;
  178. }
  179.  
  180. void fruta(char tab[][TAMH])
  181. {
  182.     int x;
  183.     int y;
  184.  
  185.     do
  186.     {
  187.         srand(time(NULL));
  188.         x = ( rand() % 44 )+1;
  189.         y = ( rand() % 13 )+1;
  190.     }
  191.     while(tab[y][x]=='o' && tab[y][x]=='#');
  192.  
  193.     tab[y][x]='x';
  194.  
  195. }
  196.  
  197. int comeFruta(char tab[][TAMH], int y, int x, char dir)
  198. {
  199.     int control=1;
  200.  
  201.     if(tab[--y][x]!='x' && dir==U)
  202.         {
  203.             control=0;
  204.         }
  205.  
  206.     if(tab[++y][x]!='x' && dir==D);
  207.         {
  208.             control=0;
  209.         }
  210.  
  211.     if(tab[y][++x]!='x' && dir==R)
  212.         {
  213.             control=0;
  214.         }
  215.  
  216.  
  217.     if(tab[y][--x]!='x' && dir==L)
  218.         {
  219.             control=0;
  220.         }
  221.  
  222.     return control;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement