123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #include <iostream>
- #include <windows.h>
- #include "Const.h"
- #include "Snake.h"
- //óñòàíàâëèâàåòñÿ íà÷àëüíîå ïîëîæåíèå çìåéêè
- void start_snake(int* snake_x, int* snake_y, int& snake_size, int& columns, int& rows)
- {
- int step_x = rand() % L + 1; // ðàçðàáîòàòü ñëó÷àéíîå íà÷àëüíîå ïîëîæåíèå çìåéêè
- //step = 13;
- //snake_x[0] = 13;
-
- snake_x[0] = columns / 2;
- snake_y[0] = rows / 2;
- for (int i = 1; i <snake_size; i++)
- {
- snake_x[i] = snake_x[0] - i;
-
- if (snake_x[i] <= 0)
- {
- snake_x[i] = 1;
- }
- snake_y[i] = snake_y[0];
- }
- }
- //Óñòàíàâëèâàåòñÿ íà÷àëüíûé ðàçìåð çìåéêè, ìàññèâ çìåéêè çàïîëíÿåòñÿ çíà÷åíèÿìè
- void init_snake(int* snake_x, int* snake_y, int& snake_size, int& columns, int& rows)
- {
- 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);
- }
-
- start_snake(snake_x, snake_y, snake_size, columns, rows);
-
- }
- // Ïåðåäâèãàþòñÿ ýëåìåíòû çìåéêè. Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî
- void move_snake(int* snake_x, int* snake_y, int& snake_size, DIRECTION& direction, int& columns, int& rows)
- {//Äëÿ äâèæåíèÿ âïðàâî, ñìåùåíèå èíäåêñîâ ïðîèñõîäèò âëåâî íà 1 ýëåìåíò
- switch (direction)
- {
- case turn_left:
- {
- for (int i = snake_size - 1; i > 0; i--)
- {
- snake_x[i] = snake_x[i - 1];
- snake_y[i] = snake_y[i - 1];
- }
- snake_x[0]--;
- if (snake_x[0] == 0)
- {
- snake_x[0] = columns - 2;
- }
- }break;
- case turn_right:
- {
- for (int i = snake_size - 1; i > 0; i--)
- {
- snake_x[i] = snake_x[i - 1];
- snake_y[i] = snake_y[i - 1];
- }
- snake_x[0]++;
- if (snake_x[0] == columns-1)
- {
- snake_x[0] = 1;
- }
- }break;
- case turn_up:
- {
- for (int i = snake_size - 1; i > 0; i--)
- {
- snake_x[i] = snake_x[i - 1];
- snake_y[i] = snake_y[i - 1];
- }
- snake_y[0]--;
- if (snake_y[0] == 0)
- {
- snake_y[0] = rows - 2;
- }
- }break;
- case turn_down:
- {
- for (int i = snake_size - 1; i > 0; i--)
- {
- snake_x[i] = snake_x[i - 1];
- snake_y[i] = snake_y[i - 1];
- }
-
- snake_y[0]++;
- if (snake_y[0] == rows - 1)
- {
- snake_y[0] = 1;
- }
- }break;
-
- default: break;
- }
- }
- void grow_snake(int* snake_x, int* snake_y, int& snake_size)
- {
- int tmp = 1;
-
- for (int i = snake_size - 1; tmp > 0; tmp--)
- {
- snake_x[i] = snake_x[i - 1];
- snake_y[i] = snake_y[i - 1];
- }
-
- }
|