Advertisement
Felanpro

Snake Game

Mar 14th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. int const width = 20;
  10. int const height = 20;
  11.  
  12. int playerX = 10, playerY = 10;
  13. int fruitX = rand() % 20;
  14. int fruitY = rand() % 20;
  15. int direction = 10;
  16.  
  17. int tailX[100],  tailY[100];
  18. int nTail; //Length of the tail
  19.  
  20. int end_game = 0;
  21. int points = 0;
  22.  
  23. int y;
  24. int x;
  25.  
  26. bool print = false;
  27.  
  28. void draw_arena_and_character()
  29. {
  30.     for (int y = 0; y < height; y++)
  31.     {
  32.         for (int x = 0; x < width; x++)
  33.         {
  34.             if (playerX == 0)
  35.             {
  36.                 direction = 4;
  37.             }
  38.             else if (playerX == 19)
  39.             {
  40.                 direction = 2;
  41.             }
  42.             else if (playerY == 0)
  43.             {
  44.                 direction = 3;
  45.             }
  46.             else if (playerY == 19)
  47.             {
  48.                 direction = 1;
  49.             }
  50.  
  51.             if (y == 0 || y == 19)
  52.             {
  53.                 cout << "#";
  54.             }
  55.             else if (x == 0 || x == 19)
  56.             {
  57.                 cout << "#";
  58.             }
  59.             else if (y == playerY && x == playerX)
  60.             {
  61.                 cout << "O";
  62.             }
  63.             else if (y == fruitY && x == fruitX)
  64.             {
  65.                 cout << "F";
  66.             }
  67.             else
  68.             {
  69.                 print = false;
  70.                 for (int k = 0; k < nTail; k++)
  71.                 {
  72.                     if (x == tailX[k] && y == tailY[k])
  73.                     {
  74.                         cout << "o";
  75.                         print = true;
  76.                     }
  77.                 }
  78.                 if (!print)
  79.                     cout << " ";
  80.             }
  81.         }
  82.         cout << endl;
  83.     }
  84.     cout << "Points: " << points;
  85.  
  86.     Sleep(100);
  87.  
  88.     system("cls");
  89. }
  90.  
  91. void input()
  92. {
  93.     if (_kbhit())
  94.     {
  95.         char input_variable;
  96.         input_variable = _getch();
  97.         if (input_variable == 'w')
  98.         {
  99.             if (direction == 3)
  100.                 ;
  101.             else
  102.                 direction = 1;
  103.         }
  104.         else if (input_variable == 'a')
  105.         {
  106.             if (direction == 4)
  107.                 ;
  108.             else
  109.                 direction = 2;
  110.         }
  111.         else if (input_variable == 's')
  112.         {
  113.             if (direction == 1)
  114.                 ;
  115.             else
  116.                 direction = 3;
  117.         }
  118.         else if (input_variable == 'd')
  119.         {
  120.             if (direction == 2)
  121.                 ;
  122.             else
  123.                 direction = 4;
  124.         }
  125.     }
  126. }
  127.  
  128. void logic()
  129. {
  130.  
  131.     if (playerX == fruitX && playerY == fruitY)
  132.     {
  133.         nTail++;
  134.         points += 10;
  135.         srand(time(NULL));
  136.         fruitY = (rand() % 18) + 1;
  137.         fruitX = (rand() % 18) + 1;
  138.     }
  139.  
  140.     int prevX = tailX[0];
  141.     int prevY = tailY[0];
  142.     int prev2X;
  143.     int prev2Y;
  144.     tailX[0] = x;
  145.     tailY[0] = y;
  146.  
  147.     for (int i = 1; i < nTail; i++)
  148.     {
  149.         prev2X = tailX[i];
  150.         prev2Y = tailY[i];
  151.         tailX[i] = prevX;
  152.         tailY[i] = prevY;
  153.         prevX = prev2X;
  154.         prevY = prev2Y;
  155.     }
  156.  
  157.     //Check death
  158.     if (nTail > 0)
  159.     {
  160.         for (int m = 0; m < nTail; m++)
  161.         {
  162.             if (tailX[m] == playerX && tailY[m] == playerY)
  163.             {
  164.                 end_game = 1;
  165.             }
  166.         }
  167.     }
  168.  
  169.     /*
  170.     tailX[3] = tailX[2];
  171.     tailY[3] = tailY[2];
  172.  
  173.     tailX[2] = tailX[1];
  174.     tailY[2] = tailY[1];
  175.  
  176.     tailX[1] = tailX[0];
  177.     tailY[1] = tailY[0];
  178.  
  179.     tailX[0] = playerX;
  180.     tailY[0] = playerY;
  181.     */
  182.  
  183.     tailX[0] = playerX;
  184.     tailY[0] = playerY;
  185.  
  186.     /*
  187.     if (nTail > 0)
  188.     {
  189.         for (int k = nTail; k >= 1; k--)
  190.         {
  191.             tailX[k] =
  192.         }
  193.     }
  194.     */
  195.  
  196.     if (direction == 1)
  197.     {
  198.         playerY--;
  199.     }
  200.     else if (direction == 2)
  201.     {
  202.         playerX--;
  203.     }
  204.     else if (direction == 3)
  205.     {
  206.         playerY++;
  207.     }
  208.     else if (direction == 4)
  209.     {
  210.         playerX++;
  211.     }
  212. }
  213.  
  214. int main()
  215. {
  216.    
  217.     while (end_game == 0)
  218.     {
  219.         draw_arena_and_character();
  220.         input();
  221.         logic();
  222.     }
  223.  
  224.     system("cls");
  225.     cout << "You died!" << endl;
  226.     cout << "Points: " << points;
  227.  
  228.     int pause; cin >> pause; //Pause the program
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement