Snake.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <iostream>
  2. #include <windows.h>
  3. #include "Const.h"
  4. #include "Snake.h"
  5. //óñòàíàâëèâàåòñÿ íà÷àëüíîå ïîëîæåíèå çìåéêè
  6. void start_snake(int* snake_x, int* snake_y, int& snake_size, int& columns, int& rows)
  7. {
  8. int step_x = rand() % L + 1; // ðàçðàáîòàòü ñëó÷àéíîå íà÷àëüíîå ïîëîæåíèå çìåéêè
  9. //step = 13;
  10. //snake_x[0] = 13;
  11. snake_x[0] = columns / 2;
  12. snake_y[0] = rows / 2;
  13. for (int i = 1; i <snake_size; i++)
  14. {
  15. snake_x[i] = snake_x[0] - i;
  16. if (snake_x[i] <= 0)
  17. {
  18. snake_x[i] = 1;
  19. }
  20. snake_y[i] = snake_y[0];
  21. }
  22. }
  23. //Óñòàíàâëèâàåòñÿ íà÷àëüíûé ðàçìåð çìåéêè, ìàññèâ çìåéêè çàïîëíÿåòñÿ çíà÷åíèÿìè
  24. void init_snake(int* snake_x, int* snake_y, int& snake_size, int& columns, int& rows)
  25. {
  26. std::cout << "Enter snake_size > 0. Snake_size must be <" << L << std::endl;
  27. std::cin >> snake_size;
  28. if (snake_size <= 0 || snake_size > L)//Ïðîâåðêà íà äîïóñòèìûé ðàçìåð çíà÷åíèÿ.
  29. {
  30. std::cout << "Err! Wrong size " << std::endl;
  31. exit(1);
  32. }
  33. start_snake(snake_x, snake_y, snake_size, columns, rows);
  34. }
  35. // Ïåðåäâèãàþòñÿ ýëåìåíòû çìåéêè. Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî
  36. void move_snake(int* snake_x, int* snake_y, int& snake_size, DIRECTION& direction, int& columns, int& rows)
  37. {//Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî íà 1 ýëåìåíò
  38. switch (direction)
  39. {
  40. case turn_left:
  41. {
  42. for (int i = snake_size - 1; i > 0; i--)
  43. {
  44. snake_x[i] = snake_x[i - 1];
  45. snake_y[i] = snake_y[i - 1];
  46. }
  47. snake_x[0]--;
  48. if (snake_x[0] == 0)
  49. {
  50. snake_x[0] = columns - 2;
  51. }
  52. }break;
  53. case turn_right:
  54. {
  55. for (int i = snake_size - 1; i > 0; i--)
  56. {
  57. snake_x[i] = snake_x[i - 1];
  58. snake_y[i] = snake_y[i - 1];
  59. }
  60. snake_x[0]++;
  61. if (snake_x[0] == columns-1)
  62. {
  63. snake_x[0] = 1;
  64. }
  65. }break;
  66. case turn_up:
  67. {
  68. for (int i = snake_size - 1; i > 0; i--)
  69. {
  70. snake_x[i] = snake_x[i - 1];
  71. snake_y[i] = snake_y[i - 1];
  72. }
  73. snake_y[0]--;
  74. if (snake_y[0] == 0)
  75. {
  76. snake_y[0] = rows - 2;
  77. }
  78. }break;
  79. case turn_down:
  80. {
  81. for (int i = snake_size - 1; i > 0; i--)
  82. {
  83. snake_x[i] = snake_x[i - 1];
  84. snake_y[i] = snake_y[i - 1];
  85. }
  86. snake_y[0]++;
  87. if (snake_y[0] == rows - 1)
  88. {
  89. snake_y[0] = 1;
  90. }
  91. }break;
  92. default: break;
  93. }
  94. }
  95. void grow_snake(int* snake_x, int* snake_y, int& snake_size)
  96. {
  97. int tmp = 1;
  98. for (int i = snake_size - 1; tmp > 0; tmp--)
  99. {
  100. snake_x[i] = snake_x[i - 1];
  101. snake_y[i] = snake_y[i - 1];
  102. }
  103. }