Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Парк объектного периода в цвете*/
- /*
- Код 3 этапа - Наследование
- */
- #include <Windows.h>
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <unordered_map>
- using namespace std;
- class Point {
- int _x, _y;
- public:
- Point() {
- _x = 0;
- _y = 0;
- }
- Point(int x, int y) {
- _x = x;
- _y = y;
- }
- int getX() { return _x; }
- int getY() { return _y; }
- void setX(int x) { _x = x; }
- void setY(int y) { _y = y; }
- double length(Point p) {
- return sqrt(pow(p.getX() - _x, 2) + pow(p.getY() - _y, 2));
- }
- };
- class ConsoleColor {
- public:
- static COLORREF red() { return RGB(255, 0, 0); }
- static COLORREF green() { return RGB(0, 255, 0); }
- static COLORREF blue() { return RGB(0, 0, 255); }
- static COLORREF yellow() { return RGB(255, 255, 0); }
- static COLORREF gray() { return RGB(128, 128, 128); }
- };
- class ConsoleDrawer {
- HANDLE _conHandle;
- HWND _hwnd;
- HDC _hdc;
- PAINTSTRUCT _ps;
- unordered_map<COLORREF, HGDIOBJ> _bm;
- unordered_map<COLORREF, HGDIOBJ> _pm;
- HGDIOBJ _curentBrush;
- HGDIOBJ _curentPen;
- int _width;
- int _height;
- bool _isWork = true;
- COLORREF getColor(int r, int g, int b) {
- return RGB(r, g, b);
- }
- void selectBrush(COLORREF color, bool filled = false) {
- if (!filled) {
- SelectObject(_hdc, CreateSolidBrush(NULL_BRUSH));
- return;
- }
- if (_bm.find(color) == _bm.end())
- _bm.insert({color, CreateSolidBrush(color) });
- if (_curentBrush != _bm[color]) {
- _curentBrush = _bm[color];
- SelectObject(_hdc, _curentBrush);
- }
- }
- void selectPen(COLORREF color) {
- if (_pm.find(color) == _pm.end())
- _pm.insert({ color, CreatePen(PS_SOLID, 1, color) });
- if (_curentPen != _pm[color]) {
- _curentPen = _pm[color];
- SelectObject(_hdc, _curentPen);
- }
- }
- public:
- ConsoleDrawer() {
- setlocale(LC_ALL, "");
- _conHandle = GetStdHandle(STD_INPUT_HANDLE);
- int t = sizeof(HGDIOBJ);
- _hwnd = GetConsoleWindow();
- RECT rc;
- GetClientRect(_hwnd, &rc);
- _width = rc.right;
- _height = rc.bottom;
- _hdc = GetDC(_hwnd);
- }
- void clearScreen() {
- system("cls");
- }
- int getWidth() { return _width; }
- int getHeight() { return _height; }
- void processLoop(int time) {
- Sleep(time);
- KEY_EVENT_RECORD key;
- INPUT_RECORD irec[100];
- DWORD cc;
- PeekConsoleInput(_conHandle, irec, 100, &cc);
- for (DWORD i = 0; i < 100; ++i) {
- if (irec[i].EventType == KEY_EVENT && ((KEY_EVENT_RECORD&)irec[i].Event).bKeyDown) {
- key = (KEY_EVENT_RECORD&)irec[i].Event;
- if (key.wVirtualKeyCode == VK_ESCAPE)
- _isWork = false;
- }
- }
- }
- void drawBackground(COLORREF color = 0) {
- RECT rc;
- GetClientRect(_hwnd, &rc);
- drawRect(Point(), Point(_width, _height), color, true);
- }
- bool isWork () {
- return _isWork;
- }
- void drawPoint(Point p, COLORREF color) {
- SetPixel(_hdc, p.getX(), p.getY(), color);
- }
- void drawLine(Point p1, Point p2, COLORREF color) {
- selectPen(color);
- //SelectObject(_hdc, CreatePen(PS_SOLID, 1, color));
- //SetDCPenColor(_hdc, color);
- MoveToEx(_hdc, p1.getX(), p1.getY(), NULL);
- LineTo(_hdc, p2.getX(), p2.getY());
- }
- void drawCircle(Point c, int radius, COLORREF color, bool filled = false) {
- selectPen(color);
- selectBrush(color, filled);
- //SetDCBrushColor(_hdc, color);
- Ellipse(_hdc, c.getX() - radius, c.getY() - radius, c.getX() + radius, c.getY() + radius);
- }
- // Рисует прямоугольник по двум точкам
- // p1 - верхний левый угол
- // p2 - нижний правый угол
- void drawRect(Point p1, Point p2, COLORREF color, bool filled = false) {
- selectPen(color);
- selectBrush(color, filled);
- Rectangle(_hdc, p1.getX(), p1.getY(), p2.getX(), p2.getY());
- }
- };
- class ParkObject {
- protected:
- int _cellX, _cellY, _cellSize;
- public:
- ParkObject(int cellX, int cellY, int cellSize) {
- _cellX = cellX;
- _cellY = cellY;
- _cellSize = cellSize;
- }
- int getX() {
- return _cellX * _cellSize;
- }
- int getY() {
- return _cellY * _cellSize;
- }
- };
- class Stone : public ParkObject {
- public:
- Stone(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) { }
- void draw(ConsoleDrawer &cd) {
- //Переходим от ячеек к координатам пикселей
- int x = getX();
- int y = getY();
- // рисуем камень как серый прямоугольник в размер ячейки
- cd.drawRect(Point(x, y), Point(x + _cellSize+1, y + _cellSize+1), ConsoleColor::gray(), true);
- }
- };
- class Grace : public ParkObject {
- // Рост
- int _growth;
- COLORREF calculateColor() {
- COLORREF color = RGB(0, 100, 0);
- if (_growth > 24 && _growth < 50)
- color = RGB(34, 139, 34);
- else if (_growth > 49 && _growth < 75)
- color = RGB(173, 255, 47);
- else if (_growth >= 75)
- color = RGB(0, 255, 0);
- return color;
- }
- public:
- Grace(int cellX, int cellY, int cellSize) : ParkObject(cellX, cellY, cellSize) {
- _growth = 0;
- }
- void draw(ConsoleDrawer &cd) {
- //Переходим от ячеек к координатам пикселей
- int x = getX();
- int y = getY();
- COLORREF color = calculateColor();
- // рисуем траву как несколько зеленых кружочков
- cd.drawCircle(Point(x + _cellSize / 2, y + _cellSize / 2), 2, color, true);
- cd.drawCircle(Point(x + 4 , y + 4), 2, color, true);
- cd.drawCircle(Point(x + _cellSize - 4, y + _cellSize - 4), 2, color, true);
- cd.drawCircle(Point(x + _cellSize -4, y + 4), 2, color, true);
- cd.drawCircle(Point(x + 4, y + _cellSize - 4), 2, color, true);
- }
- void step() {
- _growth++;
- if (_growth > 100) _growth = 100;
- }
- };
- class DinoPark {
- int _cellsXCount;
- int _cellsYCount;
- int _cellSize;
- vector<Stone> _stones;
- vector<Grace> _graces;
- public:
- DinoPark(int width, int height, int cellSize) {
- //_cd = cd;
- _cellsXCount = width;
- _cellsYCount = height;
- _cellSize = cellSize;
- }
- void draw(ConsoleDrawer &cd) {
- // Рисуем сетку
- for (int i = 0; i <= _cellsXCount; i++) {
- int x = i*_cellSize;
- int y = _cellsYCount*_cellSize;
- cd.drawLine(Point(x, 0), Point(x, y), ConsoleColor::yellow());
- }
- for (int i = 0; i <= _cellsYCount; i++) {
- int x = _cellsXCount*_cellSize;
- int y = i*_cellSize;
- cd.drawLine(Point(0, y), Point(x, y), ConsoleColor::yellow());
- }
- // Рисуем камни
- for (int i = 0; i < _stones.size(); i++)
- _stones[i].draw(cd);
- // Рисуем траву
- for (int i = 0; i < _graces.size(); i++)
- _graces[i].draw(cd);
- }
- void step() {
- // Трава
- for (int i = 0; i < _graces.size(); i++)
- _graces[i].step();
- // Динозавр травоядный
- for (int i = 0; i < _graces.size(); i++) {
- _graces[i].step();
- }
- }
- void addStone(int x, int y) {
- _stones.push_back(Stone(x, y, _cellSize));
- }
- void addGrace(int x, int y) {
- _graces.push_back(Grace(x, y, _cellSize));
- }
- };
- int main() {
- ConsoleDrawer cd;
- const int CellSize = 40;
- int width = cd.getWidth() / CellSize;
- int height = cd.getHeight() / CellSize;
- DinoPark dinoPark(width, height, CellSize);
- dinoPark.addStone(1, 1);
- dinoPark.addStone(5, 6);
- dinoPark.addStone(10, 4);
- dinoPark.addGrace(1, 2);
- dinoPark.addGrace(1, 3);
- dinoPark.addGrace(1, 4);
- cd.clearScreen();
- while (cd.isWork()) {
- cd.drawBackground(RGB(100, 0, 0));
- dinoPark.draw(cd);
- dinoPark.step();
- cd.processLoop(1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement