Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #ifndef GAME_HPP
  2. #define GAME_HPP
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <conio.h>
  6. #include <windows.h>
  7.  
  8. class game;
  9.  
  10. class snake
  11. {
  12.     public:
  13.         snake();
  14.         ~snake();
  15.  
  16.         void addScore();
  17.         void addTail();
  18.         int returnScore();
  19.         int returnTail();
  20.  
  21.     private:
  22.         int score;
  23.         int tail;
  24.        
  25. };
  26.  
  27. class game
  28. {
  29.     public:
  30.         game();
  31.         void Graphics();            //Funciton to render the game board
  32.         void GameLogic();           //Function to compute how to operatore the snake
  33.         void Apple();               //Function to generate a new apple randomly on the map
  34.         void Clear();               //Function to clear the screen
  35.         bool appleTouched();        //Function to determine if the apple was consumed by the snake
  36.         bool GameOver();            //Function to determine wether the game has ended or not
  37.  
  38.  
  39.     private:
  40.         snake snake;
  41.         int width;
  42.         int height;
  43.         int x, y, fruitx, fruity;
  44.         bool gameStatus;
  45.         bool appleExists;
  46.         friend class snake;
  47. };
  48.  
  49. /*-------------------------------------------------------------------Snake------------------------------------------------------------------------------------*/
  50. snake::snake()
  51. {
  52.     score = 0;
  53.     tail = 1;
  54. }
  55.  
  56. snake::~snake()
  57. {}
  58.  
  59. void snake::addScore()
  60. {
  61.     score += 10;
  62. }
  63.  
  64. void snake::addTail()
  65. {
  66.     tail += 1;
  67. }
  68.  
  69. int snake::returnScore()
  70. {
  71.     return score;
  72. }
  73. int snake::returnTail()
  74. {
  75.     return tail;
  76. }
  77. /*-------------------------------------------------------------------Game-------------------------------------------------------------------------------------*/
  78.  
  79. game::game()
  80. {
  81.     width = 20;
  82.     height = 20;
  83.     x = width / 2;
  84.     y = height / 2;
  85.     gameStatus = false;
  86.     appleExists = true;
  87.     fruitx = rand() % width;
  88.     fruity = rand() % height;
  89. }
  90.  
  91. void game::Graphics()
  92. {
  93.     system("cls");
  94.     //Printing the board top
  95.     for (int i = 0; i < width + 1; i++)
  96.     {
  97.         std::cout << "#";
  98.     }
  99.     std::cout << std::endl;
  100.     for (int i = 0; i < height; i++)
  101.     {
  102.         for (int j = 0; j < width; j++)
  103.         {
  104.             //Print out sides of the board
  105.             if (j == 0 || j == 19)
  106.             {
  107.                 std::cout << "#";
  108.             }
  109.  
  110.             //Print out Snake
  111.             if (i == y && j == x)
  112.             {
  113.                 std::cout << "O";
  114.             }
  115.  
  116.             //Print out the fruit
  117.             else if (i == fruitx && j == fruity)
  118.             {
  119.                 std::cout << "F";
  120.             }
  121.  
  122.             else
  123.             {
  124.  
  125.                 std::cout << " ";
  126.  
  127.             }
  128.  
  129.         };
  130.         std::cout << std::endl;
  131.     }
  132.     //Print out board bottom
  133.     for (int i = 0; i < width + 1; i++)
  134.     {
  135.         std::cout << "#";
  136.     }
  137.     std::cout << std::endl;
  138.  
  139.     //Print out score and tail length
  140.     std::cout << "Score: " << snake.returnScore();
  141.     std::cout << std::endl;
  142.     std::cout << "Tail Length: " << snake.returnTail();
  143.     std::cout << std::endl;
  144. }
  145.  
  146. void game::GameLogic()
  147. {
  148.     if (_kbhit())
  149.     {
  150.         switch (_getch())
  151.         {
  152.         case 'w':
  153.             y--;
  154.             break;
  155.         case 'd':
  156.             x++;
  157.             break;
  158.         case 's':
  159.             y++;
  160.             break;
  161.         case 'a':
  162.             x--;
  163.             break;
  164.         }
  165.     }
  166.     if (x > width || x < 0 || y > height || y < 0)
  167.     {
  168.         gameStatus = true;
  169.     }
  170.     if (x == fruitx && y == fruity)
  171.     {
  172.         snake.addScore();
  173.         snake.addTail();
  174.     }
  175.            
  176. }
  177.  
  178. void game::Apple()
  179. {
  180.     if(appleExists == false)
  181.     {  
  182.         fruitx = rand() % width;
  183.         fruity = rand() % height;
  184.         appleExists = true;
  185.     }
  186.     else if (appleTouched())
  187.     {
  188.         appleExists == false;
  189.     }
  190. }
  191.  
  192. bool game::appleTouched()
  193. {
  194.     if (x == fruitx && y == fruity)
  195.     {
  196.         snake.addScore();
  197.         snake.addTail();
  198.         return true;
  199.     }
  200. }
  201.  
  202. bool game::GameOver()
  203. {
  204.     return gameStatus;
  205. }
  206.    
  207. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement