Tavxela

Untitled

Dec 29th, 2019
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <windows.h>
  4. #include <ctime>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. bool gameOver;
  10. const int width = 20;
  11. const int height = 20;
  12. int x, y, fruitX, fruitY, score;
  13. int tailX[100], tailY[100];
  14. int nTail;
  15.  
  16. default_random_engine generator(time(NULL));
  17. uniform_int_distribution<int> rangeW(0, (width-1)), rangeH(0, (height-1));
  18.  
  19. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
  20. eDirection dir;
  21.  
  22. void setup() {
  23. gameOver = false;
  24. dir = STOP;
  25.  
  26. x = width / 2;
  27. y = height / 2;
  28.  
  29. fruitX = rangeW(generator);
  30. fruitY = rangeH(generator);
  31.  
  32. score = 0;
  33. }
  34.  
  35. void draw() {
  36. system("cls");
  37.  
  38. cout << "Score: " << score << endl;
  39.  
  40. for (int i = 0; i < width+2; i++) {
  41. cout << '#';
  42. }
  43. cout << endl;
  44.  
  45. for (int i = 0; i < height; i++) {
  46.  
  47. for (int j = 0; j < width; j++) {
  48. if (j == 0) {
  49. cout << '#';
  50. }
  51.  
  52. if (i == y && j == x) {
  53. cout << 'O';
  54. }
  55. else if (i == fruitY && j == fruitX) {
  56. cout << '@';
  57. }
  58. else {
  59. bool print = false;
  60. for (int k = 0; k < nTail; k++) {
  61.  
  62. if (tailX[k] == j && tailY[k] == i) {
  63. cout << '*';
  64. print = true;
  65. }
  66. }
  67. if (!print) {
  68. cout << ' ';
  69. }
  70. }
  71.  
  72. if (j == (width-1)) {
  73. cout << '#';
  74. }
  75. }
  76. cout << endl;
  77.  
  78. }
  79.  
  80. for (int i = 0; i < width+2; i++) {
  81. cout << '#';
  82. }
  83. cout << endl;
  84.  
  85. }
  86.  
  87. void input() {
  88. if (_kbhit()) {
  89. switch (_getch()) {
  90. case 'w':
  91. dir = UP;
  92. break;
  93. case 'a':
  94. dir = LEFT;
  95. break;
  96. case'd':
  97. dir = RIGHT;
  98. break;
  99. case 's':
  100. dir = DOWN;
  101. break;
  102. case 'p':
  103. gameOver = true;
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. void logic() {
  110. int prevX = tailX[0];
  111. int prevY = tailY[0];
  112. int prev2X, prev2Y;
  113. tailX[0] = x;
  114. tailY[0] = y;
  115.  
  116. for (int i = 1; i < nTail; i++) {
  117. prev2X = tailX[i];
  118. prev2Y = tailY[i];
  119. tailX[i] = prevX;
  120. tailY[i] = prevY;
  121. prevX = prev2X;
  122. prevY = prev2Y;
  123.  
  124. }
  125.  
  126. switch (dir)
  127. {
  128. case LEFT:
  129. x--;
  130. break;
  131. case RIGHT:
  132. x++;
  133. break;
  134. case UP:
  135. y--;
  136. break;
  137. case DOWN:
  138. y++;
  139. break;
  140. default:
  141. break;
  142. }
  143.  
  144. if (x > width || x < 0 || y > height || y < 0) {
  145. gameOver = true;
  146. }
  147. for (int i = 0; i < nTail; i++) {
  148. if (tailX[i] == x && tailY[i] == y) {
  149. gameOver = true;
  150. }
  151. }
  152.  
  153. if (x == fruitX && y == fruitY) {
  154. score++;
  155. nTail++;
  156.  
  157. fruitX = rangeH(generator);
  158. fruitY = rangeW(generator);
  159. }
  160.  
  161. }
  162.  
  163. int main() {
  164.  
  165. setup();
  166. while (!gameOver) {
  167. draw();
  168. input();
  169. logic();
  170. Sleep(10);
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment