diff --git a/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp b/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp index 5cc2564..23e61a5 100644 --- a/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp +++ b/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp @@ -1,25 +1,20 @@ -// lab16.cpp : Определяет точку входа для приложения. +// lab18.cpp : Определяет точку входа для приложения. // #define _CRT_SECURE_NO_WARNINGS -#include -#include #include "framework.h" -#include "lab16.h" -#include +#include "lab18.h" +#include #define MAX_LOADSTRING 100 -#define N 10 -#define M 15 -#define WIDTH 30 -#define HEIGHT 20 + // Глобальные переменные: -int steps = 0; -int gold = 0; +int counter = 0; HINSTANCE hInst; // текущий экземпляр WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка WCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна +int numImage = 0; // Отправить объявления функций, включенных в этот модуль кода: ATOM MyRegisterClass(HINSTANCE hInstance); @@ -39,7 +34,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, // Инициализация глобальных строк LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); - LoadStringW(hInstance, IDC_LAB16, szWindowClass, MAX_LOADSTRING); + LoadStringW(hInstance, IDC_LAB18, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Выполнить инициализацию приложения: @@ -48,7 +43,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, return FALSE; } - HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB16)); + HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB18)); MSG msg; @@ -83,10 +78,10 @@ ATOM MyRegisterClass(HINSTANCE hInstance) wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; - wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB16)); + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB18)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); - wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB16); + wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB18); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); @@ -121,327 +116,269 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) return TRUE; } -int map[N][M] = { - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, - {0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 3, 0, 0, 0}, +// +// ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM) +// +// ЦЕЛЬ: Обрабатывает сообщения в главном окне. +// +// WM_COMMAND - обработать меню приложения +// WM_PAINT - Отрисовка главного окна +// WM_DESTROY - отправить сообщение о выходе и вернуться +// +void Image1(HDC hdc, int cx, int cy, int size) { + int x1 = cx - size / 2; + int y1 = cy - size / 2; + int x2 = cx + size / 2; + int y2 = cy - size / 2; + int x3 = cx; + int y3 = cy + size / 2; - {0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 2, 0, 0, 2, 0}, - {0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 2, 0, 0, 2, 0}, - {0, 0, 0, 0, 3, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -}; -void drawMap(HDC hdc) { - HBRUSH hBrushEmptyCell = CreateSolidBrush(RGB(200, 200, 200)); - HBRUSH hBrushGold = CreateSolidBrush(RGB(255, 255, 0)); - HBRUSH hBrushWall = CreateSolidBrush(RGB(0, 0, 0)); - HBRUSH hBrushMan = CreateSolidBrush(RGB(0, 0, 255)); - HBRUSH brush[4] = { hBrushEmptyCell, hBrushMan, hBrushWall, hBrushGold }; + HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0)); + SelectObject(hdc, hPen); + MoveToEx(hdc, x1, y1, NULL); + LineTo(hdc, x2, y2); + LineTo(hdc, x3, y3); + LineTo(hdc, x1, y1); - int i, j; - - for (i = 0; i < N; i++) { - for (j = 0; j < M; j++) { - - int x1 = j * WIDTH; - int x2 = (j + 1) * WIDTH; - int y1 = i * HEIGHT; - int y2 = (i + 1) * HEIGHT; - - RECT r = { x1, y1, x2, y2 }; - FillRect(hdc, &r, brush[map[i][j]]); - } - } - for (i = 0; i < 4; i++) - DeleteObject(brush[i]); - - char strC[80]; - sprintf(strC, "steps = %d gold = %d", steps, gold); - - TCHAR strWin[80]; + char strC[5]; + sprintf(strC, "%d", counter); + TCHAR strWin[5]; OemToChar(strC, strWin); + TextOut(hdc, cx, cy, (LPCWSTR)strWin, _tcslen(strWin)); - HFONT hFont = CreateFont(HEIGHT, 0, 0, 0, 0, 0, 0, 0, - DEFAULT_CHARSET, 0, 0, 0, 0, L"Courier New"); - SelectObject(hdc, hFont); - SetTextColor(hdc, RGB(0, 0, 128)); - - TextOut(hdc, 10, HEIGHT * N, (LPCWSTR)strWin, _tcslen(strWin)); - - DeleteObject(hFont); + DeleteObject(hPen); } - - -void Left() { - int i, j; - - for (i = 0; i < N; i++) { - for (j = 0; j < M; j++) { - if (map[i][j] == 1) { - if (map[i][j - 1] == 0) { - map[i][j - 1] = 1; - map[i][j] = 0; - steps++; - } - else if (map[i][j - 1] == 3) { - map[i][j - 1] = 1; - map[i][j] = 0; - steps++; - gold++; - } - } - } - } -} -void Right() { - int i, j; - - for (i = 0; i < N; i++) { - for (j = M - 2; j >= 0; j--) { - if (map[i][j] == 1) { - if (map[i][j + 1] == 0) { - map[i][j + 1] = 1; - map[i][j] = 0; - steps++; - } - else if (map[i][j + 1] == 3) { - map[i][j + 1] = 1; - map[i][j] = 0; - steps++; - gold++; - } - } - } - } -} -void Up() { - int i, j; - - for (i = 0; i < N; i++) { - for (j = 0; j < M; j++) { - if (map[i][j] == 1) { - if (map[i - 1][j] == 0) { - map[i - 1][j] = 1; - map[i][j] = 0; - steps++; - } - else if (map[i - 1][j] == 3) { - map[i - 1][j] = 1; - map[i][j] = 0; - steps++; - gold++; - } - } - } - } -} -void Down() { - int i, j; - - for (i = N - 2; i >= 0; i--) { - for (j = 0; j < M; j++) { - if (map[i][j] == 1) { - if (map[i + 1][j] == 0) { - map[i + 1][j] = 1; - map[i][j] = 0; - steps++; - } - else if (map[i + 1][j] == 3) { - map[i + 1][j] = 1; - map[i][j] = 0; - steps++; - gold++; - } - } - } - } -} -void Save() { - FILE* fout; - fout = fopen("C:\\Temp\\Lection12\\game.txt", "wt"); - if (fout == NULL) { - printf("\nВыходной файл не создался\n"); +void RecursiveImage1_1(HDC hdc, int cx, int cy, int size) { + counter++; + Image1(hdc, cx, cy, size); + if (size < 20) { return; } - - fprintf(fout, "%d ", N); - fprintf(fout, "%d\n", M); - int i, j; - - for (i = 0; i < N; i++) { - for (j = 0; j < M; j++) { - fprintf(fout, "%d ", map[i][j]); - } - } - fprintf(fout, "%d ", steps); - fprintf(fout, "%d", gold); - - fclose(fout); - + RecursiveImage1_1(hdc, cx - size / 2, cy - size / 2, size / 2); + RecursiveImage1_1(hdc, cx + size / 2, cy - size / 2, size / 2); } -void Load() { - FILE* fin; - fin = fopen("C:\\Temp\\Lection12\\game.txt", "rt"); - if (fin == NULL) { - printf("Входной файл не найден"); - return; - } - - int n, m; - fscanf(fin, "%d", &n); - fscanf(fin, "%d", &m); - int i, j; - - for (i = 0; i < n; i++) { - for (j = 0; j < m; j++) { - fscanf(fin, "%d", &map[i][j]); - } - } - fscanf(fin, "%d ", &steps); - fscanf(fin, "%d", &gold); - - fclose(fin); -} -void SaveBinary() { - FILE* fout; - fout = fopen("C:\\Temp\\Lection12\\game_bin.bin", "wb"); - if (fout == NULL) { - printf("Выходной файл не создался"); +void RecursiveImage1_2(HDC hdc, int cx, int cy, int size) { + counter++; + Image1(hdc, cx, cy, size); + if (size < 20) { return; } - int n = N, m = M; - fwrite(&n, sizeof(n), 1, fout); - fwrite(&m, sizeof(m), 1, fout); - fwrite(map, sizeof(map), 1, fout); - fwrite(&steps, sizeof(steps), 1, fout); - fwrite(&gold, sizeof(gold), 1, fout); - - fclose(fout); + RecursiveImage1_2(hdc, cx - size / 2, cy - size / 2, size / 2); + RecursiveImage1_2(hdc, cx, cy + size / 2, size / 2); } -void LoadBinary() { - FILE* fin; - fin = fopen("C:\\Temp\\Lection12\\game_bin.bin", "rb"); - if (fin == NULL) { - printf("Входной файл не создался"); +void RecursiveImage1_3(HDC hdc, int cx, int cy, int size) { + counter++; + Image1(hdc, cx, cy, size); + if (size < 10) { return; } - int n, m; - fread(&n, sizeof(n), 1, fin); - fread(&m, sizeof(m), 1, fin); - fread(map, sizeof(map), 1, fin); - fread(&steps, sizeof(steps), 1, fin); - fread(&gold, sizeof(gold), 1, fin); - - fclose(fin); + RecursiveImage1_3(hdc, cx - size / 2, cy - size / 2, size / 2); + RecursiveImage1_3(hdc, cx + size / 2, cy - size / 2, size / 2); + RecursiveImage1_3(hdc, cx - size / 2, cy - size / 2, size / 2); + RecursiveImage1_3(hdc, cx, cy + size / 2, size / 2); } -void CreateWall() { - for (int i = 0; i < N; i++) { - for (int j = 0; j < M; j++) { - if (map[i][j] == 1) { - map[i][j - 1] = 2; - steps++; - } - } - } +void Image2(HDC hdc, int cx, int cy, int size) { + int x1 = cx - size / 2; + int y1 = cy - size; + int x2 = cx + size / 2; + int y2 = cy - size; + int x3 = cx - size; + int y3 = cy + size; + int x4 = cx + size; + int y4 = cy + size; + HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); + SelectObject(hdc, hPen); + + MoveToEx(hdc, x1, y1, NULL); + LineTo(hdc, x2, y2); + LineTo(hdc, x3, y3); + LineTo(hdc, x4, y4); + LineTo(hdc, x1, y1); + + DeleteObject(hPen); } -void CreateGold() { - for (int i = 0; i < N; i++) { - for (int j = 0; j < M; j++) { - if (map[i][j] == 1) { - map[i][j + 1] = 3; - steps++; - } - } +void RecursiveImage2_1(HDC hdc, int cx, int cy, int size) { + Image2(hdc, cx, cy, size); + if (size < 5) { + return; } - + RecursiveImage2_1(hdc, cx - size / 2, cy - size, size / 2); } -void Right2() { - for (int i = 0; i < N; i++) { - for (int j = 1; j < M; j++) { - if (map[i][j] == 1) { - - if (map[i - 1][j] == 2) { - map[i - 1][j] = 0; - steps++; - } - } - } +void RecursiveImage2_2(HDC hdc, int cx, int cy, int size) { + Image2(hdc, cx, cy, size); + if (size < 5) { + return; } - + RecursiveImage2_2(hdc, cx - size / 2, cy - size, size / 2); + RecursiveImage2_2(hdc, cx + size / 2, cy - size, size / 2); } -void Down2() { - for (int i = 0; i < N; i++) { - for (int j = 0; j < M; j++) { - if (map[i][j] == 1) { - if (map[i + 1][j] == 0) { - map[i + 1][j] = 3; - steps++; - } - } - } +void RecursiveImage2_3(HDC hdc, int cx, int cy, int size) { + Image2(hdc, cx, cy, size); + if (size < 15) { + return; } - + RecursiveImage2_3(hdc, cx - size / 2, cy - size, size / 2); + RecursiveImage2_3(hdc, cx + size / 2, cy - size, size / 2); + RecursiveImage2_3(hdc, cx - size, cy + size, size / 2); + RecursiveImage2_3(hdc, cx + size, cy + size, size / 2); } -void Destroy() { - int i, j; - for (int j = M - 1; j >= 0; j--) { - for (int i = 0; i < N; i++) { - if (map[i][j] == 1) { - int a; - for (a = j + 1; a < M && map[i][a] == 2; a++) { - map[i][a] = 0; - } - break; - } - } - } +void Image3(HDC hdc, int cx, int cy, int size) { + int x1 = cx; + int y1 = cy - size; + int x2 = cx + size; + int y2 = cy; + + int x3 = cx; + int y3 = cy + size; + + int x4 = cx - size; + int y4 = cy; + + HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); + SelectObject(hdc, hPen); + + MoveToEx(hdc, x1, y1, NULL); + LineTo(hdc, x2, y2); + LineTo(hdc, x3, y3); + LineTo(hdc, x4, y4); + LineTo(hdc, x1, y1); + + DeleteObject(hPen); } -void Destroy2() { - int i, j; - for (i = 0; i < N; i++) { - for (j = 0; j < M - 1; j++) { - if (map[i][j] == 1) { - if (map[i][j + 1] == 2) { - map[i][j + 1] = 0; - steps++; - } - if (map[i][j - 1] == 2) { - map[i][j - 1] = 0; - steps++; - } - if (map[i + 1][j + 1] == 2) { - map[i + 1][j + 1] = 0; - steps++; - } - if (map[i - 1][j - 1] == 2) { - map[i - 1][j - 1] = 0; - steps++; - } - if (map[i - 1][j] == 2) { - map[i - 1][j] = 0; - steps++; - } - if (map[i + 1][j] == 2) { - map[i + 1][j] = 0; - steps++; - } - if (map[i - 1][j + 1] == 2) { - map[i - 1][j + 1] = 0; - steps++; - } - if (map[i + 1][j - 1] == 2) { - map[i + 1][j - 1] = 0; - steps++; - } - j++; - } - } +void RecursiveImage3_1(HDC hdc, int cx, int cy, int size) { + Image3(hdc, cx, cy, size); + if (size < 20) { + return; } + RecursiveImage3_1(hdc, cx + size, cy, size / 2); + RecursiveImage3_1(hdc, cx - size, cy, size / 2); +} +void RecursiveImage3_2(HDC hdc, int cx, int cy, int size) { + Image3(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage3_2(hdc, cx + size, cy, size / 2); + RecursiveImage3_2(hdc, cx - size, cy, size / 2); + RecursiveImage3_2(hdc, cx, cy + size, size / 2); +} +void RecursiveImage3_3(HDC hdc, int cx, int cy, int size) { + Image3(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage3_3(hdc, cx + size, cy, size / 2); + RecursiveImage3_3(hdc, cx - size, cy, size / 2); + RecursiveImage3_3(hdc, cx, cy - size, size / 2); +} +void RecursiveImage3_4(HDC hdc, int cx, int cy, int size) { + Image3(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage3_4(hdc, cx + size, cy, size / 2); + RecursiveImage3_4(hdc, cx - size, cy, size / 2); + RecursiveImage3_4(hdc, cx, cy - size, size / 2); + RecursiveImage3_4(hdc, cx, cy + size, size / 2); +} +void Image4(HDC hdc, int cx, int cy, int size) { + int x1 = cx; + int y1 = cy - size; + + int x2 = cx + size / 4; + int y2 = cy - size / 4; + + int x3 = cx + size; + int y3 = cy; + + int x4 = cx + size / 4; + int y4 = cy + size / 4; + + int x5 = cx; + int y5 = cy + size; + + int x6 = cx - size / 4; + int y6 = cy + size / 4; + + int x7 = cx - size; + int y7 = cy; + + int x8 = cx - size / 4; + int y8 = cy - size / 4; + HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); + SelectObject(hdc, hPen); + + MoveToEx(hdc, x1, y1, NULL); + LineTo(hdc, x2, y2); + LineTo(hdc, x3, y3); + LineTo(hdc, x4, y4); + LineTo(hdc, x5, y5); + LineTo(hdc, x6, y6); + LineTo(hdc, x7, y7); + LineTo(hdc, x8, y8); + LineTo(hdc, x1, y1); + + DeleteObject(hPen); +} +void RecursiveImage4_1(HDC hdc, int cx, int cy, int size) { + Image4(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage4_1(hdc, cx + size, cy, size / 2); + RecursiveImage4_1(hdc, cx - size, cy, size / 2); +} +void RecursiveImage4_2(HDC hdc, int cx, int cy, int size) { + Image4(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage4_2(hdc, cx, cy + size, size / 2); + RecursiveImage4_2(hdc, cx, cy - size, size / 2); +} +void RecursiveImage4_3(HDC hdc, int cx, int cy, int size) { + Image4(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage4_3(hdc, cx, cy + size, size / 2); + RecursiveImage4_3(hdc, cx, cy - size, size / 2); + RecursiveImage4_3(hdc, cx - size, cy, size / 2); +} +void Image5(HDC hdc, int cx, int cy, int size) { + int x1 = cx - size; + int y1 = cy - size / 1.5; + + int x2 = cx + size; + int y2 = cy - size / 1.5; + + int x3 = cx + size / 2; + int y3 = cy + size / 2; + + int x4 = cx - size * 1.5; + int y4 = cy + size / 2; + HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); + SelectObject(hdc, hPen); + + MoveToEx(hdc, x1, y1, NULL); + LineTo(hdc, x2, y2); + LineTo(hdc, x3, y3); + LineTo(hdc, x4, y4); + LineTo(hdc, x1, y1); + + DeleteObject(hPen); +} +void RecursiveImage5_1(HDC hdc, int cx, int cy, int size) { + Image5(hdc, cx, cy, size); + if (size < 20) { + return; + } + RecursiveImage5_1(hdc, cx - size, cy - size / 1.5, size / 2); + RecursiveImage5_1(hdc, cx + size / 1.5, cy + size / 2, size / 2); + RecursiveImage5_1(hdc, cx + size, cy - size / 1.5, size / 2); + RecursiveImage5_1(hdc, cx - size * 1.5, cy + size / 2, size / 2); } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { @@ -450,53 +387,17 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_COMMAND: { case WM_KEYDOWN: - switch (wParam) { - case VK_DOWN: - Down(); + switch (wParam) + { + case 0xbe: + numImage++; + if (numImage >= 20) + numImage = 0; InvalidateRect(hWnd, NULL, TRUE); - break; - case VK_LEFT: - Left(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case VK_UP: - Up(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case VK_RIGHT: - Right(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case 0x53: //S - //Save(); - SaveBinary(); - break; - case 0x4c: //L - //Load(); - //LoadBinary(); - CreateWall(); - break; - case 0x52: //R - CreateGold(); - break; - case 0x55: //U - Right2(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case 0x44: //D - Down2(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case 0x5a: //Z - Destroy(); - InvalidateRect(hWnd, NULL, TRUE); - break; - case 0x41: //A - Destroy2(); - InvalidateRect(hWnd, NULL, TRUE); - break; - } break; + } + break; int wmId = LOWORD(wParam); + // Разобрать выбор в меню: switch (wmId) { case IDM_ABOUT: @@ -514,7 +415,36 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); - drawMap(hdc); + // TODO: Добавьте сюда любой код прорисовки, использующий HDC... + if (numImage == 0) + counter = 0; + RecursiveImage1_1(hdc, 200, 160, 160); + if (numImage == 1) + RecursiveImage1_2(hdc, 200, 160, 160); + if (numImage == 2) + RecursiveImage1_3(hdc, 200, 160, 160); + if (numImage == 3) + RecursiveImage2_1(hdc, 200, 200, 100); + if (numImage == 4) + RecursiveImage2_2(hdc, 200, 200, 100); + if (numImage == 5) + RecursiveImage2_3(hdc, 200, 200, 100); + if (numImage == 6) + RecursiveImage3_1(hdc, 200, 200, 100); + if (numImage == 7) + RecursiveImage3_2(hdc, 350, 200, 100); + if (numImage == 8) + RecursiveImage3_3(hdc, 350, 200, 100); + if (numImage == 9) + RecursiveImage3_4(hdc, 350, 200, 100); + if (numImage == 10) + RecursiveImage4_1(hdc, 300, 200, 100); + if (numImage == 11) + RecursiveImage4_2(hdc, 300, 200, 100); + if (numImage == 12) + RecursiveImage4_3(hdc, 300, 200, 100); + if (numImage == 13) + RecursiveImage5_1(hdc, 300, 200, 100); EndPaint(hWnd, &ps); } break;