Advertisement
Ifrail

Less7 Task3

Dec 12th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | None | 0 0
  1. /*Парк объектного периода в цвете*/
  2. /*
  3.     Код 3 этапа - Наследование
  4. */
  5.  
  6. #include <Windows.h>
  7. #include <iostream>
  8. #include <cmath>
  9. #include <vector>
  10. #include <ctime>
  11. #include <unordered_map>
  12.  
  13. using namespace std;
  14.  
  15. class Point {
  16.     int _x, _y;
  17.  
  18. public:
  19.     Point() {
  20.         _x = 0;
  21.         _y = 0;
  22.     }
  23.  
  24.     Point(int x, int y) {
  25.         _x = x;
  26.         _y = y;
  27.     }
  28.  
  29.     int getX() { return _x; }
  30.     int getY() { return _y; }
  31.  
  32.     void setX(int x) { _x = x; }
  33.     void setY(int y) { _y = y; }
  34.  
  35.     double length(Point p) {
  36.         return sqrt(pow(p.getX() - _x, 2) + pow(p.getY() - _y, 2));
  37.     }
  38.  
  39. };
  40.  
  41.  
  42.  
  43.  
  44. class ConsoleColor {
  45. public:
  46.     static COLORREF red() { return RGB(255, 0, 0); }
  47.     static COLORREF green() { return RGB(0, 255, 0); }
  48.     static COLORREF blue() { return RGB(0, 0, 255); }
  49.     static COLORREF yellow() { return RGB(255, 255, 0); }
  50.     static COLORREF gray() { return RGB(128, 128, 128); }
  51. };
  52.  
  53.  
  54. class ConsoleDrawer {
  55.     HANDLE _conHandle;
  56.     HWND _hwnd;
  57.     HDC _hdc;
  58.     PAINTSTRUCT _ps;
  59.     unordered_map<COLORREF, HGDIOBJ> _bm;
  60.     unordered_map<COLORREF, HGDIOBJ> _pm;
  61.     HGDIOBJ _curentBrush;
  62.     HGDIOBJ _curentPen;
  63.  
  64.  
  65.     int _width;
  66.     int _height;
  67.     bool _isWork = true;
  68.  
  69.     COLORREF getColor(int r, int g, int b) {
  70.         return RGB(r, g, b);
  71.     }
  72.  
  73.     void selectBrush(COLORREF color, bool filled = false) {
  74.         if (!filled) {
  75.             SelectObject(_hdc, CreateSolidBrush(NULL_BRUSH));
  76.             return;
  77.         }
  78.         if (_bm.find(color) == _bm.end())
  79.             _bm.insert({color, CreateSolidBrush(color) });
  80.  
  81.         if (_curentBrush != _bm[color]) {
  82.             _curentBrush = _bm[color];
  83.             SelectObject(_hdc, _curentBrush);
  84.         }
  85.     }
  86.  
  87.     void selectPen(COLORREF color) {
  88.         if (_pm.find(color) == _pm.end())
  89.             _pm.insert({ color, CreatePen(PS_SOLID, 1, color) });
  90.         if (_curentPen != _pm[color]) {
  91.             _curentPen = _pm[color];
  92.             SelectObject(_hdc, _curentPen);
  93.         }
  94.     }
  95.  
  96. public:
  97.     ConsoleDrawer() {
  98.         setlocale(LC_ALL, "");
  99.         _conHandle = GetStdHandle(STD_INPUT_HANDLE);
  100.         int t = sizeof(HGDIOBJ);
  101.         _hwnd = GetConsoleWindow();
  102.         RECT rc;
  103.         GetClientRect(_hwnd, &rc);
  104.         _width = rc.right;
  105.         _height = rc.bottom;
  106.         _hdc = GetDC(_hwnd);
  107.     }
  108.  
  109.     void clearScreen() {
  110.         system("cls");
  111.     }
  112.  
  113.     int getWidth() { return _width; }
  114.     int getHeight() { return _height; }
  115.  
  116.     void processLoop(int time) {
  117.         Sleep(time);
  118.  
  119.         KEY_EVENT_RECORD key;
  120.         INPUT_RECORD irec[100];
  121.         DWORD cc;
  122.         PeekConsoleInput(_conHandle, irec, 100, &cc);
  123.         for (DWORD i = 0; i < 100; ++i) {
  124.             if (irec[i].EventType == KEY_EVENT && ((KEY_EVENT_RECORD&)irec[i].Event).bKeyDown) {
  125.                 key = (KEY_EVENT_RECORD&)irec[i].Event;
  126.                 if (key.wVirtualKeyCode == VK_ESCAPE)
  127.                     _isWork = false;
  128.             }
  129.         }
  130.     }
  131.  
  132.     void drawBackground(COLORREF color = 0) {
  133.         RECT rc;
  134.         GetClientRect(_hwnd, &rc);
  135.         drawRect(Point(), Point(_width, _height), color, true);
  136.     }
  137.  
  138.     bool isWork () {
  139.        
  140.         return _isWork;
  141.     }
  142.  
  143.     void drawPoint(Point p, COLORREF color) {
  144.         SetPixel(_hdc, p.getX(), p.getY(), color);
  145.     }
  146.  
  147.     void drawLine(Point p1, Point p2, COLORREF color) {
  148.         selectPen(color);
  149.         //SelectObject(_hdc, CreatePen(PS_SOLID, 1, color));
  150.         //SetDCPenColor(_hdc, color);
  151.         MoveToEx(_hdc, p1.getX(), p1.getY(), NULL);
  152.         LineTo(_hdc, p2.getX(), p2.getY());
  153.     }
  154.  
  155.     void drawCircle(Point c, int radius, COLORREF color, bool filled = false) {
  156.         selectPen(color);
  157.         selectBrush(color, filled);
  158.         //SetDCBrushColor(_hdc, color);
  159.         Ellipse(_hdc, c.getX() - radius, c.getY() - radius, c.getX() + radius, c.getY() + radius);
  160.        
  161.     }
  162.  
  163.     // Рисует прямоугольник по двум точкам
  164.     // p1 - верхний левый угол
  165.     // p2 - нижний правый угол
  166.     void drawRect(Point p1, Point p2, COLORREF color, bool filled = false) {
  167.         selectPen(color);
  168.         selectBrush(color, filled);
  169.         Rectangle(_hdc, p1.getX(), p1.getY(), p2.getX(), p2.getY());
  170.     }
  171. };
  172.  
  173.  
  174. class ParkObject {
  175. protected:
  176.     int _cellX, _cellY, _cellSize;
  177.  
  178. public:
  179.     ParkObject(int cellX, int cellY, int cellSize) {
  180.         _cellX = cellX;
  181.         _cellY = cellY;
  182.         _cellSize = cellSize;
  183.     }
  184.  
  185.     int getX() {
  186.         return _cellX * _cellSize;
  187.     }
  188.  
  189.     int getY() {
  190.         return _cellY * _cellSize;
  191.     }
  192.  
  193. };
  194.  
  195. class Stone : public ParkObject  {
  196.    
  197. public:
  198.     Stone(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) { }
  199.  
  200.     void draw(ConsoleDrawer &cd) {
  201.         //Переходим от ячеек к координатам пикселей
  202.         int x = getX();
  203.         int y = getY();
  204.  
  205.         // рисуем камень как серый прямоугольник в размер ячейки
  206.         cd.drawRect(Point(x, y), Point(x + _cellSize+1, y + _cellSize+1), ConsoleColor::gray(), true);
  207.     }
  208. };
  209.  
  210. class Grace : public ParkObject {
  211.     // Рост
  212.     int _growth;
  213.  
  214.     COLORREF calculateColor() {
  215.         COLORREF color = RGB(0, 100, 0);
  216.         if (_growth > 24 && _growth < 50)
  217.             color = RGB(34, 139, 34);
  218.         else if (_growth > 49 && _growth < 75)
  219.             color = RGB(173, 255, 47);
  220.         else if (_growth >= 75)
  221.             color = RGB(0, 255, 0);
  222.         return color;
  223.     }
  224.  
  225. public:
  226.     Grace(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {
  227.         _growth = 0;
  228.     }
  229.  
  230.     void draw(ConsoleDrawer &cd) {
  231.         //Переходим от ячеек к координатам пикселей
  232.         int x = getX();
  233.         int y = getY();
  234.  
  235.         COLORREF color = calculateColor();
  236.         // рисуем траву как несколько зеленых кружочков
  237.         cd.drawCircle(Point(x + _cellSize / 2, y + _cellSize / 2), 2, color, true);
  238.         cd.drawCircle(Point(x  + 4 , y  + 4), 2, color, true);
  239.         cd.drawCircle(Point(x + _cellSize - 4, y + _cellSize - 4), 2, color, true);
  240.         cd.drawCircle(Point(x + _cellSize -4, y + 4), 2, color, true);
  241.         cd.drawCircle(Point(x + 4, y + _cellSize - 4), 2, color, true);
  242.     }
  243.  
  244.     void step() {
  245.         _growth++;
  246.         if (_growth > 100) _growth = 100;
  247.     }
  248. };
  249.  
  250. class DinoPark {
  251.     int _cellsXCount;
  252.     int _cellsYCount;
  253.     int _cellSize;
  254.     vector<Stone> _stones;
  255.     vector<Grace> _graces;
  256.  
  257. public:
  258.     DinoPark(int width, int height, int cellSize) {
  259.         //_cd = cd;
  260.         _cellsXCount = width;  
  261.         _cellsYCount = height;
  262.         _cellSize = cellSize;
  263.     }
  264.  
  265.     void draw(ConsoleDrawer &cd) {
  266.         // Рисуем сетку
  267.         for (int i = 0; i <= _cellsXCount; i++) {
  268.             int x = i*_cellSize;
  269.             int y = _cellsYCount*_cellSize;
  270.             cd.drawLine(Point(x, 0), Point(x, y), ConsoleColor::yellow());
  271.         }
  272.         for (int i = 0; i <= _cellsYCount; i++) {
  273.             int x = _cellsXCount*_cellSize;
  274.             int y = i*_cellSize;
  275.             cd.drawLine(Point(0, y), Point(x, y), ConsoleColor::yellow());
  276.         }
  277.  
  278.         // Рисуем камни
  279.         for (int i = 0; i < _stones.size(); i++)
  280.             _stones[i].draw(cd);
  281.  
  282.         // Рисуем траву
  283.         for (int i = 0; i < _graces.size(); i++)
  284.             _graces[i].draw(cd);
  285.     }
  286.  
  287.     void step() {
  288.         // Трава
  289.         for (int i = 0; i < _graces.size(); i++)
  290.             _graces[i].step();
  291.  
  292.         // Динозавр травоядный
  293.         for (int i = 0; i < _graces.size(); i++) {
  294.  
  295.             _graces[i].step();
  296.         }
  297.            
  298.  
  299.     }
  300.  
  301.     void addStone(int x, int y) {
  302.         _stones.push_back(Stone(x, y, _cellSize));
  303.     }
  304.  
  305.     void addGrace(int x, int y) {
  306.         _graces.push_back(Grace(x, y, _cellSize));
  307.     }
  308. };
  309.  
  310.  
  311.  
  312. int main() {
  313.     ConsoleDrawer cd;
  314.     const int CellSize = 40;
  315.     int width = cd.getWidth() / CellSize;
  316.     int height = cd.getHeight() / CellSize;
  317.     DinoPark dinoPark(width, height, CellSize);
  318.  
  319.  
  320.     dinoPark.addStone(1, 1);
  321.     dinoPark.addStone(5, 6);
  322.     dinoPark.addStone(10, 4);
  323.  
  324.     dinoPark.addGrace(1, 2);
  325.     dinoPark.addGrace(1, 3);
  326.     dinoPark.addGrace(1, 4);
  327.  
  328.     cd.clearScreen();
  329.  
  330.     while (cd.isWork()) {
  331.         cd.drawBackground(RGB(100, 0, 0));
  332.         dinoPark.draw(cd);
  333.         dinoPark.step();
  334.         cd.processLoop(1000);
  335.     }
  336.     return 0;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement