Main.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #include "Const.h"
  5. #include "Game.h"
  6. #include "Snake.h"
  7. #include "Field.h"
  8. #include "winconsole.h"
  9. int main()
  10. {
  11. char field[rows][columns]{}; //Ìàññèâ èãðîâîãî ïîëÿ
  12. int snake_x_main[L]{ 0 };//Ìàññèâ êîîðäèíàò çìåéêè ïî ãîðèçîíòàëè(X)
  13. int snake_y_main[Y]{ 0 };//Ìàññèâ êîîðäèíàò çìåéêè ïî âåðòèêàëè(Y)
  14. int snake_size = 1; //Òåêóùèé ðàçìåð çìåéêè; >0 Âêëþ÷àåò ãîëîâó + ðàçìåð õâîñòà (åñëè 1 - òî òîëüêî ãîëîâà)
  15. int timeout = 200; //Òàéìàóò çàäåðæêè ìåæäó øàãàìè èãðû; > 100 (ms), 1000 - áóäåò çàäåðæèâàòü âûïîëíåíèå ïðîãðàììû íà 1 ñåêóíäó
  16. int game_on = 1;//Ïðèçíàê ïðîäîëæåíèÿ èãðû
  17. DIRECTION direction = turn_right;//íàïðàâëåíèå äâèæåíèÿ
  18. int score = 0;
  19. init_game(field, snake_x_main, snake_y_main, direction, snake_size, timeout, game_on, score);
  20. _getch(); // îæèäàíèå îòêëèêà èãðîêà
  21. system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
  22. bool food_flag = false;//Ïðèçíàê, ÷òî åäà óñòàíîâëåíà
  23. int food_x;//Êîîðäèíàòà åäû ïî ãîðèçîíòàëè (X)
  24. int food_y;//Êîîðäèíàòà åäû ïî âåðòèêàëè (Y)
  25. #define Auto
  26. //#define Hand
  27. while (game_on)
  28. {
  29. setCursorPosition(0, 0);
  30. //system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
  31. #if defined Auto
  32. if (_kbhit())
  33. {
  34. handle_cmd(direction, game_on);
  35. }
  36. //else { continue; }
  37. #elif defined Hand
  38. handle_cmd(direction, game_on);
  39. #else
  40. std::cout << "Select mode!" << std::endl;
  41. #endif
  42. clear_snake(field, snake_x_main, snake_y_main, snake_size);//÷èñòèì "õâîñò"
  43. set_food(field, snake_size, food_flag, food_x, food_y);//óñòàíàâëèâàåòñÿ åäà
  44. move_snake(snake_x_main, snake_y_main, snake_size, direction);//ñìåùàåì ïîëîæåíèå ãîëîâû çìåéêè
  45. check_eating(field, snake_x_main, snake_y_main, snake_size, food_flag, score);//Âûïîëíÿåòñÿ ïðîâåðêà òîãî, ÷òî çìåéêà ñúåëà åäó.
  46. set_snake(field, snake_x_main, snake_y_main, snake_size);//Çìåéêà óñòàíàâëèâàåòñÿ â èãðîâîì ïîëå
  47. print_field(field, score);//ïå÷àòü
  48. game_on = check_game(field, snake_x_main, snake_y_main, snake_size, food_flag, game_on);//ïðîâåðêà íà êîíåö èãðû
  49. Sleep(timeout);//îæèäàíèå
  50. }
  51. game_over(field, snake_x_main, snake_y_main, score);
  52. return 0;//êîä çàâåðøåíèÿ ïðèëîæåíèÿ
  53. }