123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #include <iostream>
- #include <windows.h>
- #include <conio.h>
- #include "Const.h"
- #include "Field.h"
- #include "Food.h"
- #include "Snake.h"
- #include "Game.h"
- #include "winconsole.h"
- //Óñòàíàâëèâàþòñÿ íà÷àëüíûå çíà÷åíèÿ ïàðàìåòðîâ èãðû.
- void init_game(char (* field)[columns], int * snake_x, int* snake_y, DIRECTION& direction, int& snake_size, int& timeout, int& game_on, int& score)//Óñòàíàâëèâàþòñÿ íà÷àëüíûå çíà÷åíèÿ ïàðàìåòðîâ èãðû.
- {
- srand(time(0));//óñòàíîâêà íà÷àëà ïîñëåäîâàòåëüíîñòè, ãåíåðèðóåìîé ôóíêöèåé rand()
- init_snake(snake_x, snake_y, snake_size);//Èíèöèàëèçàöèÿ ìàññèâà èíäåêñîâ ïîëîæåíèÿ çìåéêè (snake_size ñ êîíñîëè)
- system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
- start_snake(snake_x, snake_y, snake_size);//Çàäàíèå ðàíäîìíîãî íà÷àëüíîãî ïîëîæåíèÿ ãîëîâû çìåéêè
- init_field(field);//Èíèöèàëèçàöèÿ ïîëÿ (ñèìâîëû çàïîëíåíèÿ ïîëÿ è ãðàíèö)
- set_snake(field, snake_x, snake_y, snake_size);//Çìåéêà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå
- print_field(field, score);//ïå÷àòü ïîëÿ
- std::cout << "Press any key" << std::endl;//5) Âûâîä ïðèãëàøåíèÿ ê èãðå.
- }
- //Çìåéêà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå
- void set_snake(char(* field)[columns], int* snake_x, int* snake_y, int& snake_size)
- {
- field[snake_y[0]][snake_x[0]] = head_symbol;//óñòàíîâêà ãîëîâû
- // óñòàíîâêà õâîñòà, åñëè åñòü
-
- for (int i = 1; i < snake_size; i++)
- {
- field[snake_y[i]][snake_x[i]] = tail_symbol;
- }
- }
- //Î÷èùàåòñÿ ïîçèöèÿ çìåéêè â ìàññèâå
- void clear_snake(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size)
- {
- //÷èñòèì "õâîñò"
- field[snake_y[snake_size-1]][snake_x[snake_size-1]] = field_symbol;
- }
- // Åäà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå.
- void set_food(char(*field)[columns], int& snake_size, bool& food_flag, int& food_x, int& food_y)
- {
- if (!food_flag && snake_size < L)//åñëè íåò åäû è çìåéêà íå äîñòèãëà ìàêñèìàëüíîãî ðàçìåðà
- {
- do
- {
- generate_food(food_x, food_y);
- } while (field[food_y][food_x] != field_symbol);
- field[food_y][food_x] = food_symbol;
- food_flag = true;
- }
- }
- //Âûïîëíÿåòñÿ ïðîâåðêà òîãî, ÷òî çìåéêà ñúåëà åäó.
- void check_eating(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size, bool& food_flag, int& score)
- {
- if (field[snake_y[0]][snake_x[0]] == food_symbol)//âïåðåäè åäà
- {
- food_flag = false;//åäû íåò
- snake_size++; //óâåëè÷åíèå ðàçìåðà çìåéêè
- }
- if (!food_flag) { grow_snake(snake_x, snake_y, snake_size); ++score; };
- }
- //Âûïîëíÿåòñÿ ïðîâåðêà òîãî, ÷òî çìåéêà âñòðåòèëàñü ñ õâîñòîì
- void check_snake(char(*field)[columns], int* snake_x, int* snake_y, int& game_on)
- {
- if (field[snake_y[0]][snake_x[0]] == tail_symbol) //ãîëîâà âñòðåòèëàñü ñ õâîñòîì
- {
- game_on = 0;
- }
- }
- //Ïðîâåðêà çàâåðøåíèÿ èãðû.
- int check_game(char(*field)[columns], int* snake_x, int* snake_y, int& snake_size, bool& food_flag, int& game_on)
- {
- int tmp_game_on = game_on;
- if (!food_flag && snake_size == L)//åñëè íåò åäû è çìåéêà ìàêñèìàëüíîãî ðàçìåðà
- {
- tmp_game_on = 0;
- }
-
- check_snake(field, snake_x, snake_y, tmp_game_on); //ïðîâåðêà òîãî, ÷òî çìåéêà âñòðåòèëàñü ñ õâîñòîì
-
- return tmp_game_on;
- }
- //Îáðàáîòêà ïîëüçîâàòåëüñêîãî ââîäà
- void handle_cmd(DIRECTION& direction, int& game_on)
- {
- char key;// äëÿ ââîäà êîäà ñèìâîëà ñ êëàâèàòóðû
- char keep_direct;// äëÿ ñîõðàíåíèÿ ñèìâîëà íàïðàâëåíèÿ äâèæåíèÿ.
- DIRECTION tmp_direction;
- key = _getch();
-
- if (key == 0 || key == -32)
- {
- keep_direct = _getch();
-
- switch (keep_direct)
- {
- case up: {tmp_direction = turn_up; } break;
- case down: {tmp_direction = turn_down; } break;
- case left: {tmp_direction = turn_left; } break;
- case right: {tmp_direction = turn_right; } break;
-
- default: {tmp_direction = err; }
- }
- }
- else
- {
- switch (key = toupper(key))
- {
- case 'D': {tmp_direction = turn_right; } break;
- case 'A': {tmp_direction = turn_left; } break;
- case 'W': {tmp_direction = turn_up; } break;
- case 'S': {tmp_direction = turn_down; } break;
- case '0': {game_on = 0; } return;
- case esc: {game_on = 0; } return;
- break;
- default: {tmp_direction = err; }
- }
- }
- if (tmp_direction != err)
- {
- switch (direction)
- {
- case turn_up:
- {
- if (tmp_direction == turn_down) { direction = turn_up; }
- else { direction = tmp_direction; }
- } break;
- case turn_down:
- {
- if (tmp_direction == turn_up) { direction = turn_down; }
- else { direction = tmp_direction; }
- } break;
- case turn_left:
- {
- if (tmp_direction == turn_right) { direction = turn_left; }
- else { direction = tmp_direction; }
- } break;
- case turn_right:
- {
- if (tmp_direction == turn_left) { direction = turn_right; }
- else { direction = tmp_direction; }
- } break;
- }
- }
- }
- // âûâîä èíôîðìàöèè ïðè îêîí÷àíèå èãðû
- void game_over(char(*field)[columns], int* snake_x, int* snake_y, int& score)
- {
- setCursorPosition(0, 0);
- field[snake_y[0]][snake_x[0]] = head_symbol;
- print_field(field, score);
- std::cout << "The end!" << std::endl;
- }
|