Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. struct position {
  7. int x, y;
  8. };
  9.  
  10. class field_cls {
  11. static const int height;
  12. static const int width;
  13. char** field;
  14. field_cls(const field_cls&);
  15. field_cls operator=(const field_cls&);
  16. public:
  17. field_cls() {
  18. field = new char* [field_cls::height];
  19. for (int c = 0; c < field_cls::height; ++c) {
  20. field[c] = new char[field_cls::width];
  21. }
  22. }
  23. ~field_cls() {
  24. for (int c = 0; c < field_cls::height; ++c) {
  25. delete[] field[c];
  26. }
  27. delete[] field;
  28. }
  29.  
  30. void print() {
  31. for (int c = 0; c < height; ++c) {
  32. for (int r = 0; r < width; ++r) {
  33. cout << field[c][r];
  34. }
  35. cout << endl;
  36. }
  37. }
  38.  
  39. void clear() {
  40. for (int c = 0; c < height; ++c) {
  41. for (int r = 0; r < width; ++r) {
  42. field[c][r] = ' ';
  43. }
  44. }
  45. }
  46.  
  47. int get_width() const { return width; }
  48. int get_height() const { return height; }
  49.  
  50.  
  51. void draw(int y, int x, char what) {
  52. //y = (y < 0) ? 0 : (y >= height ? height - 1 : y);
  53. //x = (x < 0) ? 0 : (x >= width ? width - 1 : x);
  54.  
  55. field[y][x] = what;
  56. }
  57.  
  58. } field;
  59.  
  60.  
  61. class food_cls {
  62. position pos;
  63. char symbol;
  64. public:
  65. food_cls() : symbol('-'), pos() {
  66. pos.x = pos.y = -1;
  67. }
  68.  
  69. void set_pos(int x, int y) {
  70. pos.x = x;
  71. pos.y = y;
  72. }
  73.  
  74. void reposition(const field_cls& field) {
  75. pos.x = rand() % field.get_width();
  76. pos.y = rand() % field.get_height();
  77. }
  78.  
  79. int get_x() const { return pos.x; }
  80. int get_y() const { return pos.y; }
  81. char get_symbol() const { return symbol; }
  82. } food;
  83.  
  84. class snake_cls {
  85. enum { UP, DOWN, LEFT, RIGHT } dir;
  86. char symbol, head_symbol;
  87. position pos[100];
  88. position& head;
  89. int speed;
  90. int size;
  91. bool can_turn;
  92. public:
  93. snake_cls(int x, int y) :
  94. symbol('\u0300'), head_symbol('O'), pos(),
  95. speed(1), size(1), dir(RIGHT),
  96. head(pos[0]), can_turn(true)
  97. {
  98. pos[0].x = x;
  99. pos[0].y = y;
  100. }
  101.  
  102. bool check_food(const food_cls& food) {
  103. if (food.get_x() == head.x && food.get_y() == head.y) {
  104. size += 1;
  105. return true;
  106. }
  107. return false;
  108. }
  109.  
  110. void get_input(const field_cls& field) {
  111. if (GetAsyncKeyState(VK_UP) && dir != DOWN) {
  112. dir = UP;
  113. }
  114. if (GetAsyncKeyState(VK_DOWN) && dir != UP) {
  115. dir = DOWN;
  116. }
  117. if (GetAsyncKeyState(VK_LEFT) && dir != RIGHT) {
  118. dir = LEFT;
  119. }
  120. if (GetAsyncKeyState(VK_RIGHT) && dir != LEFT) {
  121. dir = RIGHT;
  122. }
  123. }
  124.  
  125. void move(const field_cls& field) {
  126. position next = { 0, 0 };
  127. switch (dir) {
  128. case UP:
  129. next.y = -speed;
  130. break;
  131. case DOWN:
  132. next.y = speed;
  133. break;
  134. case LEFT:
  135. next.x = -speed;
  136. break;
  137. case RIGHT:
  138. next.x = speed;
  139. }
  140. for (int c = size - 1; c > 0; --c) {
  141. pos[c] = pos[c - 1];
  142. }
  143. head.x += next.x;
  144. head.y += next.y;
  145.  
  146. if (head.x < 0 || head.y < 0 || head.x >= field.get_width() || head.y >= field.get_height()) {
  147. throw "YOU DIED.";
  148. }
  149. }
  150.  
  151. void draw(field_cls& field) {
  152. for (int c = 0; c < size; ++c) {
  153. if (c == 0) {
  154. field.draw(pos[c].y, pos[c].x, head_symbol);
  155. }
  156. else {
  157. field.draw(pos[c].y, pos[c].x, symbol);
  158. }
  159. }
  160. }
  161.  
  162. int get_x() const { return head.x; }
  163. int get_y() const { return head.y; }
  164. char get_symbol() const { return symbol; }
  165. } snake(1, 1);
  166.  
  167.  
  168. const int field_cls::height = 24;
  169. const int field_cls::width = 79;
  170.  
  171.  
  172. int main() {
  173. field.clear();
  174.  
  175. food.set_pos(5, 5);
  176.  
  177. while (1) {
  178. field.clear();
  179.  
  180. snake.get_input(field);
  181. try {
  182. snake.move(field);
  183. }
  184. catch (const char* er) {
  185. field.clear();
  186. cerr << er << endl;
  187. system("pause");
  188. return -1;
  189. }
  190. snake.draw(field);
  191. field.draw(food.get_y(), food.get_x(), food.get_symbol());
  192.  
  193.  
  194. if (snake.check_food(food)) {
  195. food.reposition(field);
  196. }
  197.  
  198. field.print();
  199.  
  200. Sleep(1000 / 30);
  201. system("cls");
  202. }
  203.  
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement