SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. //For the menu
  11. int choice;
  12.  
  13. //The XY coordinate for the snake's head
  14. int headPosX, headPosY;
  15. int mouseX, mouseY;
  16.  
  17. //The height and width of the board
  18. const int height = 50;
  19. const int width = 50;
  20.  
  21. //Boolean so game runs on a loop and breaks once game is over (exits game)
  22. bool gameOver;
  23.  
  24. //To hold several variables (left = 1 right = 2 etc)
  25. enum eDirection : int { STOP = 0, LEFT, RIGHT, UP, DOWN};
  26. enum eDirection dir;
  27.  
  28. //Array for tail with fixed max length
  29. int tailX[100], tailY[100];
  30. int nTail;
  31. //Main setup for putting the snake and mouse on grid
  32.  
  33. void setup()
  34. {
  35.  gameOver = false;
  36.     dir = STOP;
  37.  
  38.  //random snake pos on grid
  39.  headPosX = width / 2;
  40.  headPosY = height / 2;
  41.  
  42.  //random generates the mosue
  43.  mouseX = rand() % width;
  44.  mouseY = rand() % height;
  45. }
  46. void draw()
  47. {
  48.     system("cls"); //Clear screen function
  49.     for (int i = 0; i < width + 2; i++)
  50.     cout << "#";
  51.     cout << endl;
  52.     //draws the board
  53.     for (int i = 0; i < height; i++) //draws side boarders of grid
  54.     {
  55.         for (int j = 0; j < width; j++) //draws top boarders of grid
  56.         {
  57.             if (j == 0)
  58.                 cout << "#";
  59.    //prints the head on grid
  60.             if (i == headPosY && j == headPosX)
  61.                 cout << "O";
  62.    //prints the mouse on grid
  63.             else if (i == mouseY && j == mouseX)
  64.                 cout << "*";
  65.    else
  66.             {
  67.                 bool draw = false;
  68.                 //Tail will be made once snake eats mouse
  69.                 for (int k = 0; k < nTail; k++)
  70.                 {
  71.                     if (tailX[k] == j && tailY[k] == i)
  72.                     {
  73.                         cout << "o";
  74.                         draw = true;
  75.                     }
  76.                 }
  77.     //if there is nothing to draw, put space
  78.                 if (!draw)
  79.                     cout << " ";
  80.             }
  81.             if (j == width - 1)
  82.                 cout << "#";
  83.         }
  84.         cout << endl;
  85.     }
  86.     for (int i = 0; i < width + 2; i++) //use the width + 2 since its missing it in the grid.
  87.         cout << "#";
  88.     cout << endl;
  89. }
  90.  
  91. //Snake movement
  92. void input()
  93. {
  94.  if(_kbhit()) //if a key is pressed
  95.  {
  96.   switch(_getch()) //go find the key that corresponds with dir
  97.  {
  98.   //Movement keys on keyboard
  99.   case 'a':
  100.    dir = LEFT;
  101.    break;
  102.  
  103.   case 'd':
  104.    dir = RIGHT;
  105.    break;
  106.    
  107.    case 'w':
  108.    dir = UP;
  109.    break;
  110.  
  111.   case 's':
  112.    dir = DOWN;
  113.    break;
  114.  
  115.   case 'x':
  116.    gameOver; //boolean of "gameOver" is called so game ends
  117.    break;
  118.   }
  119.  }
  120. }
  121.  
  122. void logic()
  123. {
  124.     //2 D array for tail
  125.  //first element of tail
  126.     int prevX = tailX[0];
  127.     int prevY = tailY[0];
  128.     int prev2X, prev2Y;
  129.  tailX[0] = headPosX;
  130.     tailY[0] = headPosY;
  131.     for (int i = 1; i < nTail; i++) //adds to body
  132.     {
  133.         //look at first segment  ((CURRENT POS))
  134.         prev2X = tailX[i];
  135.         prev2Y = tailY[i];
  136.         tailX[i] = prevX;
  137.         tailY[i] = prevY;
  138.         //update the snake length
  139.         prevX = prev2X;
  140.         prevY = prev2Y;
  141.     }
  142.  switch (dir)
  143.  {
  144.   case LEFT:
  145.   headPosX--;
  146.   break;
  147.  
  148.   case RIGHT:
  149.   headPosX++;
  150.   break;
  151.  
  152.   case UP:
  153.   headPosY--;
  154.   break;
  155.  
  156.   case DOWN:
  157.   headPosY++;
  158.   break;
  159.  
  160.   default:
  161.   break;
  162.  }
  163.  //if (x > width || x < 0 || y > height || y < 0)
  164.     gameOver = true;
  165.     if (headPosX >= width) headPosX = 0;
  166.     else if (headPosX < 0) headPosX = width - 1;
  167.     if (headPosY >= height) headPosY = 0;
  168.     else if (headPosY < 0) headPosY = height - 1;
  169.  //if snake hits itself
  170.     for (int i = 0; i < nTail; i++)
  171.         if (tailX[i] == headPosX && tailY[i] == headPosY)
  172.             gameOver = true;
  173.  //Once snake eats mouse, it will randomly go somewhere else and relocate
  174.     if (headPosX == mouseX && headPosY == mouseY)
  175.     {
  176.         mouseX = rand() % width;
  177.         mouseY = rand() % height;
  178.         nTail++;
  179.     }
  180. }
  181.  
  182. //Main instructions and goals of the game
  183. void instructions()
  184. {
  185.  cout << "    ____           __                  __  _                 " << endl;
  186.  cout << "     /  _/___  _____/ /________  _______/ /_(_)___  ____  _____" << endl;
  187.  cout << "    / // __ \/ ___/ __/ ___/ / / / ___/ __/ / __ \/ __ \/ ___/" << endl;
  188.  cout << "  _/ // / / (__  ) /_/ /  / /_/ / /__/ /_/ / /_/ / / / (__  )"  << endl;
  189.  cout << " /___/_/ /_/____/\__/_/   \__,_/\___/\__/_/\____/_/ /_/____/" << endl;
  190.  cout << "*****************************************************************************" << endl;
  191.  cout << " - Use the WASD keys (make sure caps lock is off) to move around on the grid " << endl;
  192.  cout << " - Do not hit a wall " << endl;
  193.  cout << " - Do not hit your own body " << endl;
  194.  cout << "*****************************************************************************" << endl;
  195.  cout << "    ______            __    " << endl;
  196.  cout << "   / ____/___  ____ _/ /____ " << endl;
  197.  cout << "  / / __/ __ \/ __ `/ / ___/ " << endl;
  198.  cout << " / /_/ / /_/ / /_/ / (__  ) " << endl;
  199.  cout << " \____/\____/\__,_/_/____/ " << endl;
  200.  cout << "*****************************************************************************" << endl;
  201.  cout << " - Go around the grid and try to eat as many mice as you can " << endl;
  202.  cout << " - Do not get frustrated with yourself " << endl;
  203.  cout << " - Have fun playing the game" << endl;
  204.  cout << "*****************************************************************************" << endl;
  205. }
  206. //Where the user will be prompted at the beginning
  207. void menu()
  208. {
  209.  
  210.  cout << "    _____             _          _____ " <<endl;                    
  211.  cout << "   / ____|           | |        / ____|" << endl;                    
  212.  cout << "  | (___  _ __   __ _| |_____  | | __  __ _ _ __ ___    ___" << endl;
  213.  cout << "  \___ \| '_ \ / _` | |/ / _ \ | | |_ |/ _` | '_ ` _ \ / _ \ " << endl;
  214.  cout << "  ____) | | | | (_| |   <  __/ | |__| | (_| | | | | | |  __/" << endl;
  215.  cout << " |_____/|_| |_|\__,_|_|\_\___|  \_____|\__,_|_| |_| |_|\___|" << endl;
  216. cout << " \t\nCreated by Olivia Frith " << endl;
  217.  
  218. cout << "*************************************************" << endl;
  219. cout << " Choose 1 to play the Snake Game " << endl;
  220. cout << " Choose 2 to see the game instructions and goals " << endl;
  221. cout << " Choose 3 to exit the game " << endl;
  222. cout << "*************************************************" << endl;
  223. cout << " Your choice:";
  224. cin >> choice;
  225. cout << "\n\n\n\n";
  226.  
  227. //Shows instructions and game goals
  228.  if(choice == 2)
  229.  {
  230.      instructions();
  231.      system("pause");
  232.  }
  233.  
  234.  //Exits game
  235.  if(choice == 3)
  236.  {
  237.     exit (EXIT_SUCCESS);
  238.  }
  239. }
  240.  
  241.  
  242. int main()
  243. {
  244.    
  245.     setup();
  246.     menu();
  247.     do
  248.     {
  249.         draw();
  250.         input();
  251.         logic();
  252.         this_thread::sleep_for(std::chrono::milliseconds(10));
  253.     }while(!gameOver == false);
  254.     return 0;
  255. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top