Advertisement
Guest User

Snake (release)

a guest
May 27th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5. #include <Windows.h>
  6. #include <locale.h>
  7. #define LEN 20//длина
  8. #define WID 20//ширина
  9.  
  10. int play = 0;
  11.  
  12. typedef struct Snake {
  13.     int x_coordinate;
  14.     int y_coordinate;
  15.     char symbol;
  16.     char route;
  17.     int speed;
  18.     int size;
  19.     int tailX[100], tailY[100];
  20. } PSnake;
  21.  
  22. typedef struct Fruit
  23. {
  24.     int x_coordinate;
  25.     int y_coordinate;
  26.     char symbol;
  27. } PFruit;
  28. void change_route(int code, PSnake *s);
  29. void snake_logic(PSnake *s, PFruit *f, int *score);
  30. void Draw(PSnake *s, PFruit *f, int *score);
  31. int random(int a, int b);
  32. void start_settings(PSnake *s, PFruit *f);
  33.  
  34. int main()
  35. {
  36.     setlocale(LC_ALL, "Rus");
  37.     srand(time(NULL));
  38.     int i, j, k, c = 0, score = 0;
  39.     PSnake s;
  40.     PFruit f;
  41.  
  42.     start_settings(&s, &f);
  43.  
  44.     int flag;
  45.     printf("Для начала игры нажмите любую клавишу\n");
  46.     _getch();
  47.     while (!play)
  48.     {
  49.         Draw(&s, &f, &score);
  50.         if (_kbhit()) {
  51.             c = _getch();
  52.             if (c == 27){
  53.                 play = 1;
  54.                 continue;
  55.             }
  56.             change_route(c, &s);
  57.            
  58.         }
  59.        
  60.         snake_logic(&s, &f, &score);
  61.     }
  62.     printf("Игра окончена\nНабрано очков: %d\n", score);
  63.     system("pause");
  64.     return 0;
  65. }
  66.  
  67. void change_route(int code, PSnake *s)
  68. {
  69.     switch (code)
  70.     {
  71.     case 119: /*s->x_coordinate--;*/ s->route = 'u'; break;//Вверх
  72.     case 100: /*s->y_coordinate++;*/ s->route = 'r'; break;//Вправо
  73.     case 97: /*s->y_coordinate--;*/ s->route = 'l'; break;//Влево
  74.     case 115:/* s->x_coordinate++;*/ s->route = 'd'; break;//Вниз
  75.     default: break;
  76.     }
  77.     return;
  78. }
  79.  
  80. void snake_logic(PSnake * s, PFruit * f, int * score)
  81. {
  82.     int prevX = s->tailX[0], prevY = s->tailY[0];
  83.     int prev2X, prev2Y, i;
  84.  
  85.     s->tailX[0] = s->x_coordinate;
  86.     s->tailY[0] = s->y_coordinate;
  87.  
  88.     for (i = 1; i < s->size; i++)
  89.     {
  90.         prev2X = s->tailX[i];
  91.         prev2Y = s->tailY[i];
  92.         s->tailX[i] = prevX;
  93.         s->tailY[i] = prevY;
  94.         prevX = prev2X;
  95.         prevY = prev2Y;
  96.     }
  97. /*---------------------------*/
  98.     char symbol = s->route;
  99.     switch (symbol)
  100.     {
  101.     case 'u': s->x_coordinate--; /*s->route = 'u';*/ break;//Вверх
  102.     case 'r': s->y_coordinate++; /*s->route = 'r';*/ break;//Вправо
  103.     case 'l': s->y_coordinate--; /*s->route = 'l';*/ break;//Влево
  104.     case 'd': s->x_coordinate++;/* s->route = 'd';*/ break;//Вниз
  105.     default: break;
  106.     }
  107.     /*--------------------*/
  108.     if (s->x_coordinate >= WID)
  109.         s->x_coordinate = 0;
  110.     else if (s->x_coordinate < 0)
  111.         s->x_coordinate = WID - 1;
  112.  
  113.     if (s->y_coordinate >= LEN)
  114.         s->y_coordinate = 0;
  115.     else if (s->y_coordinate < 0)
  116.         s->y_coordinate = LEN - 1;
  117. /*----------------------------------*/
  118.     if ((s->x_coordinate == f->x_coordinate) && (s->y_coordinate == f->y_coordinate))
  119.     {
  120.         *score += 10;
  121.         s->size++;
  122.         f->x_coordinate = random(1, WID - 2);
  123.         f->y_coordinate = random(1, LEN - 2);
  124.         while (f->x_coordinate == s->x_coordinate && f->y_coordinate == s->y_coordinate)
  125.         {
  126.             f->x_coordinate = random(1, WID - 2);
  127.             f->y_coordinate = random(1, LEN - 2);
  128.         }
  129.         if (s->speed - 10 != 0)
  130.             (s->speed) -= 10;
  131.     }
  132.     for (i = 0; i < s->size; i++) {
  133.         if (s->tailX[i] == s->x_coordinate && s->tailY[i] == s->y_coordinate)
  134.             play = 1;
  135.     }
  136. }
  137.  
  138. void Draw(PSnake *s, PFruit *f, int *score) {
  139.     int flag;
  140.     Sleep(s->speed);    //speed of game
  141.     system("cls");
  142.     for (int i = 0; i < WID + 2; i++)
  143.         printf("#");
  144.     printf("\n");
  145.  
  146.     for (int i = 0; i < LEN; i++) {
  147.         for (int j = 0; j <= WID; j++) {
  148.             if (j == 0 || j == WID )
  149.                 printf("#");
  150.             if (i == s->x_coordinate && j == s->y_coordinate)
  151.                 printf("%c", s->symbol);
  152.             else if (i == f->x_coordinate && j == f->y_coordinate)
  153.                 printf("%c", f->symbol);
  154.             else {
  155.                 flag = 0;
  156.                 for (int k = 0; k < s->size; k++) {
  157.                     if (s->tailX[k] == i && s->tailY[k] == j) {
  158.                         flag = 1;
  159.                         printf("%c", s->symbol);;
  160.                     }
  161.                 }
  162.                 if (!flag)
  163.                     printf(" ");
  164.             }
  165.         }
  166.         printf("\n");
  167.     }
  168.  
  169.     for (int i = 0; i < WID + 2; i++)
  170.         printf("#");
  171.     printf("\nScore: %d\n", *score);
  172. }
  173.  
  174. void start_settings(PSnake *s, PFruit *f)
  175. {
  176.     s->symbol = '0';
  177.     s->x_coordinate = WID / 2;
  178.     s->y_coordinate = LEN / 2;
  179.     s->tailX[0] = WID / 2;
  180.     s->tailY[0] = LEN / 2;
  181.     s->speed = 200;
  182.     s->size = 0;
  183.     s->route = 0;
  184.  
  185.     f->symbol = '*';
  186.  
  187.     f->x_coordinate = random(1, WID - 2);
  188.     f->y_coordinate = random(1, LEN - 2);
  189.     while(f->x_coordinate == s->x_coordinate && f->y_coordinate == s->y_coordinate)
  190.     {
  191.         f->x_coordinate = random(1, WID - 2);
  192.         f->y_coordinate = random(1, LEN - 2);
  193.     }
  194.  
  195.     return;
  196. }
  197.  
  198. int random(int a, int b)
  199. {
  200.     srand(time(NULL));
  201.     if (a > 0) return a + rand() % (b - a);
  202.     else return a + rand() % (abs(a) + b);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement