1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #include <iostream>
- #include <windows.h>
- #include "Const.h"
- #include "Snake.h"
- int snake_size =1; //Òåêóùèé ðàçìåð çìåéêè; >0 Âêëþ÷àåò ãîëîâó + ðàçìåð õâîñòà (åñëè 1 - òî òîëüêî ãîëîâà)
- int snake_x[L]{0};//Ìàññèâ êîîðäèíàò çìåéêè ïî ãîðèçîíòàëè(X)
- void start_snake()//íà÷àëüíîå ïîëîæåíèå ãîëîâû çìåéêè ðàíäîìíîå
- {
- int step = rand() % L + 1;
- //step = 13;
- //snake_x[0] = 13;
- for (int i = 0; i < step - 1; i++)
- {
- move_snake();
- }
- }
- void init_snake()//Óñòàíàâëèâàåòñÿ íà÷àëüíûé ðàçìåð çìåéêè.
- {
- std::cout << "Enter snake_size > 0. Snake_size must be <" << L << std::endl;
- std::cin >> snake_size;
- if (snake_size <= 0 || snake_size > L)//Ïðîâåðêà íà äîïóñòèìûé ðàçìåð çíà÷åíèÿ.
- {
- std::cout << "Err! Wrong size " << std::endl;
- exit(1);
- }
-
- //Çàïîëíåíèå ìàññèâà êîîðäèíàò èíäåêñàìè îò 1 äî L; snake_x[0] = 1
- // snake_x[L-1] - íà÷àëî õâîñòà
- // snake_x[L-snake_size] - ïîëå, ãäå íàõîäèòñÿ ïóñòîé ñèìâîë
-
- for (int i = 0; i < L; i++)
- {
- snake_x[i] = i + 1;
- }
- }
- void move_snake()// Ïåðåäâèãàþòñÿ ýëåìåíòû çìåéêè.
- {//Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî íà 1 ýëåìåíò
- int tmp = snake_x[0];
- for (int j = 0; j < L - 1; j++)
- {
- snake_x[j] = snake_x[j + 1];
- }
- snake_x[L - 1] = tmp;
- }
|