Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <sstream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. #define UP_KEY 'w'
  10. #define DOWN_KEY 's'
  11. #define LEFT_KEY 'a'
  12. #define RIGHT_KEY 'd'
  13.  
  14. //Ascii art title and Menu to select gameplay
  15. void menu()
  16. {
  17.     cout << R"(
  18. _______  _      _______  _      _______ _______  _______  _______  _______
  19. (  ____ \( (    /|(  ___  )| \  /\(  ____ \  (  ____ \(  ___  )(    )(  ____ \
  20. | ( \/|  \  ( || (   ) ||  \  / /| (    \/  | ( \/| (   ) || () () || ( \/
  21. | (_____ |   \ | || (___) ||  (_/ / | (__   | |     | (___) || || || || (__
  22. (_____  )| (\ \) ||  ___  ||   _ (  |  __)  | | ____ |  ___  || |(_)| ||  __)  
  23.     ) || | \   || (   ) ||  ( \ \ | (       | | \_  )| (   ) || |   | || (     
  24. /\____) || )  \  || )   ( ||  /  \ \| (____/\  | (___) || )   ( || )   ( || (____/\
  25. \_______)|/ )_)|/   \||_/   \/(_______/  (_______)|/    \||/    \|(_______/  
  26. )" << std::endl;
  27.     cout << "\n-------------------------------------------------\n";
  28.     cout << "Select your gamemode:\n\n";
  29.     cout << "\t-Enter 1 play the Snake Game\n";
  30.     cout << "\t-Enter 2 to read the rules\n";
  31.     cout << "\t-Enter 3 to exit the game\n";
  32.     cout << "\n-------------------------------------------------\n";
  33.     cout << "\tYour Choice: ";
  34. }
  35.  
  36. void clearScreen(int newline)
  37. {
  38.     for (int y = 0; y < newline; y++)
  39.     {
  40.         cout << "\n" << endl;
  41.     }
  42. }
  43.  
  44. /*//snake mouvement
  45. int playerMovement(char WASD, int v, int h)
  46. {
  47.     char ok = 'y';
  48.     //allows the player to move
  49.     do
  50.     {
  51.         switch (WASD)
  52.         {
  53.         case UP_KEY:
  54.             v = v - 1;
  55.             ok = 'y';
  56.             return v;
  57.             break;
  58.         case DOWN_KEY:
  59.             v = v + 1;
  60.             ok = 'y';
  61.             return v;
  62.             break;
  63.         case LEFT_KEY:
  64.             h = h - 1;
  65.             ok = 'y';
  66.             return h;
  67.             break;
  68.         case RIGHT_KEY:
  69.             h = h + 1;
  70.             ok = 'y';
  71.             return h;
  72.             break;
  73.         default:
  74.             cout << "\nPlease enter a vaild option: ";
  75.             cin >> WASD;
  76.             cout << '\n\n';
  77.             ok = 'n';
  78.             break;
  79.         }
  80.     } while (ok == 'n');
  81. }
  82. */
  83.  
  84.  
  85. //Contains vectors, the mouvements,
  86. class Snake //sneaky boy
  87. {
  88. public:
  89.  
  90.     Snake()
  91.     {
  92.         snakePos.push_back(make_pair(0, 0));
  93.  
  94.     }
  95.  
  96.     vector <pair<int, int >> getPos() const
  97.     {
  98.         return snakePos;
  99.     }
  100.  
  101.     Snake(int x, int y)
  102.     {
  103.         snakePos.push_back(make_pair(x, y));
  104.     }
  105.  
  106. private:
  107.     vector <pair<int, int>> snakePos;
  108. };
  109.  
  110. /*//Contains food for Snake
  111. class Mouse
  112. {
  113. public:
  114.  
  115.     Mouse()
  116.     {
  117.         mousePos.push_back();
  118.  
  119.     }
  120.  
  121.     vector <int> getPos() const
  122.     {
  123.         return mousePos;
  124.     }
  125.  
  126.     Mouse()
  127.     {
  128.         mousePos.push_back();
  129.     }
  130.  
  131. private:
  132.     vector <int> mousePos;
  133. };
  134. */
  135.  
  136. //Contains the empty grid, can make several grids
  137. class Board
  138. {
  139. public:
  140.  
  141.     //Default constructor
  142.     Board()
  143.     {
  144.         width = 50;
  145.         height = 50;
  146.         int randX = (rand() % (width - 1 - 1 + 1)) + 1;
  147.         int randY = (rand() % (height - 1 - 1 + 1)) + 1;
  148.         snake = Snake(randX, randY);
  149.     }
  150.  
  151.     //Overload constructor, change size of board if I have time
  152.     Board(int defaultWidth, int defaultHeight)
  153.     {
  154.         height = defaultHeight;
  155.         width = defaultWidth;
  156.         int randX = (rand() % (width - 1 - 1 + 1)) + 1;
  157.         int randY = (rand() % (height - 1 - 1 + 1)) + 1;
  158.         snake = Snake(randX, randY);
  159.     }
  160.  
  161.     //Height Getter and Setter
  162.     void hSetter(int newHeight) { height = newHeight; }
  163.     int hGetter() { return height; }
  164.  
  165.     //Function to draw everything
  166.     void draw()
  167.     {
  168.         clearScreen(height);
  169.         //Creates Rectangle and Generates Snake
  170.         for (int i = 0; i < width; i++) //Loop
  171.         {
  172.             for (int j = 0; j < height; j++) //Iterates
  173.             {
  174.                 for (int x = 0; x < snake.getPos().size(); ++x)
  175.                 {
  176.                     if (snake.getPos()[x].first == i && snake.getPos()[x].second == j)
  177.                     {
  178.                         cout << "o"; //draws the Head of Snake
  179.                     }
  180.                     //Draw walls if it detects, if not do nothing
  181.                     else if (i == 0 || i == width - 1 ||
  182.                         j == 0 || j == height - 1)
  183.                     {
  184.                         cout << "|"; //Adds wall if variable == to width or height
  185.                     }
  186.                     else
  187.                         cout << " ";
  188.                 }
  189.             }
  190.             cout << endl;
  191.         }
  192.     }
  193.  
  194. //Always stay the same
  195. private:
  196.     int width;
  197.     int height;
  198.     Snake snake;
  199. };
  200.  
  201. //Everything is put together here
  202. int main()
  203. {
  204.     //calls menu
  205.     menu();
  206.     //Current time in mm
  207.     srand(time(NULL));
  208.     //Prints board
  209.     Board mainBoard;
  210.     mainBoard.draw();
  211.     getchar();
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement