Advertisement
Guest User

Untitled

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