Game.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #include "Const.h"
  5. #include "Field.h"
  6. #include "Food.h"
  7. #include "Snake.h"
  8. #include "Game.h"
  9. #include "winconsole.h"
  10. //Óñòàíàâëèâàþòñÿ íà÷àëüíûå çíà÷åíèÿ ïàðàìåòðîâ èãðû.
  11. void init_game(char (* field)[columns], int * snake_x, int* snake_y, DIRECTION& direction, int& snake_size, int& timeout, int& game_on, int& score)//Óñòàíàâëèâàþòñÿ íà÷àëüíûå çíà÷åíèÿ ïàðàìåòðîâ èãðû.
  12. {
  13. srand(time(0));//óñòàíîâêà íà÷àëà ïîñëåäîâàòåëüíîñòè, ãåíåðèðóåìîé ôóíê­öèåé rand()
  14. init_snake(snake_x, snake_y, snake_size);//Èíèöèàëèçàöèÿ ìàññèâà èíäåêñîâ ïîëîæåíèÿ çìåéêè (snake_size ñ êîíñîëè)
  15. system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
  16. start_snake(snake_x, snake_y, snake_size);//Çàäàíèå ðàíäîìíîãî íà÷àëüíîãî ïîëîæåíèÿ ãîëîâû çìåéêè
  17. init_field(field);//Èíèöèàëèçàöèÿ ïîëÿ (ñèìâîëû çàïîëíåíèÿ ïîëÿ è ãðàíèö)
  18. set_snake(field, snake_x, snake_y, snake_size);//Çìåéêà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå
  19. print_field(field, score);//ïå÷àòü ïîëÿ
  20. std::cout << "Press any key" << std::endl;//5) Âûâîä ïðèãëàøåíèÿ ê èãðå.
  21. }
  22. //Çìåéêà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå
  23. void set_snake(char(* field)[columns], int* snake_x, int* snake_y, int& snake_size)
  24. {
  25. field[snake_y[0]][snake_x[0]] = head_symbol;//óñòàíîâêà ãîëîâû
  26. // óñòàíîâêà õâîñòà, åñëè åñòü
  27. for (int i = 1; i < snake_size; i++)
  28. {
  29. field[snake_y[i]][snake_x[i]] = tail_symbol;
  30. }
  31. }
  32. //Î÷èùàåòñÿ ïîçèöèÿ çìåéêè â ìàññèâå
  33. void clear_snake(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size)
  34. {
  35. //÷èñòèì "õâîñò"
  36. field[snake_y[snake_size-1]][snake_x[snake_size-1]] = field_symbol;
  37. }
  38. // Åäà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå.
  39. void set_food(char(*field)[columns], int& snake_size, bool& food_flag, int& food_x, int& food_y)
  40. {
  41. if (!food_flag && snake_size < L)//åñëè íåò åäû è çìåéêà íå äîñòèãëà ìàêñèìàëüíîãî ðàçìåðà
  42. {
  43. do
  44. {
  45. generate_food(food_x, food_y);
  46. } while (field[food_y][food_x] != field_symbol);
  47. field[food_y][food_x] = food_symbol;
  48. food_flag = true;
  49. }
  50. }
  51. //Âûïîëíÿåòñÿ ïðîâåðêà òîãî, ÷òî çìåéêà ñúåëà åäó.
  52. void check_eating(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size, bool& food_flag, int& score)
  53. {
  54. if (field[snake_y[0]][snake_x[0]] == food_symbol)//âïåðåäè åäà
  55. {
  56. food_flag = false;//åäû íåò
  57. snake_size++; //óâåëè÷åíèå ðàçìåðà çìåéêè
  58. }
  59. if (!food_flag) { grow_snake(snake_x, snake_y, snake_size); ++score; };
  60. }
  61. //Âûïîëíÿåòñÿ ïðîâåðêà òîãî, ÷òî çìåéêà âñòðåòèëàñü ñ õâîñòîì
  62. void check_snake(char(*field)[columns], int* snake_x, int* snake_y, int& game_on)
  63. {
  64. if (field[snake_y[0]][snake_x[0]] == tail_symbol) //ãîëîâà âñòðåòèëàñü ñ õâîñòîì
  65. {
  66. game_on = 0;
  67. }
  68. }
  69. //Ïðîâåðêà çàâåðøåíèÿ èãðû.
  70. int check_game(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size, bool& food_flag, int& game_on)
  71. {
  72. int tmp_game_on = game_on;
  73. if (!food_flag && snake_size == L)//åñëè íåò åäû è çìåéêà ìàêñèìàëüíîãî ðàçìåðà
  74. {
  75. tmp_game_on = 0;
  76. }
  77. check_snake(field, snake_x, snake_y, tmp_game_on); //ïðîâåðêà òîãî, ÷òî çìåéêà âñòðåòèëàñü ñ õâîñòîì
  78. return tmp_game_on;
  79. }
  80. //Îáðàáîòêà ïîëüçîâàòåëüñêîãî ââîäà
  81. void handle_cmd(DIRECTION& direction, int& game_on)
  82. {
  83. char key;// äëÿ ââîäà êîäà ñèìâîëà ñ êëàâèàòóðû
  84. char keep_direct;// äëÿ ñîõðàíåíèÿ ñèìâîëà íàïðàâëåíèÿ äâèæåíèÿ.
  85. DIRECTION tmp_direction;
  86. key = _getch();
  87. if (key == 0 || key == -32)
  88. {
  89. keep_direct = _getch();
  90. switch (keep_direct)
  91. {
  92. case up: {tmp_direction = turn_up; } break;
  93. case down: {tmp_direction = turn_down; } break;
  94. case left: {tmp_direction = turn_left; } break;
  95. case right: {tmp_direction = turn_right; } break;
  96. default: {tmp_direction = err; }
  97. }
  98. }
  99. else
  100. {
  101. switch (key = toupper(key))
  102. {
  103. case 'D': {tmp_direction = turn_right; } break;
  104. case 'A': {tmp_direction = turn_left; } break;
  105. case 'W': {tmp_direction = turn_up; } break;
  106. case 'S': {tmp_direction = turn_down; } break;
  107. case '0': {game_on = 0; } return;
  108. case esc: {game_on = 0; } return;
  109. break;
  110. default: {tmp_direction = err; }
  111. }
  112. }
  113. if (tmp_direction != err)
  114. {
  115. switch (direction)
  116. {
  117. case turn_up:
  118. {
  119. if (tmp_direction == turn_down) { direction = turn_up; }
  120. else { direction = tmp_direction; }
  121. } break;
  122. case turn_down:
  123. {
  124. if (tmp_direction == turn_up) { direction = turn_down; }
  125. else { direction = tmp_direction; }
  126. } break;
  127. case turn_left:
  128. {
  129. if (tmp_direction == turn_right) { direction = turn_left; }
  130. else { direction = tmp_direction; }
  131. } break;
  132. case turn_right:
  133. {
  134. if (tmp_direction == turn_left) { direction = turn_right; }
  135. else { direction = tmp_direction; }
  136. } break;
  137. }
  138. }
  139. }
  140. // âûâîä èíôîðìàöèè ïðè îêîí÷àíèå èãðû
  141. void game_over(char(*field)[columns], int* snake_x, int* snake_y, int& score)
  142. {
  143. setCursorPosition(0, 0);
  144. field[snake_y[0]][snake_x[0]] = head_symbol;
  145. print_field(field, score);
  146. std::cout << "The end!" << std::endl;
  147. }