Snake.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <iostream>
  2. #include <windows.h>
  3. #include "Const.h"
  4. #include "Snake.h"
  5. int snake_size =1; //Òåêóùèé ðàçìåð çìåéêè; >0 Âêëþ÷àåò ãîëîâó + ðàçìåð õâîñòà (åñëè 1 - òî òîëüêî ãîëîâà)
  6. int snake_x[L]{0};//Ìàññèâ êîîðäèíàò çìåéêè ïî ãîðèçîíòàëè(X)
  7. void start_snake()//íà÷àëüíîå ïîëîæåíèå ãîëîâû çìåéêè ðàíäîìíîå
  8. {
  9. int step = rand() % L + 1;
  10. //step = 13;
  11. //snake_x[0] = 13;
  12. for (int i = 0; i < step - 1; i++)
  13. {
  14. move_snake();
  15. }
  16. }
  17. void init_snake()//Óñòàíàâëèâàåòñÿ íà÷àëüíûé ðàçìåð çìåéêè.
  18. {
  19. std::cout << "Enter snake_size > 0. Snake_size must be <" << L << std::endl;
  20. std::cin >> snake_size;
  21. if (snake_size <= 0 || snake_size > L)//Ïðîâåðêà íà äîïóñòèìûé ðàçìåð çíà÷åíèÿ.
  22. {
  23. std::cout << "Err! Wrong size " << std::endl;
  24. exit(1);
  25. }
  26. //Çàïîëíåíèå ìàññèâà êîîðäèíàò èíäåêñàìè îò 1 äî L; snake_x[0] = 1
  27. // snake_x[L-1] - íà÷àëî õâîñòà
  28. // snake_x[L-snake_size] - ïîëå, ãäå íàõîäèòñÿ ïóñòîé ñèìâîë
  29. for (int i = 0; i < L; i++)
  30. {
  31. snake_x[i] = i + 1;
  32. }
  33. }
  34. void move_snake()// Ïåðåäâèãàþòñÿ ýëåìåíòû çìåéêè.
  35. {//Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî íà 1 ýëåìåíò
  36. int tmp = snake_x[0];
  37. for (int j = 0; j < L - 1; j++)
  38. {
  39. snake_x[j] = snake_x[j + 1];
  40. }
  41. snake_x[L - 1] = tmp;
  42. }