Game.cpp 5.0 KB

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