Snake.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <iostream>
  2. #include "Const.h"
  3. //Óñòàíàâëèâàåòñÿ ïîëüçîâàòåëüñêèé ðàçìåð çìåéêè, ìàññèâ çìåéêè çàïîëíÿåòñÿ ñòàðòîâûìè çíà÷åíèÿìè
  4. void init_snake(Snake& snake)
  5. {
  6. std::cout << "Enter snake_size > 0. Snake_size must be <" << L << std::endl;
  7. std::cin >> snake.snake_size;
  8. if (snake.snake_size <= 0 || snake.snake_size > L)//Ïðîâåðêà íà äîïóñòèìûé ðàçìåð çíà÷åíèÿ.
  9. {
  10. std::cout << "Err! Wrong size " << std::endl;
  11. exit(1);
  12. }
  13. for (int i = 1; i < snake.snake_size; i++)
  14. {
  15. snake.snake_x[i] = snake.snake_x[0] - i;
  16. if (snake.snake_x[i] <= 0)
  17. {
  18. snake.snake_x[i] = 1;
  19. }
  20. snake.snake_y[i] = snake.snake_y[0];
  21. }
  22. }
  23. // Ïåðåäâèãàþòñÿ ýëåìåíòû çìåéêè.
  24. void move_snake(Snake& snake)
  25. {
  26. for (int i = snake.snake_size - 1; i > 0; i--)// ñäâèã õâîñòà
  27. {
  28. snake.snake_x[i] = snake.snake_x[i - 1];
  29. snake.snake_y[i] = snake.snake_y[i - 1];
  30. }
  31. switch (snake.direction)//óñòàíîâêà çíà÷åíèÿ ãîëîâû
  32. {
  33. case turn_left: {snake.snake_x[0]--;}break;
  34. case turn_right: {snake.snake_x[0]++;}break;
  35. case turn_up: {snake.snake_y[0]--;}break;
  36. case turn_down: {snake.snake_y[0]++;}break;
  37. default: break;
  38. }
  39. }