Vita94

NVitanovic C++ Tutorial 18 - Simple Snake Game

Apr 8th, 2018
8,964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4. using namespace std;
  5. bool gameOver;
  6. const int width = 20;
  7. const int height = 20;
  8. int x, y, fruitX, fruitY, score;
  9. int tailX[100], tailY[100];
  10. int nTail;
  11. enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
  12. eDirecton dir;
  13. void Setup()
  14. {
  15.     gameOver = false;
  16.     dir = STOP;
  17.     x = width / 2;
  18.     y = height / 2;
  19.     fruitX = rand() % width;
  20.     fruitY = rand() % height;
  21.     score = 0;
  22. }
  23. void Draw()
  24. {
  25.     system("cls"); //system("clear");
  26.     for (int i = 0; i < width+2; i++)
  27.         cout << "#";
  28.     cout << endl;
  29.  
  30.     for (int i = 0; i < height; i++)
  31.     {
  32.         for (int j = 0; j < width; j++)
  33.         {
  34.             if (j == 0)
  35.                 cout << "#";
  36.             if (i == y && j == x)
  37.                 cout << "O";
  38.             else if (i == fruitY && j == fruitX)
  39.                 cout << "F";
  40.             else
  41.             {
  42.                 bool print = false;
  43.                 for (int k = 0; k < nTail; k++)
  44.                 {
  45.                     if (tailX[k] == j && tailY[k] == i)
  46.                     {
  47.                         cout << "o";
  48.                         print = true;
  49.                     }
  50.                 }
  51.                 if (!print)
  52.                     cout << " ";
  53.             }
  54.                  
  55.  
  56.             if (j == width - 1)
  57.                 cout << "#";
  58.         }
  59.         cout << endl;
  60.     }
  61.  
  62.     for (int i = 0; i < width+2; i++)
  63.         cout << "#";
  64.     cout << endl;
  65.     cout << "Score:" << score << endl;
  66. }
  67. void Input()
  68. {
  69.     if (_kbhit())
  70.     {
  71.         switch (_getch())
  72.         {
  73.         case 'a':
  74.             dir = LEFT;
  75.             break;
  76.         case 'd':
  77.             dir = RIGHT;
  78.             break;
  79.         case 'w':
  80.             dir = UP;
  81.             break;
  82.         case 's':
  83.             dir = DOWN;
  84.             break;
  85.         case 'x':
  86.             gameOver = true;
  87.             break;
  88.         }
  89.     }
  90. }
  91. void Logic()
  92. {
  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.     {
  100.         prev2X = tailX[i];
  101.         prev2Y = tailY[i];
  102.         tailX[i] = prevX;
  103.         tailY[i] = prevY;
  104.         prevX = prev2X;
  105.         prevY = prev2Y;
  106.     }
  107.     switch (dir)
  108.     {
  109.     case LEFT:
  110.         x--;
  111.         break;
  112.     case RIGHT:
  113.         x++;
  114.         break;
  115.     case UP:
  116.         y--;
  117.         break;
  118.     case DOWN:
  119.         y++;
  120.         break;
  121.     default:
  122.         break;
  123.     }
  124.     //if (x > width || x < 0 || y > height || y < 0)
  125.     //  gameOver = true;
  126.     if (x >= width) x = 0; else if (x < 0) x = width - 1;
  127.     if (y >= height) y = 0; else if (y < 0) y = height - 1;
  128.  
  129.     for (int i = 0; i < nTail; i++)
  130.         if (tailX[i] == x && tailY[i] == y)
  131.             gameOver = true;
  132.  
  133.     if (x == fruitX && y == fruitY)
  134.     {
  135.         score += 10;
  136.         fruitX = rand() % width;
  137.         fruitY = rand() % height;
  138.         nTail++;
  139.     }
  140. }
  141. int main()
  142. {
  143.     Setup();
  144.     while (!gameOver)
  145.     {
  146.         Draw();
  147.         Input();
  148.         Logic();
  149.         Sleep(10); //sleep(10);
  150.     }
  151.     return 0;
  152. }
Add Comment
Please, Sign In to add comment