Advertisement
Guest User

dino

a guest
Jan 25th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.88 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <vector>
  5. #include <ctime>
  6. #include <unordered_map>
  7. using namespace std;
  8. vector< vector<int> > map(0, vector<int>(0));
  9. int width;
  10. int height;
  11. class Point {
  12.     int _x, _y;
  13.  
  14.  
  15.  
  16. public:
  17.     Point() {
  18.         _x = 0;
  19.         _y = 0;
  20.     }
  21.  
  22.     Point(int x, int y) {
  23.         _x = x;
  24.         _y = y;
  25.     }
  26.  
  27.     int getX() { return _x; }
  28.     int getY() { return _y; }
  29.  
  30. };
  31.  
  32. // Отсюда вы можете брать цвета и здесь же создавать свои
  33. class ConsoleColor {
  34. public:
  35.     static COLORREF red() { return RGB(255, 0, 0); }
  36.     static COLORREF blue() { return RGB(0, 0, 255); }
  37.     static COLORREF gray() { return RGB(200, 200, 200); }
  38.     static COLORREF gray2() { return RGB(61, 58, 58); }
  39.     static COLORREF white() { return RGB(255, 255, 255); }
  40.     static COLORREF green() { return RGB(0, 255, 0); }
  41.     static COLORREF black() { return RGB(0, 0, 0); }
  42.     static COLORREF brown() { return RGB(80, 20, 25); }
  43.     static COLORREF yellow() { return RGB(255, 255, 0); }
  44.     static COLORREF purple() { return RGB(66, 49, 137); }
  45.     static COLORREF redx() { return RGB(166, 15, 15); }
  46. };
  47.  
  48. // Это класс, который нужен для рисования. В нем все готово. Ничего менять не нужно. Нужно только пользоваться
  49. class ConsoleDrawer {
  50.     HANDLE _conHandle;
  51.     HWND _hwnd;
  52.     HDC _hdc;
  53.     PAINTSTRUCT _ps;
  54.     unordered_map<COLORREF, HGDIOBJ> _bm;
  55.     unordered_map<COLORREF, HGDIOBJ> _pm;
  56.     HGDIOBJ _curentBrush;
  57.     HGDIOBJ _curentPen;
  58.  
  59.     int _width;
  60.     int _height;
  61.     bool _isWork = true;
  62.  
  63.     void selectBrush(COLORREF color, bool filled = false) {
  64.         if (!filled) {
  65.             SelectObject(_hdc, CreateSolidBrush(NULL_BRUSH));
  66.             return;
  67.         }
  68.         if (_bm.find(color) == _bm.end())
  69.             _bm.insert({ color, CreateSolidBrush(color) });
  70.  
  71.         if (_curentBrush != _bm[color]) {
  72.             _curentBrush = _bm[color];
  73.             SelectObject(_hdc, _curentBrush);
  74.         }
  75.     }
  76.  
  77.     void selectPen(COLORREF color) {
  78.         if (_pm.find(color) == _pm.end())
  79.             _pm.insert({ color, CreatePen(PS_SOLID, 1, color) });
  80.         if (_curentPen != _pm[color]) {
  81.             _curentPen = _pm[color];
  82.             SelectObject(_hdc, _curentPen);
  83.         }
  84.     }
  85.  
  86. public:
  87.     ConsoleDrawer() {
  88.         setlocale(LC_ALL, "");
  89.         _conHandle = GetStdHandle(STD_INPUT_HANDLE);
  90.         int t = sizeof(HGDIOBJ);
  91.         _hwnd = GetConsoleWindow();
  92.         RECT rc;
  93.         GetClientRect(_hwnd, &rc);
  94.         _width = rc.right;
  95.         _height = rc.bottom;
  96.         _hdc = GetDC(_hwnd);
  97.     }
  98.  
  99.     void clearScreen() {
  100.         system("cls");
  101.     }
  102.  
  103.     int getWidth() { return _width; }
  104.     int getHeight() { return _height; }
  105.  
  106.     HANDLE get_conHandle() {
  107.         return _conHandle;
  108.     }
  109.  
  110.     bool isWork() {
  111.  
  112.         return _isWork;
  113.     }
  114.  
  115.     void stop() {
  116.         _isWork = false;
  117.     }
  118.  
  119.     void drawBackground(COLORREF color = ConsoleColor::gray()) {
  120.         RECT rc;
  121.         GetClientRect(_hwnd, &rc);
  122.         drawRect(Point(), Point(_width, _height), color, true);
  123.     }
  124.  
  125.     void drawPoint(Point p, COLORREF color) {
  126.         SetPixel(_hdc, p.getX(), p.getY(), color);
  127.     }
  128.  
  129.     void drawLine(Point p1, Point p2, COLORREF color) {
  130.         selectPen(color);
  131.         //SelectObject(_hdc, CreatePen(PS_SOLID, 1, color));
  132.         //SetDCPenColor(_hdc, color);
  133.         MoveToEx(_hdc, p1.getX(), p1.getY(), NULL);
  134.         LineTo(_hdc, p2.getX(), p2.getY());
  135.     }
  136.  
  137.     // Рисует круг или окружность по центру и радиусу
  138.     // filled - нужно ли закрашивать внутренность
  139.     void drawCircle(Point c, int radius, COLORREF color, bool filled = false) {
  140.         selectPen(color);
  141.         selectBrush(color, filled);
  142.         //SetDCBrushColor(_hdc, color);
  143.         Ellipse(_hdc, c.getX() - radius, c.getY() - radius, c.getX() + radius, c.getY() + radius);
  144.  
  145.     }
  146.  
  147.     // Рисует прямоугольник по двум точкам
  148.     // p1 - нижний левый угол
  149.     // p2 - верхний правый угол
  150.     void drawRect(Point p1, Point p2, COLORREF color, bool filled = false) {
  151.         selectPen(color);
  152.         selectBrush(color, filled);
  153.         Rectangle(_hdc, p1.getX(), p1.getY(), p2.getX(), p2.getY());
  154.     }
  155.  
  156.     // Рисует треугольник по трем точкам
  157.     void drawTriangle(Point p1, Point p2, Point p3, COLORREF color, bool filled = false) {
  158.         selectPen(color);
  159.         selectBrush(color, filled);
  160.         POINT apt[3];
  161.         apt[0].x = p1.getX(); apt[0].y = p1.getY();
  162.         apt[1].x = p2.getX(); apt[1].y = p2.getY();
  163.         apt[2].x = p3.getX(); apt[2].y = p3.getY();
  164.         Polygon(_hdc, apt, 3);
  165.     }
  166. };
  167.  
  168. // Это самый базовый класс, от которого наследуются все объекты, которые появляются на поле
  169. class ParkObject {
  170. protected:
  171.     int _cellX, _cellY, _cellSize, _score;
  172.  
  173. public:
  174.     ParkObject(int cellX, int cellY, int cellSize) {
  175.         _cellX = cellX;
  176.         _cellY = cellY;
  177.         _cellSize = cellSize;
  178.  
  179.     }
  180.  
  181.     bool isInside(int cellX, int cellY) {
  182.         return (_cellX == cellX) && (_cellY == cellY);
  183.     }
  184.  
  185.     int getCellX() {
  186.         return _cellX;
  187.     }
  188.  
  189.     int getCellY() {
  190.         return _cellY;
  191.     }
  192.  
  193.     int getX() {
  194.         return _cellX * _cellSize;
  195.     }
  196.  
  197.     int getY() {
  198.         return _cellY * _cellSize;
  199.     }
  200.  
  201. };
  202.  
  203. class Obstacle : public ParkObject {
  204.  
  205. public:
  206.     Obstacle(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) { }
  207. };
  208.  
  209. class Lake :public Obstacle {
  210. public:
  211.     Lake(int cellX, int cellY, int cellSize) : Obstacle(cellX, cellY, cellSize) {}
  212.     void draw(ConsoleDrawer& cd) {
  213.         //Переходим от ячеек к координатам пикселей
  214.         int x = getX();
  215.         int y = getY();
  216.  
  217.         double d = _cellSize / 100.0;
  218.  
  219.         cd.drawRect(Point(x, y), Point(x + _cellSize, y + _cellSize), ConsoleColor::blue(), true);
  220.     }
  221.  
  222.  
  223. };
  224.  
  225. class block :public Obstacle {
  226. public:
  227.     block(int cellX, int cellY, int cellSize) : Obstacle(cellX, cellY, cellSize) {}
  228.     void draw(ConsoleDrawer& cd) {
  229.         //Переходим от ячеек к координатам пикселей
  230.         int x = getX();
  231.         int y = getY();
  232.  
  233.         double d = _cellSize / 100.0;
  234.  
  235.         cd.drawRect(Point(x, y), Point(x + _cellSize, y + _cellSize), ConsoleColor::gray2(), true);
  236.     }
  237.  
  238.  
  239. };
  240.  
  241. class SnowMan : public Obstacle {
  242.  
  243. public:
  244.     SnowMan(int cellX, int cellY, int cellSize) : Obstacle(cellX, cellY, cellSize) { }
  245.  
  246.     void draw(ConsoleDrawer& cd) {
  247.         //Переходим от ячеек к координатам пикселей
  248.         int x = getX();
  249.         int y = getY();
  250.  
  251.         double d = _cellSize / 100.0;
  252.         cd.drawCircle(Point(x + _cellSize / 2, y + _cellSize - 20 * d), 20 * d, ConsoleColor::white(), true);
  253.         cd.drawCircle(Point(x + _cellSize / 2, y + _cellSize - 55 * d), 15 * d, ConsoleColor::white(), true);
  254.         cd.drawCircle(Point(x + _cellSize / 2, y + _cellSize - 80 * d), 10 * d, ConsoleColor::white(), true);
  255.         cd.drawRect(Point(x + _cellSize / 2 - 5 * d, y + 10 * d), Point(x + _cellSize / 2 + 5 * d, y + d), ConsoleColor::brown(), true);
  256.     }
  257. };
  258.  
  259. class Tree : public ParkObject {
  260.  
  261. public:
  262.     Tree(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {}
  263.  
  264.     void draw(ConsoleDrawer& cd) {
  265.         //Переходим от ячеек к координатам пикселей
  266.         int x = getX();
  267.         int y = getY();
  268.  
  269.         int d = _cellSize / 10;
  270.         cd.drawRect(Point(x + 4 * d, y + _cellSize), Point(x + 6 * d, y + _cellSize - 2 * d), ConsoleColor::brown(), true);
  271.         cd.drawTriangle(Point(x + 2 * d, y + _cellSize - 2 * d), Point(x + _cellSize / 2, y + d), Point(x + _cellSize - 2 * d, y + _cellSize - 2 * d), ConsoleColor::green(), true);
  272.     }
  273. };
  274.  
  275. class HappyDino : public ParkObject {
  276.  
  277. public:
  278.     HappyDino(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {}
  279.     COLORREF color = ConsoleColor::green();
  280.  
  281.     void draw(ConsoleDrawer& cd) {
  282.  
  283.         //Переходим от ячеек к координатам пикселей
  284.         int x = getX();
  285.         int y = getY();
  286.  
  287.         double d = _cellSize / 125.0;
  288.         cd.drawRect(Point(x + d, y + 63 * d), Point(x + 24 * d, y + 53 * d), color, true);
  289.         cd.drawRect(Point(x + 75 * d, y + 63 * d), Point(x + 98 * d, y + 53 * d), color, true);
  290.         cd.drawRect(Point(x + 27 * d, y + 93 * d), Point(x + 72 * d, y + 35 * d), color, true);//body
  291.         cd.drawRect(Point(x + 27 * d, y + 124 * d), Point(x + 42 * d, y + 94 * d), color, true);
  292.         cd.drawRect(Point(x + 57 * d, y + 124 * d), Point(x + 72 * d, y + 94 * d), color, true);
  293.         cd.drawRect(Point(x + 27 * d, y + 32 * d), Point(x + 88 * d, y + 2 * d), color, true);//head
  294.         cd.drawCircle(Point(x + 42 * d, y + 12 * d), 8 * d, ConsoleColor::gray(), true);
  295.         cd.drawTriangle(Point(x + 27 * d, y), Point(x + 49 * d, y - 45 * d), Point(x + 72 * d, y), ConsoleColor::red(), true);
  296.         cd.drawCircle(Point(x + 49 * d, y - 50 * d), 10 * d, ConsoleColor::white(), true);
  297.  
  298.     }
  299.  
  300.     void step(int direction) {
  301.         if (direction == 0)
  302.             _cellX -= 1;
  303.         if (direction == 1)
  304.             _cellY -= 1;
  305.         if (direction == 2)
  306.             _cellX += 1;
  307.         if (direction == 3)
  308.             _cellY += 1;
  309.     }
  310. };
  311.  
  312. //HappyDino2
  313.  
  314. /*class HappyDino2 : public ParkObject {
  315.  
  316. public:
  317. HappyDino2(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {}
  318.  
  319. void draw(ConsoleDrawer& cd) {
  320.  
  321. //Переходим от ячеек к координатам пикселей
  322. int x = getX();
  323. int y = getY();
  324. COLORREF color = ConsoleColor::green();
  325.  
  326. double d = _cellSize / 125.0;
  327. cd.drawRect(Point(x + d, y + 63 * d), Point(x + 24 * d, y + 53 * d), ConsoleColor::purple(), true);
  328. cd.drawRect(Point(x + 75 * d, y + 63 * d), Point(x + 98 * d, y + 53 * d), ConsoleColor::purple(), true);
  329. cd.drawRect(Point(x + 27 * d, y + 93 * d), Point(x + 72 * d, y + 35 * d), ConsoleColor::purple(), true);//body
  330. cd.drawRect(Point(x + 27 * d, y + 124 * d), Point(x + 42 * d, y + 94 * d), ConsoleColor::purple(), true);
  331. cd.drawRect(Point(x + 57 * d, y + 124 * d), Point(x + 72 * d, y + 94 * d), ConsoleColor::purple(), true);
  332. cd.drawRect(Point(x + 27 * d, y + 32 * d), Point(x + 88 * d, y + 2 * d), ConsoleColor::purple(), true);//head
  333. cd.drawCircle(Point(x + 42 * d, y + 12 * d), 8 * d, ConsoleColor::gray(), true);
  334. cd.drawTriangle(Point(x + 27 * d, y), Point(x + 49 * d, y - 45 * d), Point(x + 72 * d, y), ConsoleColor::red(), true);
  335. cd.drawCircle(Point(x + 49 * d, y - 50 * d), 10 * d, ConsoleColor::white(), true);
  336.  
  337. }
  338.  
  339. void step(int direction) {
  340. if (direction == 0)
  341. _cellX -= 1;
  342. if (direction == 1)
  343. _cellY -= 1;
  344. if (direction == 2)
  345. _cellX += 1;
  346. if (direction == 3)
  347. _cellY += 1;
  348. }
  349. };
  350. */
  351.  
  352. //AngryDino
  353.  
  354. class AngryDino : public ParkObject {
  355.  
  356. public:
  357.     AngryDino(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {}
  358.  
  359.     void draw(ConsoleDrawer& cd) {
  360.  
  361.         //Переходим от ячеек к координатам пикселей
  362.         int x = getX();
  363.         int y = getY();
  364.         COLORREF color = ConsoleColor::redx();
  365.         vector<vector <int> > matrix(width, vector<int>(height));
  366.         double d = _cellSize / 125.0;
  367.         cd.drawRect(Point(x + d, y + 63 * d), Point(x + 24 * d, y + 53 * d), ConsoleColor::redx(), true);
  368.         cd.drawRect(Point(x + 75 * d, y + 63 * d), Point(x + 98 * d, y + 53 * d), ConsoleColor::redx(), true);
  369.         cd.drawRect(Point(x + 27 * d, y + 93 * d), Point(x + 72 * d, y + 35 * d), ConsoleColor::redx(), true);//body
  370.         cd.drawRect(Point(x + 27 * d, y + 124 * d), Point(x + 42 * d, y + 94 * d), ConsoleColor::redx(), true);
  371.         cd.drawRect(Point(x + 57 * d, y + 124 * d), Point(x + 72 * d, y + 94 * d), ConsoleColor::redx(), true);
  372.         cd.drawRect(Point(x + 27 * d, y + 32 * d), Point(x + 88 * d, y + 2 * d), ConsoleColor::redx(), true);//head
  373.         cd.drawCircle(Point(x + 42 * d, y + 12 * d), 8 * d, ConsoleColor::gray(), true);
  374.         cd.drawTriangle(Point(x + 27 * d, y), Point(x + 49 * d, y - 45 * d), Point(x + 72 * d, y), ConsoleColor::red(), true);
  375.         cd.drawCircle(Point(x + 49 * d, y - 50 * d), 10 * d, ConsoleColor::white(), true);
  376.  
  377.     }
  378.  
  379.     void step(int direction) {
  380.         if (direction == 0)
  381.             _cellX -= 1;
  382.         if (direction == 1)
  383.             _cellY -= 1;
  384.         if (direction == 2)
  385.             _cellX += 1;
  386.         if (direction == 3)
  387.             _cellY += 1;
  388.     }
  389. };
  390.  
  391.  
  392.  
  393.  
  394. class DinoPark {
  395.     int score1, score2;
  396.     int hp1 = 3, hp2 = 3;
  397.     int _cellsXCount;
  398.     int _cellsYCount;
  399.     int _cellSize;
  400.     vector<Lake> _lake;
  401.     vector<block> _block;
  402.     vector<Obstacle> _obstacle;
  403.     vector<SnowMan> _snowmen;
  404.     vector<Tree> _trees;
  405.     vector<HappyDino> _hDino;
  406.     //vector<HappyDino2> _hDino2;
  407.     vector<AngryDino> _hDino3;
  408.  
  409. public:
  410.  
  411.     DinoPark(int width, int height, int cellSize) {
  412.         _cellsXCount = width;
  413.         _cellsYCount = height;
  414.         _cellSize = cellSize;
  415.     }
  416.     void setdinocolor(int num, COLORREF color) {
  417.         _hDino[num].color = color;
  418.     }
  419.  
  420.  
  421.     // Этот метод проверяет, что находится в клетке:
  422.     // -1 - снеговик ,озеро ,блок
  423.     //  0 - пусто
  424.     //  1 - дерево
  425.     //  2 - динозавр
  426.     //  3 - динозавр
  427.     //  4 - Злой динозавр
  428.     int find(int x, int y) {
  429.         for (int i = 0; i < _obstacle.size(); i++) {
  430.             if (_obstacle[i].isInside(x, y))
  431.                 return -1;
  432.         }
  433.         for (int i = 0; i < _trees.size(); i++) {
  434.             if (_trees[i].isInside(x, y))
  435.                 return 1;
  436.         }
  437.         for (int i = 0; i < _hDino.size(); i++) {
  438.             if (_hDino[i].isInside(x, y))
  439.                 return 2;
  440.         }
  441.         //  for (int i = 0; i < _hDino2.size(); i++) {
  442.         //      if (_hDino2[i].isInside(x, y))
  443.         //          return 2;
  444.         //  }
  445.         for (int i = 0; i < _hDino3.size(); i++) {
  446.             if (_hDino3[i].isInside(x, y))
  447.                 return 4;
  448.         }
  449.         return 0;
  450.     }
  451.  
  452.     // Метод для отрисовки всего поля
  453.     void draw(ConsoleDrawer& cd) {
  454.  
  455.         // Рисуем сетку
  456.         for (int i = 0; i <= _cellsXCount; i++) {
  457.             int x = i * _cellSize;
  458.             int y = _cellsYCount * _cellSize;
  459.             cd.drawLine(Point(x, 0), Point(x, y), ConsoleColor::black());
  460.         }
  461.         for (int i = 0; i <= _cellsYCount; i++) {
  462.             int x = _cellsXCount * _cellSize;
  463.             int y = i * _cellSize;
  464.             cd.drawLine(Point(0, y), Point(x, y), ConsoleColor::black());
  465.         }
  466.  
  467.         // Рисуем снеговиков
  468.         for (int i = 0; i < _snowmen.size(); i++) {
  469.             _snowmen[i].draw(cd);
  470.         }
  471.  
  472.         // Рисуем деревья
  473.         for (int i = 0; i < _trees.size(); i++) {
  474.             _trees[i].draw(cd);
  475.         }
  476.  
  477.         // Рисуем динозавра
  478.         for (int i = 0; i < _hDino.size(); i++) {
  479.             _hDino[i].draw(cd);
  480.         }
  481.         //  // Рисуем динозавра2
  482.         //  for (int i = 0; i < _hDino2.size(); i++) {
  483.         //      _hDino2[i].draw(cd);
  484.         //  }
  485.         // Рисуем ЗлогоДинозавра
  486.         for (int i = 0; i < _hDino3.size(); i++) {
  487.             _hDino3[i].draw(cd);
  488.         }
  489.         for (int i = 0; i < _lake.size(); i++) {
  490.             _lake[i].draw(cd);
  491.         }
  492.         for (int i = 0; i < _block.size(); i++) {
  493.             _block[i].draw(cd);
  494.         }
  495.     }
  496.  
  497.     // Метод обработки вашего хода
  498.     void step(ConsoleDrawer& cd) {
  499.  
  500.         // Пока ходит только счастливый динозавр
  501.  
  502.         // Ловим нажатие на клавиатуру
  503.         KEY_EVENT_RECORD key;
  504.         INPUT_RECORD irec[100];
  505.         DWORD cc;
  506.  
  507.         ReadConsoleInput(cd.get_conHandle(), irec, 100, &cc);
  508.         for (DWORD j = 0; j < cc; ++j) {
  509.             if (irec[j].EventType == KEY_EVENT && irec[j].Event.KeyEvent.bKeyDown) {
  510.  
  511.                 for (int i = 0; i < _hDino.size(); i++) {
  512.                     // Смотрим по сторонам
  513.                     // -1 - снеговик
  514.                     //  0 - пусто
  515.                     //  1 - дерево
  516.                     //  2 - динозавр
  517.                     vector<int> res = {
  518.                         lookLeft(_hDino[i].getCellX(),_hDino[i].getCellY()),
  519.                         lookUp(_hDino[i].getCellX(), _hDino[i].getCellY()),
  520.                         lookRight(_hDino[i].getCellX(), _hDino[i].getCellY()),
  521.                         lookDown(_hDino[i].getCellX(), _hDino[i].getCellY())
  522.                         //              lookLeft(_hDino2[i].getCellX(),_hDino2[i].getCellY()),
  523.                         //              lookUp(_hDino2[i].getCellX(), _hDino2[i].getCellY()),
  524.                         //              lookRight(_hDino2[i].getCellX(), _hDino2[i].getCellY()),
  525.                         //              lookDown(_hDino2[i].getCellX(), _hDino2[i].getCellY())
  526.                     };
  527.                     // Проверяем, какая именно кнопка была нажата
  528.                     switch (irec[j].Event.KeyEvent.wVirtualKeyCode) {
  529.                     case VK_LEFT:
  530.                         if (i == 0)
  531.                             if (res[0] == 1 || res[0] == 0) {
  532.                                 if (res[0] == 1)
  533.                                     score1++;
  534.                                 _hDino[0].step(0);
  535.  
  536.  
  537.                                 if (res[0] != 0) {
  538.  
  539.                                     refresh(cd);
  540.                                 }
  541.                             }
  542.                         break;
  543.                     case VK_UP:
  544.                         if (i == 0)
  545.                             if (res[1] == 1 || res[1] == 0) {
  546.                                 _hDino[0].step(1);
  547.                                 if (res[1] != 0) {
  548.                                     refresh(cd);
  549.                                 }
  550.                             }
  551.                         break;
  552.                     case VK_RIGHT:
  553.                         if (i == 0)
  554.                             if (res[2] == 1 || res[2] == 0) {
  555.                                 _hDino[0].step(2);
  556.                                 if (res[2] != 0) {
  557.                                     refresh(cd);
  558.                                 }
  559.                             }
  560.                         break;
  561.                     case VK_DOWN:
  562.                         if (i == 0)
  563.                             if (res[3] == 1 || res[3] == 0) {
  564.                                 _hDino[0].step(3);
  565.                                 if (res[3] != 0) {
  566.                                     refresh(cd);
  567.                                 }
  568.                             }
  569.                         break;
  570.  
  571.                         //Dino2
  572.                     case 0x57://W
  573.                         if (i == 1)
  574.                             if (res[1] == 1 || res[1] == 0) {
  575.                                 _hDino[1].step(1);
  576.                                 if (res[1] != 0) {
  577.                                     refresh(cd);
  578.                                 }
  579.                             }
  580.                         break;
  581.                     case 0x41://A
  582.                         if (i == 1)
  583.                             if (res[0] == 1 || res[0] == 0) {
  584.                                 _hDino[1].step(0);
  585.  
  586.  
  587.                                 if (res[0] != 0) {
  588.  
  589.                                     refresh(cd);
  590.                                 }
  591.                             }
  592.                         break;
  593.                     case 0x44://D
  594.                         if (i == 1)
  595.                             if (res[2] == 1 || res[2] == 0) {
  596.                                 _hDino[1].step(2);
  597.                                 if (res[2] != 0) {
  598.                                     refresh(cd);
  599.                                 }
  600.                             }
  601.                         break;
  602.                     case 0x53://S
  603.                         if (i == 1)
  604.                             if (res[3] == 1 || res[3] == 0) {
  605.                                 _hDino[1].step(3);
  606.                                 if (res[3] != 0) {
  607.                                     refresh(cd);
  608.                                 }
  609.                             }
  610.                         break;
  611.                     case VK_ESCAPE:
  612.                         if (i == 0) {
  613.                             cd.clearScreen();
  614.                             cout << "Score player 1: " << score1 << endl;
  615.                             cout << "HP player 1: " << hp1 << endl;
  616.                             cout << "Score player 2: " << score2 << endl;
  617.                             cout << "HP player 2: " << hp2 << endl;
  618.                             cd.stop();
  619.  
  620.                             break;
  621.                         }
  622.                     }
  623.  
  624.                 }
  625.  
  626.             }
  627.         }
  628.     }
  629.  
  630.     void addSnowMan(int x, int y) {
  631.         _obstacle.push_back(Obstacle(x, y, _cellSize));
  632.         _snowmen.push_back(SnowMan(x, y, _cellSize));
  633.         map[x][y] = -1;
  634.     }
  635.  
  636.     void addTree(int x, int y) {
  637.         _trees.push_back(Tree(x, y, _cellSize));
  638.         map[x][y] = 1;
  639.     }
  640.  
  641.     void addHappyDino(int x, int y) {
  642.         _hDino.push_back(HappyDino(x, y, _cellSize));
  643.         map[x][y] = 2;
  644.     }
  645.  
  646.     //void addHappyDino2(int x, int y) {
  647.     //  _hDino2.push_back(HappyDino2(x, y, _cellSize));
  648.     //}
  649.  
  650.     void addAngryDino(int x, int y) {
  651.         _hDino3.push_back(AngryDino(x, y, _cellSize));
  652.         map[x][y] = -2;
  653.     }
  654.     void addLake(int x, int y) {
  655.         _obstacle.push_back(Obstacle(x, y, _cellSize));
  656.         _lake.push_back(Lake(x, y, _cellSize));
  657.         map[x][y] = -1;
  658.     }
  659.     void addBlock(int x, int y) {
  660.         _obstacle.push_back(Obstacle(x, y, _cellSize));
  661.         _block.push_back(block(x, y, _cellSize));
  662.         map[x][y] = -1;
  663.     }
  664.  
  665.     // Взгляд на клетку вверх
  666.     int lookUp(int cellX, int cellY) {
  667.         if (cellY == 0)
  668.             return -1;
  669.         return find(cellX, cellY - 1);
  670.     }
  671.  
  672.     // Взгляд на клетку вниз
  673.     int lookDown(int cellX, int cellY) {
  674.         if (cellY == _cellsYCount - 1)
  675.             return -1;
  676.         return find(cellX, cellY + 1);
  677.     }
  678.  
  679.     // Взгляд на клетку вправо
  680.     int lookRight(int cellX, int cellY) {
  681.         if (cellX == _cellsXCount - 1)
  682.             return -1;
  683.         return find(cellX + 1, cellY);
  684.     }
  685.  
  686.     // Взгляд на клетку влево
  687.     int lookLeft(int cellX, int cellY) {
  688.         if (cellX == 0)
  689.             return -1;
  690.         return find(cellX - 1, cellY);
  691.     }
  692.  
  693.     // Обновляем картину
  694.     void refresh(ConsoleDrawer& cd) {
  695.         for (int i = 0; i < _hDino.size(); i++) {
  696.             // Смотрим, где стоит динозавр
  697.             int x = _hDino[i].getCellX();
  698.             int y = _hDino[i].getCellY();
  699.  
  700.             for (int i = 0; i < _hDino3.size(); i++) {
  701.                 int x1 = _hDino3[i].getCellX();
  702.                 int y2 = _hDino3[i].getCellY();
  703.                 if (x1 == x && y2 == y)
  704.                     cd.stop();
  705.                 //cout << "                                                           YOU DIED" << endl;
  706.                 //cout << "score player 1 : " << score1 << endl;
  707.                 //cout << "score player 2 : " << score2 << endl;
  708.             }
  709.  
  710.             // Находим то дерево, на котором он стоит
  711.             for (int j = 0; j < _trees.size(); j++) {
  712.  
  713.  
  714.                 if (_trees[j].isInside(x, y)) {
  715.                     //СЧЕТ елочек                                                   СЧЕТ елочек                              
  716.                     score1 = score1 + 1;
  717.                     //          hp = hp + 1;
  718.                     //cout << score;
  719.  
  720.  
  721.                     // Переносим дерево в другое место
  722.                     // Цикл нужен, чтобы не попасть в место, которое уже занято
  723.                     for (int k = 0; k < 100; k++) {
  724.                         int _x = rand() % _cellsXCount;
  725.                         int _y = rand() % _cellsYCount;
  726.  
  727.                         if (find(_x, _y) == 0) {
  728.                             _trees[j] = Tree(_x, _y, _cellSize);
  729.  
  730.  
  731.                             break;
  732.                         }
  733.                     }
  734.                 }
  735.             }
  736.         }
  737.     }
  738.  
  739.  
  740. };
  741.  
  742. int main() {
  743.     ConsoleDrawer cd;
  744.     srand(time(0));
  745.  
  746.     const int CellSize = 20;                                          //РАЗМЕР
  747.     width = cd.getWidth() / CellSize;
  748.     height = cd.getHeight() / CellSize;
  749.     DinoPark dinoPark(width, height, CellSize);
  750.     for (int i = 0; i < width; i++) {
  751.         map.push_back(vector<int>(0));
  752.         for (int j = 0; j < height; j++)
  753.             map[i].push_back(0);
  754.     }
  755.  
  756.     for (int i = 0; i < width; i++) {
  757.         dinoPark.addBlock(i, 0);
  758.         dinoPark.addBlock(i, height - 1);
  759.     }
  760.     for (int i = 1; i < height; i++) {
  761.         dinoPark.addBlock(width - 1, i);
  762.         dinoPark.addBlock(0, i);
  763.     }
  764.  
  765.  
  766.     dinoPark.addSnowMan(5, 3);
  767.     dinoPark.addSnowMan(5, 9);
  768.     dinoPark.addSnowMan(10, 4);
  769.  
  770.  
  771.  
  772.     for (int i = 1; i < width - 1; i++) {
  773.         dinoPark.addTree(i, 1);
  774.     }
  775.  
  776.  
  777.  
  778.     dinoPark.addLake(7, 5);
  779.     dinoPark.addLake(8, 5);
  780.     dinoPark.addLake(9, 5);
  781.     dinoPark.addLake(6, 5);
  782.     dinoPark.addLake(7, 6);
  783.     dinoPark.addLake(8, 6);
  784.     dinoPark.addLake(9, 6);
  785.     dinoPark.addLake(7, 7);
  786.     dinoPark.addLake(8, 7);
  787.     dinoPark.addLake(9, 7);
  788.     dinoPark.addLake(10, 7);
  789.  
  790.  
  791.     dinoPark.addHappyDino(1, 4);
  792.     dinoPark.addHappyDino(1, 2);
  793.     dinoPark.setdinocolor(1, ConsoleColor::purple());
  794.     dinoPark.addAngryDino(22, 6);
  795.  
  796.     cd.clearScreen();
  797.     while (cd.isWork()) {
  798.         cd.drawBackground();
  799.         dinoPark.draw(cd);
  800.         dinoPark.step(cd);
  801.     }
  802.  
  803.     system("pause");
  804.     return 0;
  805. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement