Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <sstream>
  5. #include <vector>
  6. #include <windows.h>
  7. #include <chrono>
  8. #include <thread>
  9. #include <cstdlib>
  10.  
  11. using namespace std;
  12.  
  13. #define UP_KEY 0x57 // W
  14. #define DOWN_KEY 0x53 // S
  15. #define LEFT_KEY 0x41 // A
  16. #define RIGHT_KEY 0x44 // D
  17.  
  18. //Represent the state of Snake
  19. enum class direction
  20. {
  21.     UP,
  22.     DOWN,
  23.     LEFT,
  24.     RIGHT,
  25. };
  26.  
  27. //Generates the mouse in the grid without generating under a snake
  28. pair<int, int> randMousePos(vector <pair<int, int >> snakePos, int width, int height)
  29. {
  30.     for (;;)
  31.     {
  32.         int randX = (rand() % (width - 1 - 1 + 1)) + 1;
  33.         int randY = (rand() % (height - 1 - 1 + 1)) + 1;
  34.         if (find(snakePos.begin(), snakePos.end(), make_pair(randX, randY)) == snakePos.end())
  35.         {
  36.             return make_pair(randX, randY);
  37.         }
  38.     }
  39. }
  40.  
  41. //Contains vectors, the mouvements,
  42. class Snake //sneaky boy
  43. {
  44. public:
  45.  
  46.     Snake()
  47.     {
  48.         snakePos.push_back(make_pair(0, 0));
  49.         snakeMovement = direction::RIGHT;
  50.     }
  51.  
  52.     vector <pair<int, int >> getPos() const
  53.     {
  54.         return snakePos;
  55.     }
  56.  
  57.     Snake(int x, int y)
  58.     {
  59.         snakePos.push_back(make_pair(x, y));
  60.         snakeMovement = direction::RIGHT;
  61.     }
  62.  
  63.     void setMovement(direction move)
  64.     {
  65.         snakeMovement = move;
  66.     }
  67.  
  68.     //Movement thread (runs at same time as the board)
  69.     void processInputs()
  70.     {
  71.         if (GetKeyState(UP_KEY) & 0x8000)
  72.         {
  73.             setMovement(direction::UP);
  74.             //cout << " Pressed up" << endl;
  75.         }
  76.         else if (GetKeyState(DOWN_KEY) & 0x8000)
  77.         {
  78.             setMovement(direction::DOWN);
  79.             //cout << " Pressed down" << endl;
  80.         }
  81.         else if (GetKeyState(LEFT_KEY) & 0x8000)
  82.         {
  83.             setMovement(direction::LEFT);
  84.             //cout << " Pressed left" << endl;
  85.         }
  86.         else if (GetKeyState(RIGHT_KEY) & 0x8000)
  87.         {
  88.             setMovement(direction::RIGHT);
  89.             //cout << " Pressed right" << endl;
  90.         }
  91.     }
  92.  
  93. private:
  94.     vector <pair<int, int>> snakePos;
  95.     direction snakeMovement;
  96. };
  97.  
  98.  
  99. //Contains food for Snake
  100. class Mouse
  101. {
  102. public:
  103.  
  104.     Mouse() { posY = 0; posX = 0; }
  105.  
  106.     Mouse(int height, int width)
  107.     {
  108.         posX = (rand() % (width - 1 - 1 + 1)) + 1; //counts from row 2 of the board to 47 (ignores the walls)
  109.         posY = (rand() % (height - 1 - 1 + 1)) + 1;
  110.     }
  111.  
  112.     int getPosY() const { return posY; }
  113.  
  114.     int getPosX() const { return posX; }
  115.  
  116. private:
  117.     int posY;
  118.     int posX;
  119. };
  120.  
  121. //Contains the empty grid, can make several grids
  122. class Board
  123. {
  124. public:
  125.  
  126.     //Default constructor
  127.     Board()
  128.     {
  129.         width = 50;
  130.         height = 50;
  131.         int randX = (rand() % (width - 2 - 1 + 1)) + 1;
  132.         int randY = (rand() % (height - 2 - 1 + 1)) + 1;
  133.         snake = Snake(randX, randY);
  134.         pair<int, int> mousePos = randMousePos(snake.getPos(), width, height);
  135.         mouse = Mouse(mousePos.first, mousePos.second);
  136.     }
  137.  
  138.     //Overload constructor, change size of board if I have time
  139.     Board(int defaultWidth, int defaultHeight)
  140.     {
  141.         height = defaultHeight;
  142.         width = defaultWidth;
  143.         int randX = (rand() % (width - 1 - 1 + 1)) + 1;
  144.         int randY = (rand() % (height - 1 - 1 + 1)) + 1;
  145.         snake = Snake(randX, randY);
  146.         pair<int, int> mousePos = randMousePos(snake.getPos(), width, height);
  147.         mouse = Mouse(mousePos.first, mousePos.second);
  148.     }
  149.  
  150.     //Returns snake object so it can be used
  151.     Snake& getSnake()
  152.     {
  153.         return snake;
  154.     }
  155.  
  156.     void process()
  157.     {
  158.         snake.processInputs();
  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); (Not working?)
  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.                 if (i == mouse.getPosX() && j == mouse.getPosY())
  175.                 {
  176.                     cout << "*";
  177.                 }
  178.                 else
  179.                 {
  180.                     for (int x = 0; x < snake.getPos().size(); ++x)
  181.                     {
  182.                         if (i == 0 || i == width - 1 ||
  183.                             j == 0 || j == height - 1)
  184.                         {
  185.                             cout << "#"; //Adds wall if variable == to width or height
  186.                         }
  187.                         else if (snake.getPos()[x].first == i && snake.getPos()[x].second == j)
  188.                         {
  189.                             cout << "o"; //draws the Head of Snake
  190.                         }
  191.                         //Draw walls if it detects, if not do nothing
  192.                         else
  193.                             cout << " ";
  194.                     }
  195.                 }
  196.             }
  197.             cout << endl;
  198.         }
  199.     }
  200.  
  201. private:
  202.     int width;
  203.     int height;
  204.     Snake snake;
  205.     Mouse mouse;
  206. };
  207.  
  208. void instructions()
  209. {
  210.     cout << R"(
  211.   ______            __    
  212.  / ____/___  ____ _/ /____
  213. / / __/ __ \/ __ `/ / ___/
  214. / /_/ / /_/ / /_/ / (__  )
  215. \____/\____/\__,_/_/____/                      
  216. )" << endl;
  217.  
  218.     cout << "\n-------------------------------------------------\n";
  219.     cout << " - Eat as many apples as you can" << endl;
  220.     cout << " - Pay attention --> Do not distract yourself or get distracted" << endl;
  221.     cout << " - Have a good time and enjoy the game!" << endl;
  222.     cout << "\n-------------------------------------------------\n";
  223.  
  224.     cout << R"(
  225.    ____           __                  __  _                
  226.   /  _/___  _____/ /________  _______/ /_(_)___  ____  _____
  227.   / // __ \/ ___/ __/ ___/ / / / ___/ __/ / __ \/ __ \/ ___/
  228. _/ // / / (__  ) /_/ /  / /_/ / /__/ /_/ / /_/ / / / (__  )
  229. /___/_/ /_/____/\__/_/   \__,_/\___/\__/_/\____/_/ /_/____/  
  230. )" << endl;
  231.  
  232.     cout << "\n-------------------------------------------------\n";
  233.     cout << " - Use WASD to move around the grid" << endl;
  234.     cout << " - Do not hit a wall = Game Over" << endl;
  235.     cout << " - Do not run into yourself = Game over" << endl;
  236.     cout << "\n-------------------------------------------------\n";
  237. }
  238.  
  239. //Ascii art title and Menu to select gameplay
  240. void menu()
  241. {
  242.     cout << R"(
  243.   _____             _           _____                      
  244.  / ____|           | |         / ____|                    
  245. | (___  _ __   __ _| | _____  | |  __  __ _ _ __ ___   ___
  246.  \___ \| '_ \ / _` | |/ / _ \ | | |_ |/ _` | '_ ` _ \ / _ \
  247.  ____) | | | | (_| |   <  __/ | |__| | (_| | | | | | |  __/
  248. |_____/|_| |_|\__,_|_|\_\___|  \_____|\__,_|_| |_| |_|\___|                                                                                                            
  249. )" << endl;
  250.  
  251.     int choice;
  252.     cout << "\n-------------------------------------------------\n";
  253.     cout << "Select your gamemode:\n\n";
  254.     cout << "\t-Enter 1 play the Snake Game\n";
  255.     cout << "\t-Enter 2 to see the instructions and rules\n";
  256.     cout << "\t-Enter 3 to exit the game\n";
  257.     cout << "\n-------------------------------------------------\n";
  258.     cout << "\tYour Choice: ";
  259.     cin >> choice;
  260.  
  261.     switch (choice)
  262.     {
  263.     case 1:
  264.         int 1 = ;
  265.  
  266.         break;
  267.  
  268.     case 2:
  269.         instructions();
  270.         break;
  271.  
  272.     case 3:
  273.         exit(EXIT_SUCCESS);
  274.     }
  275. }
  276.  
  277. void clearScreen(int newline)
  278. {
  279.     for (int y = 0; y < newline; y++)
  280.     {
  281.         cout << "\n" << endl;
  282.     }
  283. }
  284.  
  285. //Everything is put together here
  286. int main()
  287. {
  288.     srand(time(NULL));
  289.     //SetConsoleTitleW(L"Sneaky sneaky app"); **EXTRA**
  290.     menu(); //calls menu
  291.     Board mainBoard; //Prints board
  292.    
  293.     //Main Game loop
  294.     int lastTime = time(NULL);
  295.     while (true)
  296.     {
  297.         mainBoard.process();
  298.         if (time(NULL) != lastTime)
  299.         {
  300.             mainBoard.draw();
  301.         }
  302.         lastTime = time(NULL);
  303.     }
  304.     getchar();
  305.     return 0;
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement