Daniel_leinaD

snake

Apr 17th, 2022
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5.  
  6. bool gameOver;
  7. const int widht = 20, height = 20;
  8. int x, y, fruitX, fruitY, score;
  9. int tailX[100];
  10. int tailY[100];
  11. int nTail;
  12. enum eDirection { stop = 0, LEFT, RIGHT, DOWN, UP };
  13. eDirection dir;
  14.  
  15. void setup() {
  16.     gameOver = false;
  17.     dir = stop;
  18.     x = widht / 2 - 1;
  19.     y = height / 2 - 1;
  20.     fruitX = rand() % widht;
  21.     fruitY = rand() % height;
  22.     score = 0;
  23. }
  24.  
  25.  
  26. void draw() {
  27.     system("cls");
  28.  
  29.     for (int i = 0; i < widht+1; i++) {
  30.         cout << "#";
  31.     }
  32.     cout << '\n';
  33.  
  34.     for (int i = 0; i < widht; i++) {
  35.         for (int j = 0; j < height; j++) {
  36.             if (j == 0 || j == height - 1) {
  37.                 cout << "#";
  38.             }
  39.             if (i == y && j == x) {
  40.                 cout << "S";
  41.             }
  42.             else if (i == fruitY && j == fruitX) {
  43.                 cout << "0";
  44.             }
  45.             else {
  46.                 bool print = false;
  47.                 for (int k = 0; k < nTail; k++) {
  48.                     if (tailX[k] == j && tailY[k] == i) {
  49.                         print = true;
  50.                         cout << "s";
  51.                     }
  52.                 }
  53.                 if (print == false) {
  54.                     cout << " ";
  55.                 }
  56.  
  57.             }
  58.         }
  59.         cout << '\n';
  60.     }
  61.  
  62.     for (int i = 0; i < widht+1; i++) {
  63.         cout << "#";
  64.     }
  65.     cout << '\n';
  66.     cout << "Ваш счет равен = " << score;
  67. }
  68.  
  69. void input() {
  70.     if (_kbhit() == true) {
  71.         switch (_getch())
  72.         {
  73.         case 'a':
  74.             dir = LEFT;
  75.             break;
  76.         case 'w':
  77.             dir = UP;
  78.             break;
  79.         case 'd':
  80.             dir = RIGHT;
  81.             break;
  82.         case 's':
  83.             dir = DOWN;
  84.             break;
  85.         case 'g':
  86.             gameOver = true;
  87.             break;
  88.         }
  89.     }
  90. }
  91.  
  92. void logic() {
  93.     int prevX = tailX[0];
  94.     int prevY = tailY[0];
  95.     int prev2X, prev2Y;
  96.     tailX[0] = x;
  97.     tailY[0] = y;
  98.     for (int i = 1; i < nTail; i++) {
  99.         prev2X = tailX[i];
  100.         prev2Y = tailY[i];
  101.         tailX[i] = prevX;
  102.         tailY[i] = prevY;
  103.         prevX = prev2X;
  104.         prevY = prev2Y;
  105.     }
  106.     switch (dir)
  107.     {
  108.     case LEFT:
  109.         x--;
  110.         break;
  111.     case RIGHT:
  112.         x++;
  113.         break;
  114.     case DOWN:
  115.         y++;
  116.         break;
  117.     case UP:
  118.         y--;
  119.         break;
  120.     }
  121.     /*if (x > widht-2  || x < 0 || y > height-2 || y < 0) {
  122.         gameOver = true;
  123.     }*/
  124.     if (x >= widht - 1) {
  125.         x = 0;
  126.     }
  127.     else if (x < 0) {
  128.         x = widht - 2;
  129.     }
  130.     if (y >= height) {
  131.         y = 0;
  132.     }
  133.     else if (y < 0) {
  134.         y = height-1;
  135.     }
  136.  
  137.     for (int i = 0; i < nTail; i++) {
  138.         if (tailX[i] == x && tailY[i] == y) {
  139.             gameOver = true;
  140.         }
  141.     }
  142.  
  143.     if (x == fruitX && y == fruitY) {
  144.         score+=10;
  145.         fruitX = rand() % widht - 1;
  146.         fruitY = rand() % height;
  147.         nTail++;
  148.     }
  149. }
  150.  
  151.  
  152. int main() {
  153.     setlocale(LC_ALL, "Rus");
  154.     setup();
  155.     while (gameOver == false) {
  156.         draw();
  157.         input();
  158.         logic();
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment