Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6. //Enumeration for direction
  7. enum EDirecton
  8. {
  9.     Stop = 0,
  10.     Left,
  11.     Right,
  12.     Up,
  13.     Down
  14. };
  15. struct Position
  16. {
  17.     int Row;
  18.     int Col;
  19. };
  20. //Declare variables
  21. bool gameOver;
  22. const int width = 40;
  23. const int height = 20;
  24. Position head;
  25. Position food;
  26. int score;
  27. Position snake[200];
  28. int snakeSize;
  29. EDirecton direction;
  30. Position obstacle;
  31.  
  32. void SetUp()
  33. {
  34.     gameOver = false;
  35.     direction = Stop;
  36.     head.Col = width / 2;
  37.     head.Row = height / 2;
  38.     food.Col = rand() % width;
  39.     food.Row = rand() % height;
  40.     score = 0;
  41.     obstacle.Col = rand() % width;
  42.     obstacle.Row = rand() % height;
  43. }
  44. //Function for changing the cursor coordinates
  45. void SetCursorPointer(int col, int row)
  46. {
  47.     static HANDLE hStdout = NULL;
  48.     COORD coord;
  49.  
  50.     coord.X = col;
  51.     coord.Y = row;
  52.  
  53.     if (!hStdout)
  54.     {
  55.         hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  56.     }
  57.  
  58.     SetConsoleCursorPosition(hStdout,coord);
  59. }
  60. void Draw()
  61. {
  62.     //Draw upper border
  63.     SetCursorPointer(0, 0);
  64.     for (int col = 0; col < width + 2; col++)
  65.     {
  66.          cout << "#";
  67.     }
  68.     cout << endl;
  69.  
  70.     //Draw field
  71.     for (int row = 0; row < height; row ++)
  72.     {
  73.         for (int col = 0; col < width; col++)
  74.         {
  75.             if (col == 0)
  76.             {
  77.                 cout << "#";
  78.             }
  79.             //Draw snake head
  80.             if (row == head.Row && col == head.Col)
  81.             {
  82.                  cout << "O";
  83.             }
  84.             //Draw food
  85.             else if (row == food.Row && col == food.Col)
  86.             {
  87.                 cout << "@";
  88.             }
  89.             //Draw Obstacle
  90.             else if (row == obstacle.Row && col == obstacle.Col)
  91.             {
  92.                 cout << "X";
  93.             }
  94.             else
  95.             {
  96.                 //Draw snake body
  97.                 bool print = false;
  98.                 for (int index = 0; index < snakeSize; index++)
  99.                 {
  100.                     if (snake[index].Col == col && snake[index].Row == row)
  101.                     {
  102.                         cout << "o";
  103.                         print = true;
  104.                     }
  105.                 }
  106.                 if (!print)
  107.                 {
  108.                     cout << " ";
  109.                 }
  110.             }
  111.  
  112.  
  113.             if (col == width - 1)
  114.             {
  115.                 cout << "#";
  116.             }
  117.         }
  118.         cout << endl;
  119.     }
  120.  
  121.     //Draw bottom border
  122.     for (int col = 0; col < width + 2; col++)
  123.     {
  124.         cout << "#";
  125.     }
  126.     cout << endl;
  127.     SetCursorPointer(15, 22);
  128.     cout << "Score:" << score << endl;
  129. }
  130. //Function for changing direction
  131. void Input()
  132. {
  133.     //The direction is changed when a key is pressed
  134.     if (_kbhit())
  135.     {
  136.         switch (_getch())
  137.         {
  138.         case 'a':
  139.             direction = Left;
  140.             break;
  141.         case 'd':
  142.             direction = Right;
  143.             break;
  144.         case 'w':
  145.             direction = Up;
  146.             break;
  147.         case 's':
  148.             direction = Down;
  149.             break;
  150.         case 'x':
  151.             gameOver = true;
  152.             break;
  153.         }
  154.     }
  155. }
  156. //Implement thе snake movement
  157. void Logic()
  158. {
  159.     Position previous = snake[0];
  160.     Position next;
  161.     snake[0].Col = head.Col;
  162.     snake[0].Row = head.Row;
  163.     for (int index = 1; index < snakeSize; index++)
  164.     {
  165.         next.Col = snake[index].Col;
  166.         next.Row = snake[index].Row;
  167.         snake[index].Col = previous.Col;
  168.         snake[index].Row = previous.Row;
  169.         previous.Col = next.Col;
  170.         previous.Row = next.Row;
  171.     }
  172.     switch (direction)
  173.     {
  174.         case Left:
  175.             head.Col--;
  176.             break;
  177.         case Right:
  178.             head.Col++;
  179.             break;
  180.         case Up:
  181.             head.Row--;
  182.             break;
  183.         case Down:
  184.             head.Row++;
  185.             break;
  186.         default:
  187.             break;
  188.     }
  189.     //Check if the snake have hit the border
  190.     if (head.Col >= width || head.Col < 0 || head.Row >= height || head.Row < 0)
  191.     {
  192.         gameOver = true;
  193.     }
  194.  
  195.     //Check if the snake has eaten its body
  196.     for (int index = 0; index < snakeSize; index++)
  197.     {
  198.         if (snake[index].Col == head.Col & snake[index].Row == head.Row)
  199.         {
  200.             gameOver = true;
  201.         }
  202.     }
  203.  
  204.     //Check if the snake has hit an obstacle
  205.     if (obstacle.Col == head.Col & obstacle.Row == head.Row)
  206.     {
  207.         gameOver = true;
  208.     }
  209.  
  210.     //Check if the snake has eaten the food
  211.     if (head.Col == food.Col && head.Row == food.Row)
  212.     {
  213.         score += 10;
  214.         food.Col = rand() % width;
  215.         food.Row = rand() % height;
  216.         snakeSize++;
  217.     }
  218. }
  219. //Main function where everything starts
  220. int main()
  221. {
  222.     SetUp();
  223.     while (!gameOver)
  224.     {
  225.         Draw();
  226.         Input();
  227.         Logic();
  228.     }
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement