#include <iostream>
#include <Windows.h>
using namespace std;
HWND hWnd = GetConsoleWindow();
HDC hDC = GetDC(hWnd);
void RectangleShow(COLORREF Color, int x, int y, int w, int h)
{
HPEN hPen = CreatePen(PS_SOLID, 2, Color);
HBRUSH hBrush = GetStockBrush(NULL_BRUSH);
SelectObject(hDC, hPen);
SelectObject(hDC, hBrush);
Rectangle(hDC, x, y, x + w, y + h);
DeleteObject(hPen);
}
int main()
{
int x = 100;
int y = 100;
int squareSize = 200;
int width = 80;
int height = 40;
COLORREF background = GetPixel(hDC, 0, 0);
// Движение вправо
for (x = 100; x <= 100 + squareSize - width; x += 5)
{
RectangleShow(background, x - 5, y, width, height);
width -= 1;
if (width < 20)
width = 20;
RectangleShow(RGB(255, 0, 0), x, y, width, height);
Sleep(50);
}
// Движение вниз
for (y = 100; y <= 100 + squareSize - height; y += 5)
{
RectangleShow(background, x, y - 5, width, height);
RectangleShow(RGB(255, 0, 0), x, y, width, height);
Sleep(50);
}
// Движение влево
for (x = 100 + squareSize - width; x >= 100; x -= 5)
{
RectangleShow(background, x + 5, y, width, height);
width += 1;
if (width > 80)
width = 80;
RectangleShow(RGB(255, 0, 0), x, y, width, height);
Sleep(50);
}
// Движение вверх
for (y = 100 + squareSize - height; y >= 100; y -= 5)
{
RectangleShow(background, x, y + 5, width, height);
RectangleShow(RGB(255, 0, 0), x, y, width, height);
Sleep(50);
}
cin.get();
return 0;
}