Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include < iostream >
  2.  
  3. #include < conio.h >
  4.  
  5. using namespace std;
  6.  
  7. bool gameover;
  8.  
  9. const int width = 20;
  10.  
  11. const int height = 17;
  12.  
  13. int x, y, fruitX, fruitY, score;
  14.  
  15. int tailX[100], tailY[100]; //snake coordinates
  16.  
  17. int nTail;
  18.  
  19. enum eDirecton {STOP = 0, LEFT,RIGHT, UP, DOWN}; // Controls
  20.  
  21. eDirecton dir;
  22.  
  23. void Setup() {
  24. gameover = false;
  25.  
  26. dir = STOP;
  27.  
  28. x = width / 2;
  29.  
  30. y = height / 2;
  31.  
  32. fruitX = rand() % width; //display fruit in a random place
  33.  
  34. fruitY = rand() % height; score = 0;
  35.  
  36. }
  37.  
  38. void Draw() {
  39. system("cls");
  40.  
  41. for(int i = 0; i < width+2; i++)
  42.  
  43. cout << "#";
  44.  
  45. cout << endl ;
  46.  
  47. for (int i = 0; i < height ; i++) {
  48.  
  49. for (int j = 0; j < width; j++) {
  50.  
  51. if (j == 0)
  52.  
  53. cout << "#"; //walls
  54.  
  55. if (i == y && j == x)
  56.  
  57. cout << "*"; // snake tale
  58.  
  59. else if (i == fruitY && j == fruitX )
  60.  
  61. cout << "%"; // change it to change the fruit
  62.  
  63. else {
  64.  
  65. bool print = false;
  66.  
  67. for (int k = 0; k< nTail ; k++) {
  68.  
  69. if (tailX [k] == j && tailY [k] == i) {
  70.  
  71. cout << "*"; print = true;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. if (!print) cout << " ";
  78.  
  79. }
  80.  
  81. if (j == width -1)
  82.  
  83. cout << "#";
  84.  
  85. }
  86.  
  87. cout << endl;
  88.  
  89. }
  90.  
  91. for (int i = 0; i< width+2; i++)
  92.  
  93. cout << "#";
  94.  
  95. cout << endl;
  96.  
  97. cout << "Score:" << score << endl ;
  98.  
  99. }
  100.  
  101. void Input ()
  102. {
  103.  
  104. if (_kbhit ()) {
  105.  
  106. switch (_getch ()) {
  107.  
  108. case 'a':
  109.  
  110. dir = LEFT;
  111.  
  112. break;
  113.  
  114. case 'd':
  115.  
  116. dir = RIGHT;
  117.  
  118. break;
  119.  
  120. case 'w':
  121.  
  122. dir = UP;
  123.  
  124. break;
  125.  
  126. case 's':
  127.  
  128. dir = DOWN ;
  129.  
  130. break;
  131.  
  132. case 'x':
  133.  
  134. gameover = true;
  135.  
  136. break;
  137.  
  138. }
  139.  
  140. }
  141.  
  142. }
  143.  
  144. void algorithm()
  145. {
  146.  
  147. int prevX = tailX [0];
  148.  
  149. int prevY = tailY [0];
  150.  
  151. int prev2X, prev2Y;
  152.  
  153. tailX[0] = x;
  154.  
  155. tailY[0] = y;
  156.  
  157. for(int i = 1;i < nTail ; i++) {
  158.  
  159. prev2X = tailX[i];
  160.  
  161. prev2Y = tailY[i];
  162.  
  163. tailX[i] = prevX;
  164.  
  165. tailY[i] = prevY;
  166.  
  167. prevX = prev2X;
  168.  
  169. prevY = prev2Y ;
  170.  
  171. }
  172.  
  173. switch (dir) {
  174.  
  175. case LEFT:
  176.  
  177. x--;
  178.  
  179. break;
  180.  
  181. case RIGHT:
  182.  
  183. x++;
  184.  
  185. break;
  186.  
  187. case UP:
  188.  
  189. y--;
  190.  
  191. break;
  192.  
  193. case DOWN:
  194.  
  195. y++;
  196.  
  197. break;
  198.  
  199. default:
  200.  
  201. break;
  202.  
  203. }
  204.  
  205. if (x >= width) x =0;else if (x <0) x = width -1;
  206.  
  207. if (y >= height) y = 0; else if (y < 0) y = height - 1;
  208.  
  209. for (int i =0; i< nTail ;i++)
  210.  
  211. if (tailX[i] == x && tailY[i] == y)
  212. gameover = true;
  213.  
  214. if (x == fruitX && y == fruitY) {
  215.  
  216. score +=10;
  217.  
  218. fruitX = rand() % width;
  219.  
  220. fruitY = rand() % height;
  221.  
  222. nTail ++;
  223.  
  224. }
  225.  
  226. }
  227.  
  228. int main()
  229. {
  230.  
  231. Setup();
  232.  
  233. while (!gameover) {
  234.  
  235. Draw ();
  236.  
  237. Input ();
  238.  
  239. algorithm ();
  240.  
  241. }
  242.  
  243. return 0;
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement