Guest User

Untitled

a guest
Feb 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include "conio.h"
  4. using namespace std;
  5.  
  6. enum CellType{floor_cell = 0, wall = 1, player = 2, finish = 3};
  7.  
  8.  
  9. struct Vector2
  10. {
  11.  
  12. int x;
  13. int y;
  14.  
  15. public:
  16. Vector2(){
  17.  
  18. x = 0;
  19. y = 0;
  20.  
  21. }
  22. Vector2(int new_x, int new_y)
  23. {
  24. x = new_x;
  25. y = new_y;
  26. }
  27. friend
  28. Vector2 operator + (Vector2 v1, Vector2 v2)
  29. {
  30. return Vector2(v1.x + v2.x, v1.y + v2.y);
  31.  
  32. }
  33.  
  34. };
  35.  
  36.  
  37.  
  38.  
  39.  
  40. class Game {
  41. private:
  42.  
  43. char symbols[4];
  44.  
  45. int lab[24][24] = { {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
  46. {1,0,1,0,0,1,0,1,0,0,0,1,1,1,1,0,0,0,0,1,},
  47. {1,0,1,1,0,1,0,1,0,1,0,0,0,0,1,0,1,1,0,1,},
  48. {1,0,0,0,0,0,0,1,0,1,1,1,1,0,1,0,0,1,0,1,},
  49. {1,1,1,1,1,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,},
  50. {1,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,},
  51. {1,0,1,1,0,1,1,1,1,1,0,0,1,0,1,1,1,1,0,1,},
  52. {1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,},
  53. {1,1,1,1,1,1,0,1,1,1,0,0,0,0,1,0,1,1,1,1,},
  54. {1,1,0,0,0,1,0,0,1,1,0,1,1,1,1,0,0,0,0,1,},
  55. {1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,0,1,},
  56. {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,},
  57. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,},
  58. {1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,},
  59. {1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,},
  60. {1,0,1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,1,},
  61. {1,0,1,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,1,1,},
  62. {1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,},
  63. {1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,1,},
  64. {1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,},
  65. {1,0,1,0,1,0,0,0,1,0,1,1,1,1,1,1,0,1,0,1,},
  66. {1,7,1,0,1,1,1,1,1,0,0,0,1,0,1,0,0,1,0,1,},
  67. {1,0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,1,1,0,1,},
  68. {1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,}
  69. };
  70.  
  71. void Drawlab()
  72. {
  73.  
  74. for (int x = 0; x <= 23; x++)
  75. {
  76. for (int y = 0; y <= 23; y++)
  77. {
  78.  
  79. Vector2 pos = Vector2(x, y);
  80. cout << symbols[lab[x][y]];
  81.  
  82. }
  83. cout << endl;
  84. }
  85.  
  86. }
  87.  
  88.  
  89. int ap = 0;
  90.  
  91.  
  92. Vector2 PlayerCoords;
  93.  
  94. bool running = true;
  95.  
  96. void PrintSymbol(Vector2 pos, CellType cell_type)
  97. {
  98. SetCursorPosition(pos);
  99. cout << symbols[static_cast<int>(cell_type)];
  100.  
  101. }
  102. void SetCursorPosition(Vector2 coord)
  103. {
  104. COORD pos = { coord.x, coord.y };
  105. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  106. SetConsoleCursorPosition(output, pos);
  107.  
  108.  
  109. }
  110.  
  111.  
  112. void HideCursor() {
  113. CONSOLE_CURSOR_INFO lpCursor;
  114. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  115.  
  116. GetConsoleCursorInfo(output, &lpCursor);
  117. lpCursor.bVisible = false;
  118. SetConsoleCursorInfo(output, &lpCursor);
  119. }
  120. //void Draw(char c, int x, int y) {
  121. //COORD pos = { x, y };
  122. //HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  123. //SetConsoleCursorPosition(output, pos);
  124.  
  125. //std::cout << c;
  126. //}
  127.  
  128. int CTTI(CellType ct)
  129. {
  130.  
  131. return static_cast<int>(ct);
  132. }
  133.  
  134. bool CheckCell(Vector2 v) {
  135.  
  136.  
  137. bool wallka(lab[v.y][v.x] == CTTI(wall));
  138.  
  139.  
  140. bool finishka(lab[v.y][v.x] == CTTI(finish));
  141. if (finishka == true)
  142. {
  143.  
  144. cout << "You win!!!!!";
  145. return true;
  146. }
  147.  
  148. return !wallka;
  149. }
  150.  
  151.  
  152. bool Finish(Vector2 j)
  153. {
  154.  
  155. return false;
  156.  
  157. }
  158.  
  159. void MovePlayer(Vector2 shift)
  160. {
  161.  
  162. Vector2 new_coords = PlayerCoords + shift;
  163.  
  164. if (!CheckCell(new_coords))
  165. return;
  166.  
  167. PrintSymbol(PlayerCoords, CellType::floor_cell);
  168. PlayerCoords = new_coords;
  169. PrintSymbol(PlayerCoords, CellType::player);
  170.  
  171.  
  172. }
  173. //void PrintMesAbotStart()
  174. //{
  175. //hero_y = 30;
  176. //hero_x = 5;
  177. //cout << "Press W or S to select level";
  178.  
  179. //}
  180.  
  181.  
  182.  
  183. public:
  184.  
  185. Game() {
  186.  
  187. symbols[static_cast<int>(floor_cell)] = ' ';
  188. symbols[static_cast<int>(wall)] = 178;
  189. symbols[static_cast<int>(player)] = '@';
  190. symbols[static_cast<int>(finish)] = '*';
  191.  
  192. }
  193.  
  194. void Run()
  195. {
  196. Drawlab();
  197. const int NotUsed = system("color 7B");
  198. HideCursor();
  199. PrintSymbol(Vector2(0, 0), CellType::player);
  200. while (running)
  201. {
  202.  
  203. char c = _getch();
  204.  
  205. switch (c) {
  206. case 27:
  207. std::cout << "Game over!" << '\n';
  208. running = false;
  209. break;
  210.  
  211. case 119:
  212. Vector2(+3, +2);
  213. //cout<<player;
  214. break;
  215. case 115:
  216. Vector2(+37, +22);
  217. // cout << player;
  218. break;
  219. case 72:
  220. MovePlayer( Vector2(0,-1) );
  221.  
  222.  
  223. break;
  224.  
  225. case 80:
  226. MovePlayer(Vector2(0, +1));
  227. break;
  228.  
  229. case 75:
  230. MovePlayer(Vector2(-1,0));
  231. break;
  232.  
  233. case 77:
  234. MovePlayer(Vector2(+1, 0));
  235. break;
  236. }
  237.  
  238. }
  239. }
  240. };
  241.  
  242.  
  243.  
  244.  
  245.  
  246. int main()
  247. {
  248.  
  249. Game * game = new Game();
  250. game->Run();
  251.  
  252. }
Add Comment
Please, Sign In to add comment