123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include <iostream>
- #include <windows.h>
- #include <conio.h>
- #include "Const.h"
- #include "Game.h"
- #include "Snake.h"
- #include "Field.h"
- #include "Food.h"
- int main()
- {
- snake_size = 5;// >0 Âêëþ÷àåò ãîëîâó + ðàçìåð õâîñòà (åñëè 1 - òî òîëüêî ãîëîâà)
- //timeout = 200; //> 100 (ms), 1000 - áóäåò çàäåðæèâàòü âûïîëíåíèå ïðîãðàììû íà 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;
- }
- //Çàäàíèå ðàíäîìíîãî íà÷àëüíîãî ïîëîæåíèÿ ãîëîâû çìåéêè
- srand(time(0));
- int step = rand() % L + 1;
- //step = 13;
- //snake_x[0] = 13;
- for (int i = 0; i < step - 1; i++)
- {
- 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;
- }
- for (int i = 0; i < columns; i++)//çàïîëíåíèå ïîëÿ ïóñòûìè ñèìâîëàìè
- {
- field[i] = field_symbol;
- }
- field[0] = field[columns - 1] = '*';//óñòàíîâêà ãðàíèö
- field[snake_x[0]] = head_symbol;//óñòààíîâêà ãîëîâû
- // óñòàíîâêà õâîñòà, åñëè åñòü
- for (int i = 1; i < snake_size; i++)
- {
- field[snake_x[L - i]] = tail_symbol;
- }
- stop
- for (int i = 0; i < columns; i++)
- {
- std::cout << border_symbol;
- }
- std::cout << std::endl;
- for (int i = 0; i < columns; i++)
- {
- std::cout << field[i];
- }
- std::cout << std::endl;
- for (int i = 0; i < columns; i++)
- {
- std::cout << border_symbol;
- }
- std::cout << std::endl;
- std::cout << "Press control key" << std::endl;//5) Âûâîä ïðèãëàøåíèÿ ê èãðå.
- //char key = _getch();
- //ïîÿâëåíèå åäû íà ïîëå
- do
- {
- food_x = rand() % (columns - 2) + 1;
- } while (field[food_x] != field_symbol);
- field[food_x] = food_symbol;
- food_flag = true;
- while (true)
- {
- system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
- //ñìåùàåì ïîëîæåíèå ãîëîâû çìåéêè
- 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;
- if (field[snake_x[0]] == field_symbol)//âïåðåäè ïóñòî
- {
- //óñòàíàâëèâàåì ïîëîæåíèå ãîëîâû
- field[snake_x[0]] = head_symbol;
- //÷èñòèì "õâîñò"
- field[snake_x[L - snake_size]] = field_symbol;
- if (snake_size > 1)
- {
- //ñèìâîë õâîñòà âìåñòî ãîëîâû
- field[snake_x[L - 1]] = tail_symbol;
- }
- }
- if (field[snake_x[0]] == food_symbol)//âïåðåäè åäà
- {
- //óñòàíàâëèâàåì ïîëîæåíèå ãîëîâû
- field[snake_x[0]] = head_symbol;
- if (snake_size > 1)
- {
- //ñèìâîë õâîñòà âìåñòî ãîëîâû
- field[snake_x[L - 1]] = tail_symbol;
- }
- food_flag = false;//åäû íåò
- snake_size++; //óâåëè÷åíèå ðàçìåðà çìåéêè
- }
- if (!food_flag && snake_size < L)//åñëè íåò åäû è çìåéêà äîñòèãëà ìàêñèìàëüíîãî ðàçìåðà
- {
- do
- {
- food_x = rand() % (columns - 2) + 1;
- } while (field[food_x] != field_symbol);
- field[food_x] = food_symbol;//óñòàíîâèòü åäó
- food_flag = true;//åäà åñòü
- }
- if (!food_flag && snake_size == L)//åñëè íåò åäû è çìåéêà ìàêñèìàëüíîãî ðàçìåðà
- {
- std::cout << "The End" << std::endl;//âûâåñòè ñîîáùåíèå îá îêîí÷àíèè èãðû
- break;//âûéòè èç öèêëà
- }
- //ïå÷àòü
- for (int i = 0; i < columns; i++)
- {
- std::cout << border_symbol;
- }
- std::cout << std::endl;
- for (int i = 0; i < columns; i++)
- {
- std::cout << field[i];
- }
- std::cout << std::endl;
- for (int i = 0; i < columns; i++)
- {
- std::cout << border_symbol;
- }
- std::cout << std::endl;
- Sleep(timeout);
- }
- return 0;//êîä çàâåðøåíèÿ ïðèëîæåíèÿ
- }
|